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錯誤,說我沒有被授權。我有 查找人們使用此代碼的示例。它可能是 這類東西很簡單,就像一個不好的網址或其他東西。不幸的是,我沒有 的方式來告訴。看起來很奇怪,我可以獲得訪問令牌,但我似乎無法執行任何查詢。