【问题标题】:getOAuthToken returns null when called from Google Sheets从 Google 表格调用 getOAuthToken 时返回 null
【发布时间】:2019-08-05 20:31:11
【问题描述】:

我正在努力通过 oAuth 规范和 AWS API Gateway 调用限制特定用户的某些 Google 表格,但我遇到了ScriptApp.getOAuthToken() 函数的问题。

当我使用 Google Apps 脚本调试器运行代码时,一切都很好,ScriptApp.getOAuthToken() 向我返回了一个令牌,我可以将其传递给我的 AWS API。现在的预期结果只是接收用户名。 但是,如果我尝试将我的函数用作 Google 表格单元格中的宏,则会出现以下错误 Header:null (line 13)

这是 Code.gs 文件中的代码

function HelloW() {
  var token = ScriptApp.getOAuthToken();
  var headers = {
    'Authorization' : token
  }

  var options = {
    'headers' : headers,
    'method' : 'post',
    'contentType': 'application/json',
    'payload' : JSON.stringify(data)
  };

  var response = UrlFetchApp.fetch('https://###/demo-lambda', options);

  var txt = response.getContentText();
  var json = JSON.parse(txt);
  var name = json.Message;

  return name;
}

清单以防万一

{
  "timeZone": "Europe/Paris",
  "dependencies": {
  },
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": ["https://www.googleapis.com/auth/script.external_request", 
"https://www.googleapis.com/auth/spreadsheets", 
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/drive"],
  "sheets": {
    "macros": [{
      "menuName": "HelloW",
      "functionName": "HelloW"
    }]
  }
}

我有一个错误,因为token 为空,但我不明白为什么它在调试器中运行良好,而且它不在表格文档中。我错过了一些东西,但我没有找到。

任何帮助将不胜感激。

【问题讨论】:

  • 当作为宏运行时,需要身份验证的服务可能对函数不可用,这也是ScriptApp.getAuthToken() 返回 null 的原因。查看宏文档以了解您可以访问哪些资源。

标签: google-apps-script google-sheets google-sheets-macros


【解决方案1】:

您不能在需要用户授权的宏内进行调用。

与大多数其他类型的 Apps 脚本不同,自定义函数从不询问 用户授权访问个人数据。因此,他们只能调用无法访问个人数据的服务。

Source

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 2019-06-29
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    相关资源
    最近更新 更多