Google Analytics(自定义维度更新)权限不足403

时间:2018-05-30 02:48:24

标签: c# google-analytics google-oauth

我正在尝试通过调用C#中的analytics api来更新Google Analytics中的自定义维度字段(https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/customDimensions/update)。

我在https://console.developers.google.com创建了一个项目,添加了一个服务帐户(下载了.p12,私钥文件),启用了分析API并在https://analytics.google.com中链接了服务帐户电子邮件

我能够阅读“分析数据”(如帐户摘要等),但不能插入或更新。当我尝试这样做时,我得到了Insufficient权限403错误。添加到Google Analytics的服务帐户具有所有权限。

 class Program
    {
        static void Main(string[] args)
        {
            test();
        }

        public static void test() //takes clientid as input
        {

            string[] scopes = new string[] { AnalyticsService.Scope.Analytics }; // view and manage your Google Analytics data

            var keyFilePath = @"C:\Users\xyz\Desktop\CustomDimUpdate\xxxxxxxx.p12";    // Downloaded from https://console.developers.google.com
            var serviceAccountEmail = "xxxx.iam.gserviceaccount.com";  // found https://console.developers.google.com

            //loading the Key file
            var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
            var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
            {
                Scopes = scopes
            }.FromCertificate(certificate));


            var service = new AnalyticsService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential
                //ApplicationName = "Analytics API Sample",
            });


            CustomDimension body = new CustomDimension();
            body.Name = "Configurable"; //Found in https://analytics.google.com
            body.Scope = "Product"; //Found in https://analytics.google.com
            body.Active = true;


            try
            {
                //analytics.management().customDimensions()
                //    .update("123456", "UA-123456-1", "ga:dimension2", body).execute();
                ManagementResource.CustomDimensionsResource.UpdateRequest update = service.Management.CustomDimensions.Update(body, "123456", "UA-123456-1", "ga:dimension1");

                update.Execute(); //Errors out here

                ManagementResource.AccountsResource.ListRequest list = service.Management.Accounts.List();
                list.MaxResults = 1000; // Maximum number of Accounts to return, per request. 
                Accounts feed1 = list.Execute(); //Works perfectly fine

                foreach (Account account in feed1.Items)
                {
                    // Account
                    Console.WriteLine(string.Format("Account: {0}({1})", account.Name, account.Id));
                }

                ManagementResource.ProfilesResource.ListRequest list1 = service.Management.Profiles.List("123456", "UA-123456-1");
                Profiles feed = list1.Execute();
                foreach (Profile profile in feed.Items)
                {
                    Console.WriteLine(string.Format("\t\tProfile: {0}({1})", profile.Name, profile.Id));
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

            }


        }

1 个答案:

答案 0 :(得分:1)

我可以通过更改下面的代码行来解决这个问题

string[] scopes = new string[] { AnalyticsService.Scope.Analytics }; // view and manage your Google Analytics data

string[] scopes = new string[] { AnalyticsService.Scope.AnalyticsEdit }; // view and manage your Google Analytics data