从Google表格调用时,getOAuthToken返回null [英] getOAuthToken returns null when called from Google Sheets

查看:83
本文介绍了从Google表格调用时,getOAuthToken返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on a way to limits some Google Sheets for specific users, with the oAuth specification and AWS API Gateway calls, but i'm facing a problem with the ScriptApp.getOAuthToken() function.

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

When i'm running the code with the Google Apps Script debugger, everything's fine, ScriptApp.getOAuthToken() returns me a token i can pass to my AWS API. The expected result for now is just to recieve the username. But if i try to use my function as a macro in a Google Sheets cell, i have the following error Header:null (line 13)

这是Code.gs文件中的代码

Here is the code in the Code.gs file

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为空,但是我不明白为什么它在调试器中可以很好地运行,并且不在Sheets文档中.我想念什么,我找不到.

I've got an error because token is null, but i don't understand why it runs well with the debugger, and it doesn't in the Sheets document. I'm missing something and i don't find what.

任何帮助将不胜感激.

推荐答案

您不能在需要用户授权的宏中进行调用.

You cannot make calls inside macros that require user authorization.

与大多数其他类型的应用程序脚本不同,自定义函数从不要求 用户授权访问个人数据.因此,他们只能呼叫无法访问个人数据的服务.

Unlike most other types of Apps Scripts, custom functions never ask users to authorize access to personal data. Consequently, they can only call services that do not have access to personal data.

来源

这篇关于从Google表格调用时,getOAuthToken返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆