【问题标题】:GoogleWebAuthorizationBroker is not working from IIS HostGoogleWebAuthorizationBroker 在 IIS 主机上不起作用
【发布时间】:2017-03-16 13:47:07
【问题描述】:

我正在使用以下代码通过 Google .Net 客户端库向 Google 进行身份验证。

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

我正在使用我自己的客户端 ID 和客户端密钥。 当我从 Visual Studio 运行时,此代码完美运行,但在 IIS 中托管后无法运行。

我提到在 google api 控制台中重定向的 URI 是 http://localhost/authorize/

我的 IIS 主机 URL 是 http://localhost/googleintegration.aspx

我在上个月遇到了这个问题,任何人都可以为此提供解决方案..

【问题讨论】:

标签: c# asp.net iis google-oauth google-contacts-api


【解决方案1】:

有几种不同类型的应用程序。您可以拥有 Web 应用程序、本地安装的应用程序或移动应用程序。用于验证这些的方法略有不同。

GoogleWebAuthorizationBroker.AuthorizeAsync 方法用于本机安装的应用程序。它将在运行代码的机器上打开浏览器窗口。在您在 Visual Studio 中运行它的情况下,它可以正常工作,但是一旦您尝试托管它,它就会尝试在 Web 服务器上打开浏览器窗口,但无法正常工作。

您需要使用专为与网络应用程序一起使用而设计的 GoogleAuthorizationCodeFlow。你可以找到一个例子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")
            });

更新 asp .net core 3.1 和 .net 5

我还有一个教程和视频,展示了如何使用 Asp .net Get profile information 进行此操作

【讨论】:

    【解决方案2】:

    (转自https://github.com/google/google-api-dotnet-client/issues/888

    GoogleWebAuthorizationBroker 用于在最终用户计算机上本地运行的应用程序。它会在本地打开一个允许用户进行身份验证的网络浏览器。 听起来您正试图在 IIS 内的服务器机器上使用 GoogleWebAuthorizationBroker;它不起作用的地方,因为它会尝试在该 server 机器上打开一个网络浏览器 - 这将失败,并且最终用户无论如何都看不到它,因为它会打开服务器机器,而不是他们的机器。

    我自己没有这样做,但看起来instructions here(对于 ASP.NET MVC Web 应用程序)可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-25
      • 2015-02-25
      • 2011-02-15
      • 2015-03-19
      相关资源
      最近更新 更多