Formula可在Google App脚本控制台中运行,但会在电子表格中返回身份验证错误 问题和解决方法: 1准备脚本 2部署Web应用 4测试此替代方法注意:参考文献:

如何解决Formula可在Google App脚本控制台中运行,但会在电子表格中返回身份验证错误 问题和解决方法: 1准备脚本 2部署Web应用 4测试此替代方法注意:参考文献:

我为自己的一个电子表格创建了一个自定义公式。它获取两个Date参数,并返回特定Google Analytics(分析)事件的总唯一事件总数。当我测试代码时,它可以完美地在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 settings of the script,1

Google Services settings of the script,2

Google Cloud Project association in script

Scope of the project

Credentials

解决方法

问题和解决方法:

我认为您的情况与this thread相同。不幸的是,在当前阶段,高级Google服务的Google API无法直接与自定义功能一起使用。因为无法在定制功能上检索访问令牌。看来这是当前的规范。因此,作为实现目标的解决方法,它使用由Google Apps脚本创建的Web Apps。

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

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 Apps。请注意这一点。
  8. 请将上述脚本的https://script.google.com/macros/s/###/exec的URL设置为url。并请重新部署Web Apps。这样,最新的脚本就会反映到Web应用程序中。所以请注意这一点。

4。测试此替代方法。

请把=RUN_USE_CLICK_COUNT("2020-08-31","2020-09-06")放在一个单元格中。这样,"2020-08-31","2020-09-06"的值将发送到Web Apps,并返回USE_CLICK_COUNT中的值。

注意:

  • 修改Google Apps脚本后,请重新部署为新版本。这样,修改后的脚本将反映到Web Apps。请注意这一点。

参考文献:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-