【发布时间】: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