[英]Google Apps Scripts OAuth - returnURL to update the add on
我将以下库用于Google App脚本(Google Docs Addon):
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);
// }
}
对伪代码表示歉意,但我正在用手机接听
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.