首页 > 解决方案 > 公式在 Google App 脚本控制台中有效,但在电子表格中返回身份验证错误

问题描述

我为我的一个电子表格创建了一个自定义公式。它获取两个 Date 参数并返回特定 Google Analytics 事件的 Total Unique Event 计数。当我测试我的代码时,它可以在 Google Apps 脚本中完美运行。但是当我尝试在关联的电子表格中使用相同的公式时,会导致以下错误:

GoogleJsonResponseException:对 analytics.data.ga.get 的 API 调用失败并出现错误:请求缺少所需的身份验证凭据。预期的 OAuth 2 访问令牌、登录 cookie 或其他有效的身份验证凭据。请参阅https://developers.google.com/identity/sign-in/web/devconsole-project。(第 13 行)。

这是我的代码:

var tableId = 'ga:78467590';

function test(){
  USE_CLICK_COUNT("2020-08-31", "2020-09-06");
}

function USE_CLICK_COUNT(startDate, endDate){ 
  var optArgs = {
    'dimensions':'ga:eventLabel',
    'filters':'ga:eventLabel=~^UseClick'
  };
  
  var results = Analytics.Data.Ga.get(tableId, startDate, endDate, 'ga:uniqueEvents', optArgs);
  Logger.log(results.getTotalsForAllResults());
  return results.getTotalsForAllResults();

}

当我运行该test()函数时,它会返回并记录{ga:uniqueEvents=13}- 这是我正在寻找的数字。

我一遍又一遍地检查 API 和配置详细信息,但无法弄清楚我缺少什么。请看附件。

脚本的 Google Services 设置,1

脚本的 Google Services 设置,2

脚本中的 Google Cloud 项目关联

项目范围

证书

标签: google-apps-scriptgoogle-sheetsgoogle-cloud-platformgoogle-analyticsgoogle-api

解决方案


问题和解决方法:

我以为您的情况与此线程相同。不幸的是,在现阶段,高级谷歌服务的谷歌APIs不能直接与自定义函数一起使用。因为无法在自定义函数中检索访问令牌。这似乎是当前的规范。因此,作为实现目标的解决方法,它使用由 Google Apps 脚本创建的 Web 应用程序。

但我认为你的问题的脚本与这个线程有点不同。因此,当提出示例脚本时,它可能对您有用。因此,我想介绍实现您目标的示例脚本。

1. 准备脚本。

请将以下脚本复制并粘贴到脚本编辑器并保存。

示例脚本:
// This is your script.
var tableId = 'ga:78467590';

function test(){
  USE_CLICK_COUNT("2020-08-31", "2020-09-06");
}

function USE_CLICK_COUNT(startDate, endDate){ 
  var optArgs = {
    'dimensions':'ga:eventLabel',
    'filters':'ga:eventLabel=~^UseClick'
  };
  var results = Analytics.Data.Ga.get(tableId, startDate, endDate, 'ga:uniqueEvents', optArgs);
  Logger.log(results.getTotalsForAllResults());
  return results.getTotalsForAllResults();
}

// I added below script.
const key = "samplekey"; // This is used as the key for running the Web Apps.

// Web Apps using as the wrapper.
function doGet(e) {
  if (e.parameter.key === key) {
    const res = USE_CLICK_COUNT(e.parameter.startDate, e.parameter.endDate);
    return ContentService.createTextOutput(JSON.stringify(res));
  }
  return ContentService.createTextOutput("Error.");
}

// This is used as the custom function.
function RUN_USE_CLICK_COUNT(startDate, endDate) {
  const url = `https://script.google.com/macros/s/###/exec?startDate=${startDate}&endDate=${endDate}&key=${key}`;  // Please set the URL of Web Apps after you set the Web Apps.
  return UrlFetchApp.fetch(url).getContentText();
}

2. 部署 Web 应用程序。

  1. 在脚本编辑器上,通过“发布”->“部署为 Web 应用程序”打开一个对话框。

  2. “将应用程序执行为:”选择“我” 。

    • 这样,脚本作为所有者运行。
  3. “谁有权访问应用程序:”选择“任何人,甚至匿名” 。

    • 在这种情况下,不需要请求访问令牌。我认为我建议使用此设置来测试此解决方法。
    • 当然,您也可以使用访问令牌。但是,在这种情况下,当使用访问令牌时,此示例脚本不能直接用作自定义函数。所以在这个答案中,我建议使用密钥来运行 Web Apps 脚本。但是如果你想使用访问令牌,我认为它会使用PropertiesService来实现。
  4. 单击“部署”按钮作为新的“项目版本”。

  5. 自动打开“需要授权”对话框。

    1. 单击“查看权限”。
    2. 选择自己的帐户。
    3. 在“此应用未验证”中单击“高级”。
    4. 点击“转到###项目名称###(不安全)”
    5. 单击“允许”按钮。
  6. 单击“确定”。

  7. 复制 Web 应用程序的 URL。就像https://script.google.com/macros/s/###/exec

    • 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。
  8. 请设置上述脚本的https://script.google.com/macros/s/###/execURL url。请重新部署 Web 应用程序。这样,最新的脚本就会反映到 Web 应用程序中。所以请注意这一点。

4. 测试此解决方法。

请放到=RUN_USE_CLICK_COUNT("2020-08-31", "2020-09-06")牢房里。这样, 的值"2020-08-31", "2020-09-06"将发送到 Web 应用程序并USE_CLICK_COUNT返回值 from 。

笔记:

  • 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。

参考:


推荐阅读