如何在托管库中正确构造回调URL的状态标记?

时间:2014-05-26 09:39:54

标签: google-apps-script

从托管库调用时,我遇到了Google Apps脚本state tokens的问题。这意味着始终接收到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);
}

运行testFunction时,文档中的对话框会显示两个网址,第一个包含动态状态网址的网址无效,第二个存储状态标记可用。

这是错误还是预期的行为?

2 个答案:

答案 0 :(得分:2)

目前您正在尝试做的事情不受支持。特别是在外部脚本中运行的库中创建状态标记,但让回调直接进入库。截至今天,回调必须始终指向外部脚本,然后可以根据需要将其委托回库中。您可以在问题跟踪器上打开功能请求以支持您的用例,我们会进一步考虑。

答案 1 :(得分:0)

使用库来处理身份验证流程的一个示例是从用户所指向的库中发布Web应用程序作为身份验证过程。

nextLine()