【问题标题】:Google Apps Scripts OAuth - returnURL to update the add onGoogle Apps Scripts OAuth - returnURL 以更新插件
【发布时间】:2016-02-11 12:26:40
【问题描述】:

我正在为 Google App Scripts(Google Docs Addon)使用以下库:

https://github.com/googlesamples/apps-script-oauth2

我有我的脚本作为返回 url:

https://script.google.com/macros/d/___/usercallback

我有以下运行良好的回调:

function authCallback(request) {
    var Service = geService();
    var isAuthorized = Service.handleCallback(request);
    if (isAuthorized) {
        return HtmlService.createHtmlOutput('Success! You can close this tab.');
    } else {
        return HtmlService.createHtmlOutput('Denied. You can close this tab');
    }
}

在插件 UI 的主代码中,我有以下用于添加侧边栏的开关:

if(!Service.hasAccess()) {
    var authorizationUrl = Service.getAuthorizationUrl();

    template = HtmlService.createTemplateFromFile('HuddleSidebarNoAuth');
    template.authorizationUrl = authorizationUrl;
    html = template.evaluate();


} else {

    template = HtmlService.createTemplateFromFile('HuddleSidebar');
    html = template.evaluate();

}

我需要它在成功回调时重新运行它,以便它达到hasAccess 条件并重新呈现侧边栏而不刷新页面。我会以错误的方式解决这个问题吗?

【问题讨论】:

  • 你能澄清一下这个问题吗?据我所知,名为Service 的Apps Script 类没有定义任何OAuth 方法(如您正在调用的hasAccess() 函数)。但是,AuthorizationInfo 类上有一个方法 getAuthorizationUrl(),它由 ScriptApp.getAuthorizationInfo() 返回——这是您使用的吗?

标签: javascript oauth google-apps-script


【解决方案1】:

在您的侧边栏脚本中,您可以让启动 oauth2 流程的操作也触发 hasAccess 的侦听器。然后,只要 hasAccess 返回正数,就可以取消侦听器并正确加载 UI。

在超时时间方面,您可以根据自己的喜好采取积极或保守的态度。太长了,一旦关闭 Auth 标签页就会有延迟。

function showSidebar() {
  ...
        '<a href="<?= authorizationUrl ?>" target="_blank" onclick="initateAccessListener()">Authorize</a>. ' +
        'Reopen the sidebar when the authorization is complete.');
    ...
  }
}

function initiateAccessListener() {
    timeoutID = window.setTimeout(checkHasAccess, 2000);
}

function checkHasAccess() {

  // code to call a google side function the check hasAccess

  // if (hasAccess) {

  //     window.clearTimeout(timeoutID)
  //     """call to reload sidebar UI content"""

  // } else {
  //    timeoutID = window.setTimeout(slowAlert, 2000);
  // }
}

为伪代码道歉,但我正在通过手机接听

【讨论】:

  • 谢谢,显然我考虑过这种方法,但假设我缺少来自谷歌脚本的回调。谢谢
  • 另外,checkHasAccess 超时应该存在于客户端代码中,而不是谷歌脚本代码中。我已经用这种方法工作了,稍后会发布正确的代码。我会等到那时,看看是否有人可以通过回调而不是超时来提供答案,但我怀疑这是可能的
  • 是的,请确保您使用HtmlService.SandboxMode.IFRAME - 而不是NATIVE(昨天已弃用)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 2017-03-31
  • 2014-11-08
  • 2013-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多