Quantcast
Channel: WE MOVED to github.com/microsoft/cpprestsdk. This site is not monitored!
Viewing all articles
Browse latest Browse all 4845

Updated Wiki: OAuth Client

$
0
0

OAuth Client (2.2+ only)

OAuth Client is the client side implementation of the OAuth protocol. We support both OAuth1 and OAuth2.

OAuth2

To use the OAuth2 authoriziation, you need to set up your app key/app secrets, authorization/token endpoints and provide your redirect uri.
#include "cpprest\oauth2_handler.h"
#include "cpprest\http_client.h"usingnamespace web::http;
usingnamespace web::http::client;
usingnamespace web::http::client::experimental;

oauth2_config m_oauth2_config(U("000000004C11C831"), /* Your live App key*/
            U("r4zFBhg1qP5AvMZBLyFlMFdhAT1mkg1m"), /* Your live App secret*/
            U("https://login.live.com/oauth20_authorize.srf"), /* Authorization endpoint*/
            U("https://login.live.com/oauth20_token.srf"), /* Token endpoint */
            U("https://login.live.com/oauth20_desktop.srf")); /* Redirect URI */

If the server requires you to provide the access scope, you may use the set_scope(utility::string_t scope) member function.
m_oauth2_config.set_scope(L"wl.basic wl.skydrive");

If you want to use the implicit grant flow instead of authorization code flow, you can use the set_implicit_grant(bool) function.
m_oauth2_config.set_implicit_grant(true);

After creating this oauth2_config instance, you need to get the authorization request uri by using build_authorization_uri(bool) function. Open the web browser (if you are not building a web based application) with this authorization request uri , login and authorize the access. Also, you need to set up a http_listener to listen on the redirect_uri you provided and capture the access token by using token_from_redirected_uri().

http_listener m_listener(redirect_uri);
auto auth_uri = m_oauth2_config.build_authorization_uri(true);
m_listener->support([this](http::http_request request) -> void
{
   m_oauth2_config.token_from_redirected_uri(request.request_uri()).then([this,request]() -> void
    {
         auto token = m_config.token().access_token(); // Get the access token
    });
    request.reply(status_codes::OK, U("Ok."));
});
m_listener->open().wait();

If you successfully get the access token, you can pass this m_oauth2_config to the http_client_config::set_oauth2(), construct http_client with this http_client_config then all the http requests by this client are OAuth2 authenticated.
http_client_config http_config;
http_config.set_oauth2(m_oauth2_config);
http_client m_live_client (U("https://apis.live.net/v5.0/"), http_config);
m_live_client.request(methods::GET, U("me/skydrive/my_photos")).wait();

OAuth1

To use the OAuth1 authoriziation, you need to set up the oauth1_config as using OAuth2. The difference is OAuth1 requires to provide the temporary credentials, you need to provide the request_token endpoint.
oauth1_config m_oauth1_config(U("Client_key"), 
            U("Client_secret"),
            U("https://api.twitter.com/oauth/request_token"),
            U("https://api.twitter.com/oauth/authorize"),
            U("https://api.twitter.com/oauth/access_token"),
            U("http://testhost.local:8890/"))

Viewing all articles
Browse latest Browse all 4845

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>