【问题标题】:Got 401 response when calling my own Google Apps Script调用我自己的 Google Apps 脚本时得到 401 响应
【发布时间】:2021-03-11 17:14:16
【问题描述】:

我使用应用程序脚本编写了一个测试网络应用程序。我将其部署为以“用户访问网络应用程序”的身份执行,并将访问权限设置为“拥有 Google 帐户的任何人”。然后,我尝试从另一个充当客户端的脚本调用 Web 应用程序功能。我在登录我的谷歌帐户时完成了所有这些操作,网络应用程序和客户端都在同一个浏览器窗口中(在不同的选项卡中)。但我总是收到 401 错误。然后我尝试将一些 oauthScopes 添加到客户端脚本的 appsscript.json 文件中,但这没有帮助。任何解决此问题的帮助将不胜感激。

网页应用代码:

function doPost(e) {
  if(typeof e !== 'undefined') {
    let par = e.parameter;
    let retval = par.x*3;
    let retobj = {"retval":retval};
    return ContentService.createTextOutput(JSON.stringify(retobj));
  }

}

客户端代码:

function callWebApp(x) {
  Logger.log("starting call webapp");
  var payload = {
        "info" : "some text",
        "x" : 3,
        "type" : "post",
      };

  var options = {
        "method"  : "POST",
        "payload" : payload,
        "followRedirects" : true,
        "muteHttpExceptions": true
      };

  var result = UrlFetchApp.fetch(urlString, options);
  Logger.log(result.getResponseCode());
  if (result.getResponseCode() == 200) {
    Logger.log('got response');
    var params = JSON.parse(result.getContentText());
    Logger.log(params.retval);
  

  }

  return 1;
}

客户端的appscript.json:

{
  "timeZone": "America/New_York",
  "dependencies": {
  },
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "oauthScopes": [
      "https://www.googleapis.com/auth/script.deployments",
      "https://www.googleapis.com/auth/script.deployments.readonly",
      "https://www.googleapis.com/auth/script.external_request",
      "https://www.googleapis.com/auth/script.projects",
      "https://www.googleapis.com/auth/script.processes",
      "https://www.googleapis.com/auth/script.projects.readonly"
    ]
}  

执行日志: 7:47:30 PM 通知执行开始 7:47:30 PM 信息开始呼叫 webapp 下午 7:47:30 信息 401.0 7:47:30 PM 通知执行完成

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    修改点:

    • 当您将 Web Apps 部署为 Execute as: User accessing the web appWho has access: Anyone with Google account 并希望使用脚本(在本例中为 Google Apps 脚本)访问 Web Apps 时,需要向 Web Apps 请求通过在请求标头中包含访问令牌。
    • 并且,当访问令牌用于向 Web 应用请求时,请将 https://www.googleapis.com/auth/drive.readonly 和/或 https://www.googleapis.com/auth/drive 的范围包含到您客户端中的当前范围中。
    • 在您的脚本中,似乎没有声明urlString。虽然我不确定这是否会在其他地方声明,但请再次确认。

    当这些点反映到你的脚本中时,它变成如下。

    修改后的脚本:

    从:
    var options = {
          "method"  : "POST",
          "payload" : payload,
          "followRedirects" : true,
          "muteHttpExceptions": true
        };
    
    至:
    var options = {
      "method": "POST",
      "payload": payload,
      "muteHttpExceptions": true,
      "headers": {authorization: "Bearer " + ScriptApp.getOAuthToken()}
    };
    

    注意:

    • 如果您想让用户使用脚本向您的 Web 应用发出请求,还需要通过在请求标头中包含访问令牌来向 Web 应用发出请求。此外,还需要与用户共享 Google Apps 脚本项目。请注意这一点。
    • 如果在运行上述脚本时无法检索到预期结果,请将 Web 应用程序重新部署为新版本并再次测试。

    参考资料:

    【讨论】:

    • 感谢您对我的问题的详细回答。我有一个后续问题。我正在尝试将客户端脚本用作谷歌表格脚本的一部分。该脚本由谷歌表格中的公式调用,但返回 401 响应。我认为这是因为脚本无法访问身份验证令牌。我的谷歌表格脚本有什么方法可以调用我的网络应用程序吗?
    • @Robert 感谢您的回复。我很高兴你的问题得到了解决。我愿意支持你。但是回复的问题是新问题,和你的问题不同。那么您可以通过包含更多信息将其作为新问题发布吗?因为当您的初始问题被评论更改时,看到您的问题的其他用户会感到困惑。通过将其发布为新问题,包括我在内的用户都可以想到它。如果你能合作解决你的新问题,我很高兴。你能合作解决你的新问题吗?
    • 我发布了一个新问题stackoverflow.com/questions/66607406/…
    • @Robert 感谢您的回复。当我能正确理解您的新问题并找到解决方案时,我愿意回答。
    猜你喜欢
    • 2021-05-21
    • 2022-12-09
    • 2023-02-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多