-->

GoogleWebAuthorizationBroker is not working from I

2019-01-29 12:17发布

问题:

I am using the following code to authenticate to Google using the Google .Net client library.

public static void auth()
{

string clientId = "xxxxxx.apps.googleusercontent.com";
string clientSecret = "xxxxx";


string[] scopes = new string[] { "https://www.googleapis.com/auth/contacts.readonly" };     // view your basic profile info.
try
{
    // Use the current Google .net client library to get the Oauth2 stuff.
    UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
                                                                                 , scopes
                                                                                 , "test"
                                                                                 , CancellationToken.None
                                                                                 , new FileDataStore("test")).Result;

    // Translate the Oauth permissions to something the old client libray can read
    OAuth2Parameters parameters = new OAuth2Parameters();
    parameters.AccessToken = credential.Token.AccessToken;
    parameters.RefreshToken = credential.Token.RefreshToken;
    RunContactsSample(parameters);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

I am using my own client id and client secret key. This code is perfectly working when I am running from visual studio, but is not working from after hosted in IIS.

And I mentioned redirected URI in google api console is http://localhost/authorize/

My IIS host Url is http://localhost/googleintegration.aspx

I am facing this issue by last one month, can anyone please give a solution for this..

回答1:

(Reposting from https://github.com/google/google-api-dotnet-client/issues/888)

GoogleWebAuthorizationBroker is for use in an application that is running locally on an end-user machine. It opens a web browser locally allowing the user to authenticate. It sounds like you are trying to use GoogleWebAuthorizationBroker on a server machine, from within IIS; where it won't work, as it will try to open a web browser on that server machine - this will fail, and wouldn't be visible to the end-user anyway as it'll be on the server machine, not their machine.

I've not done this myself, but it looks like the instructions here (for an ASP.NET MVC web app) might help.



回答2:

There are several different types of applications. You can have a web application, an native installed application or a mobile application. The methods used to authenticate these are slightly different.

The method GoogleWebAuthorizationBroker.AuthorizeAsync is used for native installed applications. It will open the browser window on the machine running the code. In the instance where you are running it in Visual studio it works fine but as soon as you try to host it it will try to open the browser window on the web server which wont work.

You need to be using GoogleAuthorizationCodeFlow which was designed for use with web applications. You can find an example here.

private static readonly IAuthorizationCodeFlow flow =
        new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = new ClientSecrets
                {
                    ClientId = "PUT_CLIENT_ID_HERE",
                    ClientSecret = "PUT_CLIENT_SECRET_HERE"
                },
                Scopes = new[] { DriveService.Scope.Drive },
                DataStore = new FileDataStore("Drive.Api.Auth.Store")
            });