【问题标题】:Google Analytics(Custom Dimension update) Insufficient Permission 403Google Analytics(自定义维度更新)权限不足 403
【发布时间】:2018-11-08 18:39:46
【问题描述】:

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

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

我能够读取“分析数据”(如帐户摘要等),但不能插入或更新。当我尝试这样做时,出现权限不足 403 错误。添加到谷歌分析的服务帐号拥有所有权限。

 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);

            }


        }

【问题讨论】:

    标签: c# google-analytics google-oauth


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-01
      • 2018-10-29
      • 2021-07-11
      • 1970-01-01
      • 2015-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多