【问题标题】:Which are the right scopes to run request on Apps Script API哪些是在 Apps Script API 上运行请求的正确范围
【发布时间】:2020-07-16 21:58:42
【问题描述】:

我正在使用 Apps Script API 使用服务帐户的凭据运行函数。 我添加了 Rest 资源 API https://developers.google.com/apps-script/api/reference/rest/v1/scripts/run 中所需的所有范围。

但是当我在下面运行这个脚本时它失败了。

function run(){

 var CREDENTIALS = {
  "private_key": "Your Private key",
  "client_email": "Your Client email",
  "client_id": "Your Client ID",
  "user_email": "Your Email address",
  "api_key": "Your API key"
 };
 var service = getService(CREDENTIALS.client_email,CREDENTIALS.private_key);
  service.reset();
  if (service.hasAccess()) {
    var url = 'https://script.googleapis.com/v1/projects/[SCRIPT ID]:run';
    var body = {
      "function": [FUNCTION NAME]
    };
    var params = {
      headers: {
        Authorization: 'Bearer ' + service.getAccessToken()
      },
      method: 'post',
      playload : JSON.stringify(body),
      contentType: 'application/json',
      muteHttpExceptions: true
    };
    var response = UrlFetchApp.fetch(url, params);
    Logger.log(response);
  }
  else {
    Logger.log(service.getLastError());
  }
}

function getService(email, privateKey) {
  return OAuth2.createService('Service Account')
      // Set the endpoint URL.
      .setTokenUrl('https://oauth2.googleapis.com/token')

      // Set the private key and issuer.
      .setPrivateKey(privateKey)
      .setIssuer(email)

      // Set the name of the user to impersonate. This will only work for
      // Google Apps for Work/EDU accounts whose admin has setup domain-wide
      // delegation:
      // https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
      .setSubject([USER EMAIL])

      // Set the property store where authorized tokens should be persisted.
      .setPropertyStore(PropertiesService.getScriptProperties())

      // Set the scope. This must match one of the scopes configured during the
      // setup of domain-wide delegation.
      .setScope('https://www.googleapis.com/auth/script.external_request');
}

我有一个 404 错误,我认为它来自范围列表。 因此,我无法使用 OAuth2.0 令牌运行部署为 API 可执行文件的脚本。 我应该选择哪些范围来通过 HTTP 请求运行函数?

【问题讨论】:

  • 嗨@yoxCL9,你确定网址是正确的吗?它是 script.googleapis.com/v1/ scripts /{scriptId}:run,而你的似乎错过了 /scripts/ 路径部分,因此是 404 错误代码。如果身份验证有问题,您应该收到 403
  • 嗨@OlegValter,我以前用projects.get方法检查了一个请求,它工作正常。
  • 嗨@yoxCL9 - 我明白了 - 虽然它似乎并不相关,因为 Tanaike 的回答详细介绍了实际的潜在问题(呵呵,我怎么会错过不支持的服务帐户的免责声明)

标签: google-apps-script google-oauth service-accounts google-apps-script-api


【解决方案1】:

在您的 run 函数中,对于 params 对象,您应该有 payload 而不是 playload

【讨论】:

    【解决方案2】:
    • 您想通过服务帐号使用 Apps Script API。
    • 您希望使用 Google Apps 脚本实现此目的。

    如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。

    问题和解决方法:

    很遗憾,在当前阶段,Apps Script API 中的 scripts.run 方法无法与服务帐户一起使用。 The official document 如下所述。关于这一点,我在测试的时候可以确认 Apps Script API 中的 scripts.run 方法不能与服务账号一起使用。

    警告:Apps Script API 不适用于服务帐户。

    从上述情况来看,作为解决方法,使用 OAuth2 检索到的访问令牌如何?要将 Apps Script API 与 OAuth2 一起使用,需要将 Cloud Platform Project 链接到 Google Apps Script Project。关于这一点,您可以在here 看到链接它们的流程。

    注意:

    参考资料:

    如果这不是您想要的方向,我深表歉意。

    补充:

    • 您想让多个用户以您的所有者身份运行脚本。

    来自your replying,我可以像上面那样理解。当 Apps Script API 用于上述情况时,需要将凭证信息提供给每个用户。当每个用户使用您的凭证信息检索到的访问令牌时,您的目标就可以实现。但我不能推荐这个。因此,在您的情况下,我想使用 Web 应用程序来实现您的目标。流程如下。

    1。准备脚本。

    请准备好您的脚本。例如,在当前阶段,您想让用户运行myFunction()的功能,请输入以下示例脚本。

    function doGet(e) {
      var values = e; // When you want to give the values by requesting, you can use the event object like "e".
      var res = myFunction(values);
      return ContentService.createTextOutput(res);
    }
    
    • 在这种情况下,使用 GET 方法。当您只想运行该功能时,可以使用此脚本。当你想通过给出大数据来运行函数时,你可以使用doPost()而不是doGet()

    2。部署 Web 应用程序。

    1. 在脚本编辑器上,通过“发布”->“部署为 Web 应用”打开一个对话框。
    2. “执行应用程序为:”选择“我”
      • 由此,脚本以所有者身份运行。
      • 这里,当设置“任何人”时,脚本以每个用户的身份运行。在这种情况下,需要将脚本共享给每个用户。并且需要使用访问令牌。请注意这一点。
    3. “谁有权访问应用程序:”选择“任何人,甚至匿名”
      • 在这种情况下,不需要请求访问令牌。我认为作为测试用例,我推荐这个设置。
      • 当然,您也可以使用访问令牌。届时,请将其设为“任何人”
    4. 单击“部署”按钮作为新的“项目版本”。
    5. 自动打开“需要授权”对话框。
      1. 点击“查看权限”。
      2. 选择自己的帐户。
      3. 点击“此应用未验证”中的“高级”。
      4. 点击“转到###项目名称###(不安全)”
      5. 点击“允许”按钮。
    6. 点击“确定”。
    7. 复制 Web 应用程序的 URL。就像https://script.google.com/macros/s/###/exec
      • 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。

    3。使用 Web 应用程序运行函数。

    这是一个使用 Web 应用程序执行 myFunction 的示例 curl 命令。请设置您的网络应用 URL。在上述Web Apps的设置下,每个用户都可以通过以下curl命令访问。

    curl -GL \
      -d "key=value" \
      "https://script.google.com/macros/s/###/exec"
    
    • key=value 用作上述查询参数时,在doGet(e),您可以使用e.parameter.key 检索value

    参考资料:

    【讨论】:

    • 对不起,我错过了这个警告,那么如何使用所有者用户的凭据运行多个用户的脚本?谢谢@Tanaike,但是 Google Apps 脚本项目已经链接到云平台表单项目,并且应用程序名称填写在 API & Services > Credentials > OAuth Consent Screen。
    • @yoxCL9 根据您的问题,我提出了带有 OAuth2 的 Apps Script API。但是从您的回复来看,当您想让用户以所有者身份运行脚本时,我想建议使用 Web Apps。在这种情况下,您的目标可以实现。为此,我添加了安装 Web 应用程序的流程。你能确认一下吗?如果这不是您想要的方向,我很抱歉。
    【解决方案3】:

    您可以将脚本部署为 Web 应用程序。为此,请转至Publish > Deploy as web app。将Execute the app as: 字段设置为Me (youremail)。这样您就可以将脚本作为浏览器链接共享,任何用户都可以使用您的凭据运行脚本。

    您可以添加一些带有确认消息的用户界面,以便用户知道他们已成功执行脚本。您可以在 link 中找到文档。

    【讨论】:

      猜你喜欢
      • 2022-10-01
      • 2013-04-09
      • 1970-01-01
      • 2022-01-17
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      • 2010-12-30
      相关资源
      最近更新 更多