【问题标题】:UrlFetchApp.fetch in spreadsheet OnSubmit errors with "Don't t have permissions" despite manifest尽管有清单,但电子表格 OnSubmit 中的 UrlFetchApp.fetch 出现“没有权限”错误
【发布时间】:2020-05-13 06:31:02
【问题描述】:

如何在每次提交表单时获取外部 URL?


我需要归档使用 Google 表单提交的网址。

我为与我的表单关联的 Google 电子表格创建了一个应用脚本:

function onSubmit(e) {
  var url="https://web.archive.org/save/http://www.google.com"
  var response = UrlFetchApp.fetch(url, headers);
}

我还编辑了清单以包含允许使用UrlFetchApp.fetch:

{
  "timeZone": "Europe/(redacted)",
  "dependencies": {
  },
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": [
    "https://www.googleapis.com/auth/script.external_request"]
}

但是我不断收到以下错误消息:

您无权调用 UrlFetchApp.fetch。 所需权限:https://www.googleapis.com/auth/script.external_request

【问题讨论】:

  • 重新授权/重新创建触发器...声明所需的权限与同意他们不同

标签: google-apps-script google-sheets-api google-forms


【解决方案1】:

我刚刚测试了您的代码,非常适合我。可能错误与您使用的headers 有关。

还要记住onSubmit 不是simple trigger,这意味着您必须先安装它。简单的触发器遭受各种restrictions,一个是:

他们无法访问需要授权的服务。

因此,在您的情况下,您需要使用installable trigger。首先创建并执行创建触发器的代码,然后会弹出同意屏幕,让您接受触发器函数的范围。

onFormSubmit 的示例可能是这样的:

function createTrigger(){
  var sheet = SpreadsheetApp.openById("<your-sheet-id>");
  ScriptApp.newTrigger('onSubmit')
           .forSpreadsheet(sheet)
           .onFormSubmit()
           .create();
}

在这种情况下,onSubmit 是执行触发器时要调用的函数的名称(您的函数)。

执行此createTrigger 并接受权限。

现在一切都应该正常工作,并且每次都以正确的权限执行。

【讨论】:

    猜你喜欢
    • 2021-08-12
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 2022-01-02
    相关资源
    最近更新 更多