在Gmail加载项中使用UrlFetchApp出现权限错误 [英] Permissions error using UrlFetchApp in Gmail Add-on

查看:84
本文介绍了在Gmail加载项中使用UrlFetchApp出现权限错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始尝试构建新的Gmail加载项,并遇到以下错误消息:

I am just starting to try building a new Gmail Add-on, and am running into the following error message:

您无权调用提取"

"You do not have permission to call fetch"

在脚本编辑器中测试插件时,以及在我的Gmail中进行部署时,都会发生这种情况.这是代码示例:

This happens when testing the add-on in the Script Editor, and also when deployed inside my Gmail. Here is a sample of the code:

function getContextualAddOn(e) {
    var API_KEY = 'TESTKEY';
    var URL = 'https://[REDACTED]';
    var options = {
        'method' : 'post',
        'contentType': 'application/json',
        'headers': {
            'x-api-key': API_KEY
        },
        'payload' : JSON.stringify({ 'foo': 'bar' })
    };

    var response = UrlFetchApp.fetch(URL, options);

    [more code that builds a card] 
}

如您所见,它是UrlFetchApp.fetch的非常简单的用法.我是Apps Script的新手,所以也许我的清单中缺少一些权限声明或范围.我仅使用UrlFetchApp.getRequest尝试了一个更简单的示例,但由于您无权调用getRequest" ,该操作也失败了.

As you can see, it's a pretty straightforward use of UrlFetchApp.fetch. I'm brand new to Apps Script, so maybe I am missing some permissions declaration or scope in my manifest. I tried an even simpler example just using UrlFetchApp.getRequest, but that also failed with "You do not have permission to call getRequest".

插件的清单与示例中的清单相同:

The manifest for the addon is the same as in the examples:

{
  "timeZone": "America/New_York",
  "dependencies": {
  },
  "exceptionLogging": "STACKDRIVER",

  "oauthScopes": [
    "https://www.googleapis.com/auth/gmail.addons.execute",
    "https://www.googleapis.com/auth/gmail.addons.current.message.readonly",
    "https://www.googleapis.com/auth/userinfo.email"
  ],
  "urlFetchWhitelist": [
    "https://[REDACTED]"
  ],
  "gmail": {
    "name": "Test Add-On",
    "logoUrl": "some url",
    "primaryColor": "#4285F4",
    "secondaryColor": "#4285F4",
    "contextualTriggers": [{
      "unconditional": {},
      "onTriggerFunction": "getContextualAddOn"
    }],
    "version": "TRUSTED_TESTER_V2"
  }
}

是否应该在Gmail附件中允许UrlFetchApp,或者这仅仅是一个错误?我需要在清单中添加一些内容还是在脚本编辑器中启用其他选项?

Is UrlFetchApp supposed to be allowed inside a Gmail Add-On, or is this just a bug? Do I need to add something to my manifest or enable some other option in the script editor?

推荐答案

UrlFetchApp服务需要一个附加范围https://www.googleapis.com/auth/script.external_request.将其添加到作用域列表中,代码应该可以正常工作.

The UrlFetchApp service requires an additional scope, https://www.googleapis.com/auth/script.external_request. Add it to your list of scopes and the code should work.

参考文档(

The scopes required for each Apps Script method is listed under the "Authorization" section in the reference docs (example). Alternatively, you can discover the scopes required by your script by temporarily removing the oauthScopes section of the manifest and viewing the auto-determined scopes for your code in File > Project properties > Scopes. (If you define any scopes in your manifest, this disables the "automatic scope detection" behavior of Apps Script.)

参考

  • Setting Scopes
  • Manifest File
  • All Google Scopes

这篇关于在Gmail加载项中使用UrlFetchApp出现权限错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆