GoogleWebAuthorizationBroker.AuthorizeAsync在本地运行,但在生产IIS上挂起

时间:2015-02-13 11:27:24

标签: adsense-api

我有一个使用adsense api的报告应用程序。 我使用GoogleWebAuthorizationBroker.AuthorizeAsync进行身份验证。 当我在本地运行它工作正常,权限窗口的请求打开,在我授予访问权限之后一切正常 我的问题是当我将它部署到生产服务器并在IIS上运行时,GoogleWebAuthorizationBroker.AuthorizeAsync将永久挂起。 我的猜测是它试图打开服务器上的授权窗口而无法做到这一点。 我没有写这个实现,它已经生产了一段时间,它曾经工作正常。 我不确定发生了什么,如果改变了什么,但它现在不起作用。 我四处冲浪,尝试了不同的aproaches机器人没有工作。 当我尝试使用GoogleAuthorizationCodeFlow并将AccessType设置为“离线”并使用提供的URI时,它仍然无效。 我也尝试过使用服务帐户,但后来才知道他们没有被adsense支持。 以下是codesample

    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),但它没有代码示例,也没有帮助我。

提前谢谢。

1 个答案:

答案 0 :(得分: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为您的项目配置重定向网址。

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

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