Javascript 如何确保OAuth2身份验证成功后重新发送原始Google聊天信息?
我正在用谷歌应用程序脚本编写一个谷歌聊天机器人。bot使用应用程序脚本OAuth2库与第三方服务进行身份验证。如本文所述,当bot接收到消息但需要使用第三方服务进行身份验证时,bot会向聊天室发送一个特殊的Javascript 如何确保OAuth2身份验证成功后重新发送原始Google聊天信息?,javascript,google-apps-script,hangouts-chat,Javascript,Google Apps Script,Hangouts Chat,我正在用谷歌应用程序脚本编写一个谷歌聊天机器人。bot使用应用程序脚本OAuth2库与第三方服务进行身份验证。如本文所述,当bot接收到消息但需要使用第三方服务进行身份验证时,bot会向聊天室发送一个特殊的REQUEST\u CONFIG回复,其中包含configCompleteRedirectUrl var scriptProperties = PropertiesService.getScriptProperties(); function onMessage(event) { var
REQUEST\u CONFIG
回复,其中包含configCompleteRedirectUrl
var scriptProperties = PropertiesService.getScriptProperties();
function onMessage(event) {
var service = getThirdPartyService();
if (!service.hasAccess()) {
return requestThirdPartyAuth(service, event);
}
Logger.log('execution passed authentication');
return { text: 'Original message ' + event.message.argumentText };
}
function getThirdPartyService() {
var clientId = scriptProperties.getProperty('CLIENT_ID');
var clientSecret = scriptProperties.getProperty('CLIENT_SECRET');
return OAuth2.createService('ThirdPartyApp')
// Set the endpoint URLs.
.setAuthorizationBaseUrl('https://...')
.setTokenUrl('https://.../oauth/token')
// Set the client ID and secret.
.setClientId(clientId)
.setClientSecret(clientSecret)
// Set the name of the callback function that should be invoked to
// complete the OAuth flow.
.setCallbackFunction('authThirdPartyCallback')
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getUserProperties())
.setCache(CacheService.getUserCache())
.setLock(LockService.getUserLock())
// Set the scope and other parameters.
.setScope('...')
.setParam('audience', '...')
.setParam('prompt', 'consent');
}
function requestThirdPartyAuth(service, event) {
Logger.log('Authorization requested');
return { "actionResponse": {
"type": "REQUEST_CONFIG",
"url": service.getAuthorizationUrl({
configCompleteRedirectUrl: event.configCompleteRedirectUrl
})
}};
/**
* Handles the OAuth callback.
*/
function authThirdPartyCallback(request) {
var service = getThirdPartyService();
var authorized = service.handleCallback(request);
if (authorized) {
Logger.log("user authorized");
//https://stackoverflow.com/a/48030297/9660
return HtmlService.createHtmlOutput("<script>window.top.location.href='" + request.parameter.configCompleteRedirectUrl + "';</script>");
} else {
Logger.log("user denied access");
return HtmlService.createHtmlOutput('Denied');
}
}
var scriptProperties=PropertiesService.getScriptProperties();
函数onMessage(事件){
var service=getThirdPartyService();
如果(!service.hasAccess()){
返回请求第三方授权(服务、事件);
}
Logger.log('执行通过身份验证');
返回{text:'原始消息'+event.message.argumentText};
}
函数getThirdPartyService(){
var clientId=scriptProperties.getProperty('CLIENT_ID');
var clientSecret=scriptProperties.getProperty('CLIENT_SECRET');
返回OAuth2.createService('ThirdPartyApp')
//设置端点URL。
.setAuthorizationBaseUrl('https://...')
.setTokenUrl('https://.../oauth/token')
//设置客户端ID和密码。
.setClientId(clientId)
.setClientSecret(clientSecret)
//设置要调用的回调函数的名称
//完成OAuth流。
.SetCallback函数('authThirdPartyCallback')
//设置应在其中保留授权令牌的属性存储。
.setPropertyStore(PropertiesService.getUserProperties())
.setCache(CacheService.getUserCache())
.setLock(LockService.getUserLock())
//设置范围和其他参数。
.setScope(“…”)
.setParam('受众','…'))
.setParam(“提示”、“同意”);
}
函数requestThirdPartyAuth(服务、事件){
Logger.log(“请求授权”);
返回{“actionResponse”:{
“类型”:“请求配置”,
“url”:service.getAuthorizationUrl({
ConfigCompleteDirectURL:event.ConfigCompleteDirectURL
})
}};
/**
*处理OAuth回调。
*/
函数authThirdPartyCallback(请求){
var service=getThirdPartyService();
var authorized=服务handleCallback(请求);
如果(授权){
Logger.log(“用户授权”);
//https://stackoverflow.com/a/48030297/9660
返回HtmlService.createHtmlOutput(“window.top.location.href=”+request.parameter.ConfigCompleteDirectURL+“;”);
}否则{
Logger.log(“用户拒绝访问”);
返回HtmlService.createHtmlOutput('Denied');
}
}
该服务定义了一个回调身份验证函数,该函数依次将浏览器发送到ConfigCompleteDirectURL
。到达此URL后,应再次发送或重新发送原始消息(请参阅)
身份验证回调成功,因为浏览器OAuth流中显示的最后一个页面是在事件.configCompleteDirectURL
中指定的页面。在聊天窗口中,配置提示将被擦除,原始消息将更改为public。但是,原始消息不会再次调度。最后一个日志将被删除在中显示的是来自身份验证回调事件的
是否有一些我做得不正确的事情阻止了原始消息再次发送?与Google支持团队成员讨论后,发现在针对V8 Apps脚本运行时运行的Hangouts Chat实现中存在错误
我的
appsscript.json
文件设置了“runtimeVersion”:“V8”
在这种情况下重新分派不起作用。在我在appsscript.json
中恢复到“runtimeVersion”:“STABLE”
并重新部署我的脚本后,重新分派开始工作。当你调用requestThirdPartyAuth()时,我迷路了;
它不存在于代码的任何部分,也没有任何参数。此函数引用的是什么?谢谢,函数的名称不正确requestThirdPartyServiceAuth
。修复了它。