我有一个使用adsense api的报表应用程序。我正在使用GoogleWebAuthorizationBroker.AuthorizeAsync进行身份验证。当我在本地运行时,它运行良好,权限请求窗口打开,在我授予访问权限后,一切都正常我的问题是,当我将其部署到生产服务器并在IIS上运行时,GoogleWebAuthorizationBroker.AuthorizeAsync将永远挂起。我的猜测是,它试图打开服务器上的授权窗口,但无法做到这一点。我没有写这个实现,它已经在生产中有一段时间了,它过去工作得很好。我不确定发生了什么,也不确定是否有什么变化,但现在不起作用了。我四处冲浪,尝试了各种各样的按摩,但都不起作用。当我尝试在AccessType设置为"offline"并且提供了URI的情况下使用GoogleAuthorizationCodeFlow时,它仍然不起作用。我也尝试过使用服务帐户,但后来我了解到他们不支持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 API GoogleWebAuthorizationBroker.AuthorizeAsync在本地工作,但没有部署到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;
}
}
}
}
}
该代码将提示您登录谷歌,但登录后,您最初应该会收到重定向错误。您需要在配置项目的重定向URLhttp://Console.developers.google.com.
Url重定向设置位于API&验证>凭据。您需要单击"编辑设置"按钮,并指定以下适用于您的域名和端口号的授权重定向URI:
http://localhost:1234/Default.aspxhttp://localhost:1234/Options.aspx