问题描述
我正在尝试使用 C# 从 Google Analytics 下载指标数据,并且正在使用 OAuth 2.0 执行用户身份验证.我正在使用已安装的应用程序 授权流程,这需要登录 Google 并将代码复制并粘贴到应用程序中.我正在关注取自 google-api-dotnet-client 的代码:
I am attempting to download metric data from Google Analytics using C# and am performing user authentication with OAuth 2.0. I'm using the Installed Application authorisation flow, which requires logging into Google and copy-and-pasting a code into the application. I'm following the code taken from 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 中提取数据时,它会通过授权过程,这似乎可以正常工作.然后它失败了:
The Google account xxxxxx@gmail.com
registered the Client ID and secret. The same account has full administration rights in Google Analytics. When I try to pull data from Google Analytics, it goes through the authorisation process, which appears to work properly. Then it fails with:
Google.GoogleApiException
Google.Apis.Requests.RequestError
用户对此配置文件没有足够的权限.[403]
错误 [
消息[用户对此配置文件没有足够的权限.] 位置[ - ] 原因 [insufficientPermissions] 域[全局]
]
我已经为此苦苦挣扎了几个小时.我已经仔细检查过是否使用了正确的用户,并且在 Google Analytics 上获得了授权.我对什么配置错误一无所知.关于需要配置或更改什么的任何想法?
I've been struggling with this for a few hours. I've double checked that the correct user is being used, and is authorised on Google Analytics. I'm at a loss as to what is misconfigured. Any ideas as to what requires configuring or changing?
推荐答案
如果 auth 似乎可以正常工作,那么我的建议是您确保提供正确的 ID,因为基于您的代码片段:
If auth seems to be working working then my suggestion is that you make sure you're providing the correct ID because based on your code snippet:
var request = service.Data.Ga.Get(AccountID, StartDate, EndDate, Metrics);
只能假设您使用的是帐户 ID.如果是这样,那是不正确的,您会收到您遇到的错误.您需要使用配置文件 ID 进行查询.
one can only assume that you're using the Account ID. If so, that is incorrect and you'd receive the error you've encountered. You need to query with the Profile ID.
如果您使用网络界面登录 Google Analytics,您将在浏览器地址栏的 URL 中看到以下模式:
If you login to Google Analytics using the web interface you'll see the following pattern in URL of the browser's address bar:
/a12345w654321p9876543/
p 后面的数字是配置文件 ID,因此在上面的示例中为 9876543.确保您正在使用它,实际上您应该使用 表id 即 ga:9876543.
The number following the p is the profile ID, so 9876543 in the example above. Make sure you're using that and actually you should be using the table id which would be ga:9876543.
如果不是 ID 问题,请查询管理 API 列出帐户并查看您有权访问的内容并验证身份验证是否正常工作.
If it isn't an ID issue then instead query the Management API to list accounts and see what you have access to and to verify auth is working correctly.
这篇关于谷歌分析抛出 403 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!