0

I have been searching through a lot of forums to understand if it's possible to fetch the members from a google space. However, I couldn't find any relevant source that I could rely on. https://developers.google.com/chat/api/reference/rest/v1/spaces.members/list - This is one official documentation page, but was not able to follow since no sample codes/explanations were provided. Could someone shed some light on this topic?

Gitz
  • 63
  • 5

1 Answers1

1

In Google Apps Script you can use UrlFetchApp.fetch() to create the API call. This is the code:

function listMembers() {
  var spaceId = "YOUR_SPACE_ID"
  var url = 'https://chat.googleapis.com/v1/spaces/' + spaceId + '/members';
  var options = {
    'method': 'GET',
    'muteHttpExceptions': true,
    'headers': {
      'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
    }
  };

    var response = UrlFetchApp.fetch(url, options);
    Logger.log(response.getContentText()); 
}

You'll need to:

  1. Link the Google Apps Script project to an Standard GCP Project: https://developers.google.com/apps-script/guides/cloud-platform-projects#access_a_standard
  2. Enable the Google Chat API in the GCP project: https://console.cloud.google.com/marketplace/product/google/chat.googleapis.com
  3. Add the following scopes to the appscript.json manifest in your Google Apps Script project:
  "oauthScopes": [
    "https://www.googleapis.com/auth/chat.memberships",
    "https://www.googleapis.com/auth/script.external_request"
  ]

Note:

This API is only available in the Google's Developer Preview Program, running this code without being part of the program will show you this error:

{
  "error": {
    "code": 403,
    "message": "The Google Cloud project isn't allowed to call this API. To call this API, join the Google Workspace Developer Preview program.",
    "status": "PERMISSION_DENIED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help",
        "links": [
          {
            "description": "See the Developer Preview Program page.",
            "url": "https://developers.google.com/workspace/preview"
          }
        ]
      }
    ]
  }
}

More details here.

Bryan Monterrosa
  • 1,385
  • 1
  • 3
  • 13