【问题标题】:GoogleWebAuthorizationBroker.AuthorizeAsync works locally but hangs on production IISGoogleWebAuthorizationBroker.AuthorizeAsync 在本地工作,但在生产 IIS 上挂起
【发布时间】:2015-02-13 11:27:24
【问题描述】:

我有一个使用 adsense api 的报表应用程序。 我正在使用 GoogleWebAuthorizationBroker.AuthorizeAsync 进行身份验证。 当我在本地运行它时,它工作正常,权限请求窗口打开,在我授予访问权限后一切正常 我的问题是当我将它部署到生产服务器并在 IIS 上运行时,GoogleWebAuthorizationBroker.AuthorizeAsync 会永远挂起。 我的猜测是它试图打开服务器上的授权窗口并且无法做到这一点。 我没有编写这个实现,它已经投入生产一段时间了,它曾经工作得很好。 我不确定发生了什么以及是否发生了某些变化,但它现在不起作用。 我四处冲浪并尝试了不同的方法,机器人都没有奏效。 当我尝试使用 GoogleAuthorizationCodeFlow 并将 AccessType 设置为“离线”并使用提供的 URI 时,它仍然无法正常工作。 我也尝试使用服务帐户,但后来我了解到它们不支持 adsense。 下面是代码示例

    var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                new ClientSecrets
                {
                    ClientId = ConfigurationManager.AppSettings["AdSenseClientId"],
                    ClientSecret = ConfigurationManager.AppSettings["AdSenseClientSecret"]
                },
                new string[] { AdSenseService.Scope.Adsense },
                "xxxxxxxx@gmail.com",
                CancellationToken.None).Result;
            // Create the service.
            adSenseService = new AdSenseService(new BaseClientService.Initializer
            {
                HttpClientInitializer = credential,
                ApplicationName = "My API Project"
            });
    var adClientRequest = adSenseService.Adclients.List();
        var adClientResponse = adClientRequest.Execute();

如果有一个示例代码可以解决这个问题,我会非常高兴。 我看到了这篇文章 (ASP.NET MVC5 Google APIs GoogleWebAuthorizationBroker.AuthorizeAsync works locally but not deployed to IIS),但它没有代码示例,对我没有帮助。

提前谢谢你。

【问题讨论】:

  • 你找到解决方案了吗?
  • 也很想听听您是否知道这一点

标签: c# adsense-api


【解决方案1】:

我们花了几天时间试图弄清楚为什么这个方法在 IIS 中挂起并且在 Visual Studio Dev Server 中运行良好。最后我们使用了另一种似乎可以完成这项工作的方法,希望这可以节省您一些时间。

以下代码返回所有上传的视频:

using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.Auth.OAuth2.Web;
using Google.Apis.Services;
using Google.Apis.YouTube.v3.Data;
using System.Threading;

private void GetData()
{
    IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(
        new GoogleAuthorizationCodeFlow.Initializer
        {
            ClientSecrets = new ClientSecrets { ClientId = "[ClientID]", ClientSecret = "[ClientSecret]" },
            DataStore = new FileDataStore("C:\\Temp", true),
            Scopes = new[] { YouTubeService.Scope.YoutubeReadonly }
        });
    var userId = "[ACCOUNTNAME]";
    var uri = Request.Url.ToString();
    var code = Request["code"];
    if (code != null)
    {
        var token = flow.ExchangeCodeForTokenAsync(userId, code, uri.Substring(0, uri.IndexOf("?")), CancellationToken.None).Result;

        // Extract the right state.
        var oauthState = AuthWebUtility.ExtracRedirectFromState(flow.DataStore, userId, Request["state"]).Result;
        Response.Redirect(oauthState);
    }
    else
    {
        var result = new AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync(userId, CancellationToken.None).Result;

        if (result.RedirectUri != null)
        {
            // Redirect the user to the authorization server.
            Response.Redirect(result.RedirectUri);
        }
        else
        {
            // The data store contains the user credential, so the user has been already authenticated.
            var youtubeService = new YouTubeService(new BaseClientService.Initializer
            {
                ApplicationName = "MyVideoApp",
                HttpClientInitializer = result.Credential
            });

            if (youtubeService != null)
            {
                var channelsListRequest = youtubeService.Channels.List("contentDetails");
                channelsListRequest.Mine = true;
                ChannelListResponse channelsListResponse = channelsListRequest.ExecuteAsync().Result;
                foreach (var channel in channelsListResponse.Items)
                {
                    var playlistItemsListRequest = youtubeService.PlaylistItems.List("snippet, status");
                    playlistItemsListRequest.PlaylistId = channel.ContentDetails.RelatedPlaylists.Uploads;
                    var playlistItemsListResponse = playlistItemsListRequest.ExecuteAsync().Result;
                }
            }
        }
    }
}

代码会提示您登录 Google,但在登录后,您最初应该会收到重定向错误。您需要在 http://Console.developers.google.com 为您的项目配置重定向 URL。

Url 重定向设置位于 APIs & Auth > Credentials 下。您需要单击“编辑设置”按钮并指定以下适用于您的域名和端口号的授权重定向 URI:

http://localhost:1234/Default.aspx http://localhost:1234/Options.aspx

【讨论】:

  • [AccountName] 中应该包含什么内容?
  • 我认为放在那里的内容并不重要,“Response.Redirect(oauthState)”行应该将您重定向到您必须登录的 Google 登录页面。
  • 我在回答中添加了更多细节,看看吧。
  • 能否请您告诉我如何进行离线访问,我目前正在这样做flow.RefreshTokenAsync 但它给了我unauthorized_client
猜你喜欢
  • 1970-01-01
  • 2015-06-19
  • 2011-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-29
  • 2015-02-18
  • 1970-01-01
相关资源
最近更新 更多