简体   繁体   English

Google Apps脚本OAuth-returnURL以更新插件

[英]Google Apps Scripts OAuth - returnURL to update the add on

I'm using the following library for Google App Scripts (Google Docs Addon): 我将以下库用于Google App脚本(Google Docs Addon):

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

I have my script as the return url: 我将脚本作为返回网址:

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

And I have the following callback which runs fine: 我有以下回调运行良好:

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

In my main code for the add-on UI, I have the following switch for adding a sidebar: 在附加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();

}

I need it to rerun this on successful callback so it hits the hasAccess condition and re-renders the sidebar without refreshing the page. 我需要它在成功的回调上重新运行此命令,以便它达到hasAccess条件并重新渲染边栏而不刷新页面。 Am I going about this the wrong way? 我会以错误的方式处理吗?

In your sidebar script you could have the action that initiates the oauth2 flow also trigger a listener for hasAccess. 在边栏脚本中,您可以执行启动oauth2流的操作,也触发hasAccess的侦听器。 Then as soon as hasAccess returns positive the listener can be cancelled and the UI loaded appropriately. 然后,只要hasAccess返回正值,就可以取消侦听器并正确加载UI。

Be as aggressive or conservative as you like with the timeout duration. 对于超时时间,您可以根据自己的意愿积极或保守。 Too long and there will be a delay once the Auth tab has been closed. 验证标签太长,一旦关闭“验证”标签,就会延迟。

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

Apologies for the pseudocode but i'm answering from my phone 对伪代码表示歉意,但我正在用手机接听

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM