我在使用 Google Apps 脚本时遇到问题状态代币从托管库调用时。这意味着一个The state token is invalid or has expired. Please try again.
如果状态令牌是从子函数创建的,则始终会收到错误。
这是库中的一些示例代码(您可以使用项目密钥添加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);
}
When testFunction
运行时,文档中的对话框会显示两个 url,第一个具有动态状态 url 无效,第二个具有存储状态令牌有效。
这是错误还是预期行为?
您当前尝试执行的操作不受支持。具体来说,在外部脚本中运行的库中创建状态令牌,但让回调直接进入库。从今天开始,回调必须始终针对外部脚本,然后可以根据需要委托回库。您可以在问题跟踪器上打开功能请求来支持您的用例,我们将进一步考虑。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)