从谷歌表调用时getOAuthToken返回null(getOAuthToken returns n

2019-10-29 18:52发布

我工作的一种方式来限制一些谷歌的表为特定用户,使用OAuth规范和AWS API网关呼叫,但我面临的一个问题与ScriptApp.getOAuthToken()函数。

当我正在与谷歌Apps脚本调试器的代码,一切都很好, ScriptApp.getOAuthToken()返回我的令牌,我可以传递给我的AWS API。 现在预期的结果是刚收到的用户名。 但是,如果我尝试使用我的功能在谷歌表单元格宏,我有以下错误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是空的,但我不明白为什么它与调试器运行良好,并有纸张文档中不一样。 我失去了一些东西,我不觉得有什么。

任何帮助将非常感激。

Answer 1:

你不能让里面需要用户授权宏的调用。

与大多数其他类型的应用程序的脚本,自定义的功能不要求用户授权访问个人数据。 因此,他们只能调用没有访问个人数据服务。

资源



文章来源: getOAuthToken returns null when called from Google Sheets