Search code examples
google-apps-scripturlfetchgoogle-hangouts

"Invalid argument" when sending GET to spaces.members.list


I'm trying to create a Google Hangouts Chat chatbot (in G Suite) using Apps Script. I want to get a list of everyone in the chatroom, but this isn't directly supported in Apps Scripts yet, so I'm using the rest API. The API call list seems straightforward:

The command is
GET https://chat.googleapis.com/v1/{parent=spaces/*}/members

I've created a service account for authorization and then used

var endpoint = 'https://chat.googleapis.com/v1/{parent="spaces/pQkgxxxxxxx"}/members'
var options = {
  method: "GET",
  contentType : "application/json" ,
  muteHttpExceptions : true,
  headers: {
    "Authorization": "Bearer " + goa.getToken(),
  }
};
var response = UrlFetchApp.fetch(endpoint, options)`

To which I get

Invalid argument: https://chat.googleapis.com/v1/{parent="spaces/pQkgxxxxxxxx"}/members

I've tried encoding the parent parameter, but the error persists. Any ideas?


Solution

  • Per official documentation on the page you linked, the expected format of the path parameter parent is of the form spaces/*. The example value given is spaces/AAAAMpdlehY

    In other words, you are not expected to write the {parents= and } bits, even though the template URL

    GET https://chat.googleapis.com/v1/{parent=spaces/*}/members

    has them. This template url format is explained in-depth on the Google API HTTP annotation website.

    In your example, the correct URI to GET is https://chat.googleapis.com/v1/spaces/pQkgxxxxxxx/members

    You should also consider that it may take multiple calls to resolve all members of the space pQkgxxxxxxx, by checking for a nextPageToken in the response (and passing that as the URL parameter pageToken in the next call).

    You should also consider that the MemberShip returned by this query may include members with various states of membership.