javascript - Google Apps 脚本 OAuth - returnURL 以更新附加组件

标签 javascript oauth google-apps-script

我正在使用以下 Google 应用脚本库(Google 文档插件):

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

我的脚本作为返回网址:

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 条件并重新呈现侧边栏而不刷新页面。我是否以错误的方式处理这个问题?

最佳答案

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

根据您的喜好设置超时持续时间。太长,一旦关闭“身份验证”选项卡就会出现延迟。

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);
  // }
}

对伪代码表示歉意,但我是通过手机接听的

关于javascript - Google Apps 脚本 OAuth - returnURL 以更新附加组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33633175/

相关文章:

javascript - 悬停时元素位置正在改变

javascript - Yii 将 url 解析为 Controller /操作而不是执行底层脚本

javascript - ReactJS 和 jQuery 是互斥的吗?

javascript - Gmail 插件 : Oauth not being triggered

javascript - 使用 JavaScript 在网页之间共享数据

google-app-engine - 在 Google App Engine 中保护 RESTful API

mysql - 未定义 PlatformTransactionManager 类型的合格 bean,仅当它不在内存中时才会发生错误

php - 集成 openID 和 oauth 作为网站登录、登录和身份验证系统

google-apps-script - Google Apps 脚本异步函数在服务器端执行

google-apps-script - Trello API : How to POST a Card from Google Apps Script (GAS)