Google AnalyticsAPI API错误403:“用户无权访问个人资料”

时间:2013-06-05 16:17:07

标签: google-analytics google-api google-analytics-api google-api-dotnet-client

我一直在努力学习如何使用Google AnalyticsAPI来获取网站流量数据。我一直遇到错误,说用户没有访问配置文件的权限。我将服务帐户中的电子邮件地址添加到用户,并尝试将其设置为可以访问所有配置文件的用户和管理员,但都没有工作。

这是我的代码,我是从here获得的:

        string scope = AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue();
        string clientId = "xxxxxxxx@developer.gserviceaccount.com";
        string keyFile = @"C:\Users\emorris\Downloads\xxxxxxxxxx-privatekey.p12";
        string keyPassword = "notasecret";

        AuthorizationServerDescription desc = GoogleAuthenticationServer.Description;

        X509Certificate2 key = new X509Certificate2(keyFile, keyPassword, X509KeyStorageFlags.Exportable);

        AssertionFlowClient client = new AssertionFlowClient(desc, key) { ServiceAccountId= clientId, Scope = scope };

        OAuth2Authenticator<AssertionFlowClient> auth = new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState);

        AnalyticsService gas = new AnalyticsService(new BaseClientService.Initializer() { Authenticator = auth });

        DataResource.GaResource.GetRequest r = gas.Data.Ga.Get("ga:xxxxxx", "2013-05-01", "2013-05-31", "ga:visitors");

        GaData d = r.Fetch();

它在最后一行遇到问题,它给了我:

Google.Apis.Requests.RequestError

用户对此个人资料没有足够的权限。 [403]

我看了here,似乎有类似的问题,但它说的只是在API控制台中添加电子邮件,我做了。有人知道我需要改变什么吗?

1 个答案:

答案 0 :(得分:1)

要确认 - 在原始帖子中,您说您在“API控制台”中添加了电子邮件。但实际上,您需要使用Google Analytics网页界面将电子邮件添加到Google Analytics配置文件中。是你做的吗?仅当您提出请求的用户ID不在分析配置文件中时,才会出现此错误。

相关问题