google-apps-script - 尝试从应用程序脚本获取谷歌分析 4 数据时出现 403 错误

标签 google-apps-script google-api google-oauth google-analytics-4 google-analytics-data-api

我正在尝试将自定义社区连接器连接到 google analytics 4,以便从分析中获取数据并能够在应用脚本中对其进行修改,然后将其发送到数据工作室。但是,我在连接 Google Analytics 和从中检索数据时遇到困难。

错误:

    { error: 
   { code: 403,
     message: 'Google Analytics Data API has not been used in project 640397821842 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/analyticsdata.googleapis.com/overview?project=640397821842 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: [ [Object], [Object] ] } }

我已经设置了范围

 "oauthScopes": [
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/analytics.readonly"
  ],

我要测试的代码:

function testFetch(){
  const options = {
  entry: { propertyId: 263290444},
  "dateRanges": [{ "startDate": "2020-12-01", "endDate": "2021-03-01" }],
  "dimensions": [{ "name": "country" }],
  "metrics": [{ "name": "activeUsers" }],
  // etc.
  }
  var response = UrlFetchApp.fetch(
    'https://analyticsdata.googleapis.com/v1alpha:runReport', {
    method: 'POST',
    muteHttpExceptions: true,
    headers: {
      Authorization : `Bearer ${ScriptApp.getOAuthToken()}`
    },
    contentType: 'application/json; charset=utf-8',
    payload: JSON.stringify(options)
  });
  var result = JSON.parse(response.getContentText());
  console.log(result);

最佳答案

'Google Analytics Data API has not been used in project 640397821842 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/analyticsdata.googleapis.com/overview?project=640397821842 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.', status:

您遇到的问题是您尚未在 Google 开发人员控制台中启用该 API 您需要点击错误消息中的链接,然后转到库并查找 Google 分析数据 API 并启用它。

启用它后,尝试再次运行您的应用程序。

enter image description here

关于google-apps-script - 尝试从应用程序脚本获取谷歌分析 4 数据时出现 403 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66496448/

相关文章:

c# - 使用 Json 文件的 Google 服务帐户身份验证

javascript - gapi 的身份验证弹出窗口立即关闭并失败并出现 401 错误

javascript - Google Signin 无法点击并且仅在移动设备上卡住登录

oauth - 对Google OAuth 2.0使用refresh_token返回HTTP 400错误请求

reactjs - 提交表单(ReactJS)到 Google Spreadsheet - 更改消息验证

javascript - Google 表格中的日期/时间未将正确的时间传输到 Google 日历中

javascript - Google Apps 脚本正则表达式匹配第 n 次出现

javascript - Google api - 防止标记在缩放时改变大小

javascript - 无法将 POST 请求发送到 Google Apps 脚本

ios - 在 Google Maps API 上批量处理多个反向地理编码请求