【发布时间】:2013-09-23 01:51:06
【问题描述】:
我正在使用Google APIs for .NET。我正在关注示例项目Simple OAuth2,但我不断收到来自 DotNetOpenAuth 的协议异常。
这是我现在拥有的:
public static void Main( string[] args )
{
// Register the authenticator.
NativeApplicationClient provider = new NativeApplicationClient(
GoogleAuthenticationServer.Description, gaAppId, gaSecret );
OAuth2Authenticator<NativeApplicationClient> auth = new OAuth2Authenticator<NativeApplicationClient>(
provider, GetAuthorization );
AnalyticsService analyticsService =
new AnalyticsService( new BaseClientService.Initializer {
Authenticator = auth,
ApplicationName = @"Test Application",
} );
DataResource.GaResource.GetRequest request = analyticsService.Data.Ga.Get(
gaId, @"2013-09-04", @"2013-09-18", "ga:totalEvents" );
GaData data = request.Execute();
Console.ReadKey();
}
private static IAuthorizationState GetAuthorization( NativeApplicationClient arg )
{
// Get the auth URL:
IAuthorizationState state =
new AuthorizationState( new[] {AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue()} );
state.Callback = new Uri( NativeApplicationClient.OutOfBandCallbackUrl );
// Retrieve the access token by using the authorization code:
return arg.ProcessUserAuthorization( authCode, state );
}
注意事项: 我在这段代码中使用了 Analytics API,因为这正是我所需要的。使用示例中所述的 Tasks API 时,我遇到了同样的错误。
身份验证代码是由示例代码中定义的进程生成的刷新令牌。两种情况都会出现错误(请求新令牌,或重新使用旧令牌。)
DotNetOpenAuth 触发的 ProtocolException 存在是因为 accounts.google.com 返回错误:无效请求。
OAuth 请求如下所示:
Aplication/x-www-form-urlencoded; charset=utf-8
User-Agent: DotNetOpenAuth/4.3.1.13153
Host: accounts.google.com
Cache-Control: no-store,no-cache
Pragma: no-cache
Content-Length: 148
Connection: Keep-Alive
code=REDACTED&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&grant_type=authorization_code
这就是谷歌返回的内容:
HTTP/1.1 400 Bad Request
IRRELEVANT_HEADERS
{
"error" : "invalid_request"
}
我不知道为什么会发生这种情况以及如何解决这个问题。我找不到任何其他可用的 C# 示例。这似乎是与this thread 中的错误不同的一种错误。有没有人知道我该如何解决这个问题?
【问题讨论】:
-
您是否尝试在我们的示例存储库中运行我们的示例,比如说(任务示例 - code.google.com/p/google-api-dotnet-client/source/browse/…)。它应该对你有用,按照自述文件的说明告诉我。工作后,检查工作示例和您的代码之间的差异是什么。祝你好运!
-
@peleyal 我从存储库运行了示例,它们似乎可以工作。我的代码中一定有错误...我注意到该示例不使用离线刷新令牌,并且使用刷新令牌(通过将 access_type=offline&approval_prompt=force 添加到授权请求 url)不起作用.我将仔细查看这些示例,看看是否可以找到差异,或者是否可以找到确实使用了更新令牌的示例。
标签: google-api oauth-2.0 dotnetopenauth google-api-client google-api-dotnet-client