OAuth: Use the OAuth client with embedded browser
For the first authorization (when we don't have a refresh token or it is not valid anymore) using the browser, embedded into your application, you need to take the following steps:
// create and setup the http transport to be used to talk to // the authorization server TElHTTPSClient oauthTransport = new TElHTTPSClient(); // assign an event handler to validate SSL certificate(s) oauthTransport.OnCertificateValidate += ...;
// create a OAuth 2.0 client TElSimpleOAuth2Client oauth = new TElSimpleOAuth2Client(); // assign the created HTTPS transport oauth.HTTPClient = https; // set the local URL to be used during authorization; // the specified port must be free and the application // has to be allowed to open a listening socket on that port oauth.RedirectURL = @"http://localhost:5050/"; // authorization server URLs oauth.AuthURL = @"https://accounts.google.com/o/oauth2/auth"; oauth.TokenURL = @"https://accounts.google.com/o/oauth2/token"; // copy the client id and the client secret of your app // registered in Google Developers Console oauth.ClientID = @"your_client_id"; oauth.ClientSecret = @"your_client_secret"; // tell the authorization server what access is needed oauth.Scope = @"https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile";
If the URL provided is not empty, the application needs to direct the embedded browser to this URL, wait for authentication and obtain the authorization code.
Retrieval of the authorization code is a heuristic procedure, where you need to analyze browser parameters and the page being shown. For example, Google server shows the web page, that has a title which starts with "Success code=", followed by the authorization code. And if your code analyzes the title, it can retrieve the authorization code automatically.