我们希望将API中的数据导入Google表格。我们的API需要身份验证我们的暂定架构让用户在ModalDialog中输入他们的凭证,然后我们点击我们的认证端点,该端点返回保存的会话令牌。随后,用户进行自定义函数调用,我们在其中调用API数据端点并包含身份验证令牌。
我的问题 - 如何将身份验证令牌放入自定义函数的范围?似乎PropertiesService.getUserProperties应该执行此操作,但我们在获取凭据令牌的函数中设置的值在自定义函数中不可见。
function saveCredentials(action){
var username = action['username'],
password = action['password'];
var response = UrFetchApp.fetch('https://www.foo.com/account/logon/, {method: 'post', payload: {email: action, password: password}});
var token = response.getAllHeaders()['Set-Cookie'];
PropertiesService.getUserProperties().setProperty('token', token);}
/**
* @param {parameter1}
* @param {parameter2}
* @customfunction
*/
function getDataFromFooAPI(parameter1, parameter2){
var token = PropertiesService.getUserProperties().getProperty('token') // expecting this to be the value set in saveCredentials but it is not.
var url = 'https://www.foo.com/api/data/' + parameter1 + '/' + parameter2;
var response = UrlFetchApp.fetch(url, {'headers' : {'token' : token}};
}
自定义函数执行的上下文是否引用了与脚本其余部分不同的PropertiesService?我们是在尝试做不可能的事吗?
答案 0 :(得分:1)
有一些documented limitations具有自定义功能,特别是他们无法访问个人信息。他们可以访问脚本和文档属性,但不能访问用户属性。对于有权访问工作表并未经授权操作的所有用户,自定义功能应始终如一。依靠用户属性会使这几乎不可能。
如果您可以使用文档属性,那么应该可以使用。