Search code examples
google-apps-scriptgoogle-apigoogle-chat

Google App Script - Chat API - 404 error when retrieving messages in space via REST API


I face an issue when trying to GET messages for a specific Google Chat Space using APIs.

I try to use the spaces.messages/list endpoint from the official Google API.

✅ When I try to POST a new message with the same code / scope, it works.
❌ When I fo the GET method to retrieve messages, I get a 404 error message (see below).

My code

function getMessagesInSpaceAPI() {

  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('https://www.googleapis.com/auth/chat.messages');

  if (!service.hasAccess()) {
   Logger.log('Authentication error: %s', service.getLastError());
    return;
  } else {
    Logger.log('token = ' + service.getAccessToken())
  }

  // GET MESSAGE
  var res = UrlFetchApp.fetch('https://chat.googleapis.com/v1/spaces/AAAAzf4hBOQ/messages', {
    method: 'GET',
    followRedirects: true,
    muteHttpExceptions: true,
    headers: { 'Authorization': 'Bearer ' + service.getAccessToken() },
    contentType: 'application/json'
  });

  Logger.log(res)
}

Output

{
  "error": {
    "code": 404,
    "message": "Method not found.",
    "status": "NOT_FOUND"
  }
}

(tried in Google App Script debugger and also in Postman, same result)


Solution

  • Update: Access to this method no longer requires the Developer Preview Program.

    I was able to reproduce the problem you encountered when using a Google project that is not registered in the Developer Preview. However, it works fine when I use a Google project that is registered for the Developer Preview Program.

    in the link you referenced says:

    Developer Preview: Available as part of the Google Workspace Developer Preview Program, which grants early access to certain features.

    In order to proceed, it is necessary to have a project registered in the Developer Preview. To register your project, please visit: https://developers.google.com/workspace/preview

    Upon registration, it typically takes approximately one week for the review process to be completed, based on previous experiences.