5

我有一个使用 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 在本地工作,但没有部署到 IIS),但它没有代码示例,也没有帮助我。

先感谢您。

4

1 回答 1

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

于 2015-06-08T16:28:45.463 回答