C# GoogleWebAuthorizationBroker.AuthorizationAsync在本地工作,但在生产IIS上挂起

C# GoogleWebAuthorizationBroker.AuthorizationAsync在本地工作,但在生产IIS上挂起,c#,adsense-api,C#,Adsense Api,我有一个使用adsense api的报表应用程序。 我正在使用GoogleWebAuthorizationBroker.AuthorizationAsync进行身份验证。 当我在本地调用它时,它工作正常,“请求权限”窗口打开,在我授予访问权限后,一切正常 我的问题是,当我将其部署到生产服务器并在IIS上运行时,GoogleWebAuthorizationBroker.AuthorizationAsync将永远挂起。 我的猜测是,它试图打开服务器上的授权窗口,但无法做到这一点。 我没有编写这个实现

我有一个使用adsense api的报表应用程序。 我正在使用GoogleWebAuthorizationBroker.AuthorizationAsync进行身份验证。 当我在本地调用它时,它工作正常,“请求权限”窗口打开,在我授予访问权限后,一切正常 我的问题是,当我将其部署到生产服务器并在IIS上运行时,GoogleWebAuthorizationBroker.AuthorizationAsync将永远挂起。 我的猜测是,它试图打开服务器上的授权窗口,但无法做到这一点。 我没有编写这个实现,它已经在生产中有一段时间了,过去工作得很好。 我不知道发生了什么,也不知道是否有什么变化,但现在不行了。 我四处冲浪,尝试不同的方法,但都不管用。 当我尝试使用GoogleAuthorizationCodeFlow时,AccessType设置为“offline”,并且使用提供的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();
如果有一个解决这个问题的示例代码,我会非常感激。 我看到了这篇文章(),但它没有代码示例,对我也没有帮助


提前感谢您。

我们花了几天时间试图弄清楚为什么此方法挂起在IIS中,并在Visual Studio开发服务器中运行良好。最后,我们使用了另一种方法来完成这项工作,希望这能为您节省一些时间

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

使用Google.api.Auth.OAuth2.Flows;
使用Google.api.Auth.OAuth2;
使用Google.api.Util.Store;
使用Google.api.YouTube.v3;
使用Google.api.Auth.OAuth2.Web;
使用Google.api.Services;
使用Google.api.YouTube.v3.Data;
使用系统线程;
私有void GetData()
{
IAAuthorizationCodeFlow=新的GoogleAuthorizationCodeFlow(
新的GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets=new ClientSecrets{ClientId=“[ClientId]”,ClientSecret=“[ClientSecret]”,
数据存储=新文件数据存储(“C:\\Temp”,true),
Scopes=new[]{YouTubeService.Scope.YoutubeReadonly}
});
var userId=“[ACCOUNTNAME]”;
var uri=Request.Url.ToString();
var代码=请求[“代码”];
如果(代码!=null)
{
var-token=flow.ExchangeCodeForTokenAsync(用户ID、代码、uri.Substring(0、uri.IndexOf(“?”),CancellationToken.None)。结果;
//提取正确的状态。
var oauthState=AuthWebUtility.ExtracRedirectFromState(flow.DataStore,userId,Request[“state]”)。结果;
重定向(oauthState);
}
其他的
{
var result=new AuthorizationCodeWebApp(流、uri、uri).authorizationAsync(userId、CancellationToken.None).result;
if(result.RedirectUri!=null)
{
//将用户重定向到授权服务器。
Response.Redirect(result.RedirectUri);
}
其他的
{
//数据存储包含用户凭据,因此用户已通过身份验证。
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通道响应项)
{
var playlitemslistrequest=youtubeService.playlitems.List(“代码段,状态”);
PlayItemsListRequest.PlayId=channel.ContentDetails.RelatedPlayList.Uploads;
var playlItemsListResponse=playlItemsListRequest.ExecuteAsync().Result;
}
}
}
}
}
该代码将提示您登录Google,但在登录后,您最初会收到一个重定向错误。您需要在为项目配置重定向URL

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


你有没有找到解决方案?也很想听听你是否知道[AccountName]中应该有什么?我认为你放在那里的内容并不重要,“Response.Redirect(oauthState)”行应该将你重定向到谷歌登录页面,在那里你必须登录。我在回答中添加了更多细节,看一看。您能告诉我如何进行电话访问吗?我目前正在做这个
flow.RefreshTokenAsync
,但它给了我未经授权的客户端