如何确保在 OAuth2 身份验证成功后重新发送原始 Google 聊天消息?

How can I ensure re-dispatch of original Google Chat message after OAuth2 authentication succeeds?

我正在使用 Google Apps 脚本编写 Google 环聊聊天机器人。机器人使用应用程序脚本 OAuth2 库向第三方服务进行身份验证。如本 How-To 中所述,当机器人收到一条消息但需要第三方服务进行身份验证时,机器人会向聊天发送一个特殊的 REQUEST_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");
    //
    return HtmlService.createHtmlOutput("<script>window.top.location.href='" + request.parameter.configCompleteRedirectUrl + "';</script>");
  } else {
    Logger.log("user denied access");
    return HtmlService.createHtmlOutput('Denied');
  }
}

该服务定义了一个回调验证函数,该函数又将浏览器发送到 configCompleteRedirectUrl。达到此 URL 后,应再次发送或重新发送原始消息(请参阅 How-To step 7.3)。

认证回调成功,因为浏览器OAuth流程中显示的最后一页是event.configCompleteRedirectUrl中指定的页面。在聊天 window 中,配置提示被删除,原始消息更改为 public。但是,不会再次发送原始消息。 apps script console 中显示的最后一条日志来自身份验证回调事件。

是不是我做错了什么导致无法再次发送原始消息?

much back and forth 与 Google 支持团队成员交流后,发现当 运行 针对 V8 Apps 脚本 运行时间。

我的 appsscript.json 文件设置了 "runtimeVersion": "V8"。重新调度在这种情况下不起作用。在我恢复到 appsscript.json 中的 "runtimeVersion": "STABLE" 并重新部署我的脚本后,重新调度开始工作。