Authentication Can';t使用OAuth 2查询谷歌分析报告API

Authentication Can';t使用OAuth 2查询谷歌分析报告API,authentication,oauth-2.0,google-analytics-api,Authentication,Oauth 2.0,Google Analytics Api,我试图使用OAuth 2使用最新版本的报表API。似乎还没有很多人使用这个版本,所以很难找到例子 我有一个刷新令牌,用于生成访问令牌 private AnalyticsService getAnalyticsService() { AuthorizationServerDescription description = new AuthorizationServerDescription(); description.TokenEndpoint = new Uri(login.T

我试图使用OAuth 2使用最新版本的报表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;
}
然而,当我做一个请求时,我得到了一个错误。比如说这里, 是导致错误的查询:

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或者别的什么。不幸的是,我没有 告诉你的方式。我能得到一个访问令牌似乎很奇怪,但我
似乎无法执行任何查询。

对于OAuth 2,作用域从:

致:

您正在获取身份验证错误,因为您试图在没有正确作用域的情况下获取访问权限

快速简单的修复

{"error":{"errors":[{"domain":"global","reason":"authError","message":"Invalid
Credentials","locationType":"header","location":"Authorization"}],"code":401,"message":"Invalid
Credentials"}}