Google 分析数据 API 尚未在项目 12345 中使用,但我无权访问此项目

Google Analytics Data API has not been used in project 12345, but I have no access to this project

我是运行这个代码:

var auth = "Bearer " + token;
var myHeaders = {
  "Authorization": auth
}

var rawdata = JSON.stringify({
  "dimensions": [
    {
      "name": "appVersion"
    },
    {
      "name": "eventName"
    }
  ],
  "metrics": [
    {
      "name": "eventCount"
    }
  ],
  "dateRanges": [
    {
      "startDate": "30daysAgo",
      "endDate": "yesterday"
    }
  ],
  "orderBys": [
    {
      "dimension": {
        "orderType": "ALPHANUMERIC",
        "dimensionName": "appVersion"
      }
    },
    {
      "dimension": {
        "orderType": "ALPHANUMERIC",
        "dimensionName": "eventName"
      }
    }
  ],
  "metricAggregations": [
    "TOTAL"
  ]
});

var requestOptions = {
  'contentType': 'application/json',
  'headers': myHeaders,
  'method' : 'post',
  'payload' : rawdata, 
  'followRedirects': true,
   'muteHttpExceptions' : true
};

  var response = UrlFetchApp.fetch(url, requestOptions);
    Logger.log(`response is ${response}`)
   var result = JSON.parse(response.getContentText());
  var dataSet = result;
Once I had created a trigger, we saw 100% failure rate.

response is {
  "error": {
    "code": 403,
    "message": "Request had insufficient authentication scopes.",
    "status": "PERMISSION_DENIED"
  }
}

并得到这个错误:

  "error": {
    "code": 403,
    "message": "Google Analytics Data API has not been used in project 548594456844 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/analyticsdata.googleapis.com/overview?project=548594456844 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
    "status": "PERMISSION_DENIED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help",
        "links": [
          {
            "description": "Google developers console API activation",
            "url": "https://console.developers.google.com/apis/api/analyticsdata.googleapis.com/overview?project=548594456844"
          }
        ]
      },
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "SERVICE_DISABLED",
        "domain": "googleapis.com",
        "metadata": {
          "consumer": "projects/548594456844",
          "service": "analyticsdata.googleapis.com"
        }
      }
    ]
  }
}

但我无权访问此 GCP 项目。 这是否意味着 GCP 必须调用 GA4 API?

如果我无权访问此 GCp 项目(apps-script 的默认项目)- 那么我该如何修复它?

Google 项目是一种通用类型,存在于例如Google Cloud、Firebase 和其他 Google 喜欢通过他们的(较小|有限的)世界观呈现 Google 的“产品领域”。

NOTE In the old days, IIRC, these were all called API projects or similar. Sometimes it's possible to browse (e.g. a GCP project) from (e.g. a Firebase) project but there's project metadata that tends to filter projects to the Consoles from which they were created.

推论:当您创建一个 Google Cloud Platform 项目时,您实际上是在创建一个通用的 Google API 项目,它可能有一些额外的元数据将项目显示为 GCP。

一个Google项目是一组GoogleAPI、一组凭据(用户|服务)帐户和一个计费帐户的交集。

在这种情况下,您需要在项目中启用 Google Analytics Data API,然后才能使用该项目拥有的服务帐户调用 API。

这不仅为 Google 的 API 的使用提供了安全性,还提供了计费范围。在这种情况下,Google Analytics API 似乎不会产生账单费用,但许多其他 Google API 会产生账单费用。

如果您无法访问该项目,并且没有访问权限的人愿意为您启用该项目,您将无法使用该服务帐户访问 Google Analytics。您可能想要创建另一个项目和一个新的服务帐户并使用它。我认为(!?)您不需要启用计费即可使用 Google 分析数据 API,因此您 不应该 需要提供例如一张信用卡。

NOTE Don't forget to add the Service Account's email addresss (${ACCOUNT}@${PROJECT}.iam.gserviceaccount.com) to your Google Analytics properties' (as many as you want to use) auth too.