【发布时间】:2012-01-31 22:52:37
【问题描述】:
我正在尝试通过 OAuth 2 使用最新版本的 Report API。目前使用此版本的人似乎并不多,因此很难找到示例。
我有一个刷新令牌,我用它来生成访问令牌。
private AnalyticsService getAnalyticsService()
{
AuthorizationServerDescription description = new AuthorizationServerDescription();
description.TokenEndpoint = new Uri(login.TokenEndpoint);
description.AuthorizationEndpoint = new Uri(login.AuthorizationEndpoint);
WebServerClient client = new WebServerClient(description, login.ClientId, login.ClientSecret);
OAuth2Authenticator<WebServerClient> authenticator = new OAuth2Authenticator<WebServerClient>(client, authenticate);
AnalyticsService service = new AnalyticsService(authenticator);
return service;
}
private IAuthorizationState authenticate(WebServerClient client)
{
string[] scopes = new string[] { login.ScopeUrl }; // not sure if this is necessary
IAuthorizationState state = new AuthorizationState(scopes) { RefreshToken = login.RefreshToken };
client.RefreshToken(state);
return state;
}
这似乎工作得很好:
{
"access_token" : "ya29.AHES6ZQy67SSLHWJWGWcLbLn69yKfq59y6dTHDf4ZoH9vHY",
"token_type" : "Bearer",
"expires_in" : 3600
}
但是,当我提出请求时,我收到了错误消息。例如,这里 是导致错误的查询:
AnalyticsService service = getAnalyticsService();
ManagementResource.ProfilesResource.ListRequest request = service.Management.Profiles.List("~all", "~all");
return request.Fetch();
这是我得到的错误:
{"error":{"errors":[{"domain":"global","reason":"authError","message":"Invalid
Credentials","locationType":"header","location":"Authorization"}],"code":401,"message":"Invalid
Credentials"}}
我尝试了其他查询,提供了有效的个人资料 ID。不过,我是 总是收到 401 错误,说我没有被授权。我有 很难找到人们使用此代码的示例。它可能是 一些简单的东西,比如错误的 URL 或其他东西。不幸的是,我没有 告诉的方式。我可以获得访问令牌似乎很奇怪,但我 似乎无法执行任何查询。
【问题讨论】:
标签: authentication oauth-2.0 google-analytics-api