我正在尝试使用 C# 从 Google Analytics 下载指标数据,并正在使用 OAuth 2.0 执行用户身份验证。我正在使用已安装的应用程序授权流程,这需要登录 Google 并将代码复制并粘贴到应用程序中。我正在关注从google-api-dotnet-client获取的代码:
private void DownloadData()
{
Service = new AnalyticsService(new BaseClientService.Initializer() {
Authenticator = CreateAuthenticator(),
});
var request = service.Data.Ga.Get(AccountID, StartDate, EndDate, Metrics);
request.Dimensions = Dimensions;
request.StartIndex = 1;
request.MaxResults = 10000;
var response = request.Execute(); // throws Google.GoogleApiException
}
private IAuthenticator CreateAuthenticator()
{
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description) {
ClientIdentifier = "123456789012.apps.googleusercontent.com",
ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxx",
};
return new OAuth2Authenticator<NativeApplicationClient>(provider, Login);
}
private static IAuthorizationState Login(NativeApplicationClient arg)
{
// Generate the authorization URL.
IAuthorizationState state = new AuthorizationState(new[] { AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue() });
state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
Uri authUri = arg.RequestUserAuthorization(state);
// Request authorization from the user by opening a browser window.
Process.Start(authUri.ToString());
Console.Write("Google Authorization Code: ");
string authCode = Console.ReadLine();
// Retrieve the access token by using the authorization code.
state = arg.ProcessUserAuthorization(authCode, state);
return state;
}
Google 帐户xxxxxx@gmail.com
注册了客户端 ID 和密码。同一帐户在 Google Analytics 中拥有完全的管理权限。当我尝试从 Google Analytics 中提取数据时,它会通过授权过程,这似乎可以正常工作。然后它失败了:
Google.GoogleApiException
Google.Apis.Requests.RequestError
用户对此配置文件没有足够的权限。[403]
错误 [
消息 [用户对此配置文件没有足够的权限。] 位置 [ - ] 原因 [insufficientPermissions] 域 [全局]
]
我已经为此苦苦挣扎了几个小时。我已经仔细检查了是否使用了正确的用户,并且在 Google Analytics 上获得了授权。我不知道什么配置错误。关于需要配置或更改什么的任何想法?