c# - GoogleWebAuthorizationBroker.AuthorizeAsync 在本地工作,但在生产 IIS 上挂起

标签 c# adsense-api

我有一个使用 adsense api 的报告应用程序。
我正在使用 GoogleWebAuthorizationBroker.AuthorizeAsync 进行身份验证。
当我在本地使用它时,它工作正常,权限请求窗口打开,在我授予访问权限后一切正常
我的问题是当我将它部署到生产服务器并在 IIS 上运行时,GoogleWebAuthorizationBroker.AuthorizeAsync 永远挂起。
我的猜测是它试图打开服务器上的授权窗口,但无法做到这一点。
我没有编写这个实现,它已经投入生产一段时间了,它曾经工作得很好。
我不确定发生了什么以及是否发生了变化,但现在不起作用。
我四处冲浪并尝试了不同的方法 bot 都没有奏效。
当我尝试使用 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 ),但它没有代码示例,也没有帮助我。

先感谢您。

最佳答案

我们花了几天时间试图弄清楚为什么这个方法在 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

关于c# - GoogleWebAuthorizationBroker.AuthorizeAsync 在本地工作,但在生产 IIS 上挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28498561/

相关文章:

ruby-on-rails - 检索我的 Adsense 报告作为服务器应用程序

adsense - 在 Adsense 中跟踪自定义字段

python - 使用 Python 从服务器访问 Google Adsense API?

html - 广告未显示在我的页面上,我该怎么办?

c# - 使用 java xml api 将 xml 文档保存到文件

C# Unity 3D - 有没有办法制作自定义光线转换形状?

c# - 对象池类中的死锁

c# - 如何转换字符串 "07:35"(HH :MM) to TimeSpan

c# - 根据 DayOfWeek 对字符串数组进行排序

iframe - Google 广告 iframe 被主页阻止