Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k views
in Technique[技术] by (71.8m points)

c# - GoogleWebAuthorizationBroker is not working from IIS Host

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..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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")
            });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...