Apps Script Bot 上的 404 截断服务器响应

404 truncated server response on Apps Script Bot

我正在尝试开发一个环聊聊天机器人,它每天运行一个功能并发送一条消息。我从 Google 开发者网站上的模板开始,但在获取 URL.

时收到 404 截断服务器响应错误

错误:

Truncated server response: <h1>Not Found</h1> <h2>Error 404</h2>

代码如下:

function onTrigger() {
  var spaceIds = PropertiesService.getScriptProperties()
      .getKeys();
  var message = { 'text': 'Hi! It\'s now ' + (new Date()) };
  for (var i = 0; i < spaceIds.length; ++i) {
    postMessage(spaceIds[i], message);
  }
}

var SCOPE = 'https://www.googleapis.com/auth/chat.bot';
// The values below are copied from the JSON file downloaded upon
// service account creation.
var SERVICE_ACCOUNT_PRIVATE_KEY = "-----BEGIN PRIVATE KEY---- bla bla bla---END PRIVATE KEY-----\n";
var SERVICE_ACCOUNT_EMAIL = "blablabla";

//key and email deleted here for safety reasons.

// Posts a message into the given space ID via the API, using
// service account authentication.
function postMessage(spaceId, message) {

  var service = OAuth2.createService('chat')
      .setTokenUrl('https://accounts.google.com/o/oauth2/token')
      .setPrivateKey(SERVICE_ACCOUNT_PRIVATE_KEY)
      .setClientId(SERVICE_ACCOUNT_EMAIL)
      .setPropertyStore(PropertiesService.getUserProperties())
      .setScope(SCOPE);
  if (!service.hasAccess()) {
    Logger.log('Authentication error: %s', service.getLastError());
    return;
  }
  var url = 'https://chat.googleapis.com/v1/' + spaceId + '/messages';
  UrlFetchApp.fetch(url, {
    method: 'post',
    headers: { 'Authorization': 'Bearer ' + service.getAccessToken() },
    contentType: 'application/json',
    payload: JSON.stringify(message),
  });event.type == "ADDED_TO_SPACE"
}

任何帮助将不胜感激。

提前致谢。 马科斯

答案:

用于在 space 中创建消息的 URL 需要以 spaces/* 的形式提供,其中 * 是 space ID.

修复:

您需要根据文档更改 URL:

var url = 'https://chat.googleapis.com/v1/space/' + spaceId + '/messages';

参考文献: