2012-01-31 49 views
2

我试图使用最新版本的报告API使用OAuth 2.它似乎并没有很多人使用这个版本,所以它一直在真的很难找到例子。无法查询Google Analytics报告API使用OAuth 2

我有一个刷新令牌,我用它来生成访问令牌。

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错误,说我没有被授权。我有 查找人们使用此代码的示例。它可能是 这类东西很简单,就像一个不好的网址或其他东西。不幸的是,我没有 的方式来告诉。看起来很奇怪,我可以获得访问令牌,但我似乎无法执行任何查询。

回答

相关问题