我在使用 Google Apps 脚本时遇到问题 state tokens从托管库调用时。这意味着 The state token is invalid or has expired. Please try again.
如果状态 token 是从子函数创建的,则始终会收到错误。
这是库中的一些示例代码(您可以使用项目 key MP9K5nBAvEJwbLYG58qx_coq9hSqx7jwh
添加)
var SCRIPT_ID = "1eC5VsM2vkJXa9slM40MTKTlfARGAGyK1myMCU3AB_-Ox_jGxQaoPM8P2";
// get a callback url to render in popup
function getAuthURL() {
var authorizeURL = getCallbackURL('testCallback');
return authorizeURL;
}
// generate a user callback url
function getCallbackURL(callback) {
var state = ScriptApp.newStateToken().withTimeout(3600).withMethod(callback).createToken();
return 'https://script.google.com/macros/d/'+SCRIPT_ID+'/usercallback?state='+state;
}
// generate login popup
function showLogin(doctype){
doctype.getUi().showDialog(
HtmlService
.createTemplate("<div><p><a href='<?=getAuthURL()?>' id='start-auth'><?=getAuthURL()?></a></p>" +
"<p><a href='<?=getAuthURLStored()?>' id='start-auth'><?=getAuthURLStored()?></a></p></div>")
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.NATIVE)
);
}
// dummy callback function
function testCallback(e){
return HtmlService.createHtmlOutput('<b>Success. You can close this window. !</b>')
}
/*
Rather than using dynamic state url storing the callback url and getting from property
(you could set a script trigger to refresh this every 24 hours)
*/
function getAuthURLStored() {
var authorizeURL = getSetCallbackURL();
return authorizeURL;
}
function setCallbackURL(){
PropertiesService.getScriptProperties().setProperty('callbackURL', getCallbackURL('testCallback'))
}
function getSetCallbackURL(){
return PropertiesService.getScriptProperties().getProperty('callbackURL')
}
它可以在 Google 文档中调用为(假设托管库标识符是 statetest。
function testFunction() {
statetest.showLogin(DocumentApp);
}
何时 testFunction
运行时,文档中的对话框会显示两个 url,第一个具有动态状态 url 无效,第二个具有存储状态 token 有效。
这是错误还是预期行为?
最佳答案
目前不支持您尝试执行的操作。具体来说,在外部脚本中运行的库中创建状态 token ,但让回调直接进入库。从今天开始,回调必须始终针对外部脚本,然后可以根据需要委托(delegate)回库。您可以在问题跟踪器上提出功能请求来支持您的使用案例,我们将进一步考虑。
关于google-apps-script - 如何在托管库中正确构建回调 URL 的状态 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23867189/