How do I set return_uri for GoogleWebAuthorizationBroker.AuthorizeAsync? How do I set return_uri for GoogleWebAuthorizationBroker.AuthorizeAsync? asp.net asp.net

How do I set return_uri for GoogleWebAuthorizationBroker.AuthorizeAsync?


You can use this code: (original idea from http://coderissues.com/questions/27512300/how-to-append-login-hint-usergmail-com-to-googlewebauthorizationbroker)

dsAuthorizationBroker.RedirectUri = "my localhost redirect uri";UserCredential credential = await dsAuthorizationBroker.AuthorizeAsync(...

dsAuthorizationBroker.cs

using System;using System.Collections.Generic;using System.Threading;using System.Threading.Tasks;using Google.Apis.Auth.OAuth2;using Google.Apis.Auth.OAuth2.Flows;using Google.Apis.Auth.OAuth2.Requests;using Google.Apis.Util.Store;namespace OAuth2{        public class dsAuthorizationBroker : GoogleWebAuthorizationBroker    {        public static string RedirectUri;        public new static async Task<UserCredential> AuthorizeAsync(            ClientSecrets clientSecrets,            IEnumerable<string> scopes,            string user,            CancellationToken taskCancellationToken,            IDataStore dataStore = null)        {            var initializer = new GoogleAuthorizationCodeFlow.Initializer            {                ClientSecrets = clientSecrets,            };            return await AuthorizeAsyncCore(initializer, scopes, user,                taskCancellationToken, dataStore).ConfigureAwait(false);        }        private static async Task<UserCredential> AuthorizeAsyncCore(            GoogleAuthorizationCodeFlow.Initializer initializer,            IEnumerable<string> scopes,            string user,            CancellationToken taskCancellationToken,            IDataStore dataStore)        {            initializer.Scopes = scopes;            initializer.DataStore = dataStore ?? new FileDataStore(Folder);            var flow = new dsAuthorizationCodeFlow(initializer);            return await new AuthorizationCodeInstalledApp(flow,                 new LocalServerCodeReceiver())                .AuthorizeAsync(user, taskCancellationToken).ConfigureAwait(false);        }    }    public class dsAuthorizationCodeFlow : GoogleAuthorizationCodeFlow    {        public dsAuthorizationCodeFlow(Initializer initializer)            : base(initializer) { }        public override AuthorizationCodeRequestUrl                       CreateAuthorizationCodeRequest(string redirectUri)        {            return base.CreateAuthorizationCodeRequest(dsAuthorizationBroker.RedirectUri);        }    }    }


If you are trying to use GoogleWebAuthorizationBroker.AuthorizeAsync in a .NET application NON-web server application i.e. C# Console App command line program, it's crucial when creating the Google OAuth profile (https://console.developers.google.com/apis) in the credentials to select the following. It's hidden and if you don't do it this way, it has to go through an approval process if you choose the radio button "Other". Also note by just copying the contents of the JSON parameters created in the steps below and replacing your client_id/secret with a web app version will still fail. Make a new OAuth client profile for your Google API console.

CLICK "HELP ME CHOOSE"

Step 1

CHOOSE YOUR INTENDED API LIBRARY ie (Google Calendar API) Select "User Data"

Step 2

"Yeah -NO AUTHORIZATION REQUIRED FILEDS" ie Javascript & RedirectNow you have a profile without the authorization

enter image description here

Use the "Download JSON" and save it to your application to reference in the code below. When you look inside this file, you will notice a different set of parameters as well to tell the broker this is an application. In this example, I am accessing the scope Calendar API. Just change the scope to whatever API you are trying to access.

   string[] Scopes = { CalendarService.Scope.Calendar }; //requires full scope to get ACL list..                string ApplicationName = "Name Of Your Application In Authorization Screen";                //just reference the namespaces in your using block                using (var stream = new FileStream("other_client_id.json", FileMode.Open, FileAccess.Read))                {                    // The file token.json stores the user's access and refresh tokens, and is created                    // automatically when the authorization flow completes for the first time.                    string credPath = "other_token.json";                    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(                        GoogleClientSecrets.Load(stream).Secrets,                        Scopes,                        "user",                        CancellationToken.None,                        new FileDataStore(credPath, true)).Result;                               }                // Create Google Calendar API service.                var service = new CalendarService(new BaseClientService.Initializer()                {                    HttpClientInitializer = credential,                    ApplicationName = ApplicationName,                });                //Then your ready to grab data from here using the methods mentioned in Google Calendar API docs


selecting "other" while creating oAuth Client ID helped me resolve the redirection issue for me. (Having the "Web Application" option tries to redirect to some url with random port, which is very annoying)

Now my Gmail API works like a charm :)