我正在使用以下 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/