This guide explains how to get a membership with the Google Chat API. Getting a
membership returns details about the membership. The response is an instance of
Authenticating with app authentication lets a Chat app get memberships from spaces that it has access to in Google Chat (for example, spaces it's a member of), but excludes Chat app memberships, including its own. Authenticating with user authentication returns memberships from spaces that the authenticated user has access to.
- Python 3.6 or greater
- The pip package management tool
The Google client libraries for Python. To install them, run the following command in your command-line interface:
pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib oauth2client
A published Chat app. To create and publish a Chat app, see Build a Google Chat app.
Authorization configured for the Chat app. Getting a membership supports both of the following authentication methods:
- User authentication
- App authentication
- User authentication with the
Get a membership
To asynchronously get a membership in Google Chat, pass the following in your request:
app authentication, specify the
chat.botauthorization scope. With user authentication, specify the
chat.membershipsauthorization scope. As a best practice, choose the most restrictive scope that still allows your app to function.
- Call the
getmethod on the
- Pass the
nameof the membership to get. Obtain the membership name from the membership resource of Google Chat.
Here's how to get a membership with user authentication:
- In your working directory, create a file named
Include the following code in
import os.path from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError # Define your app's authorization scopes. # When modifying these scopes, delete the file token.json, if it exists. SCOPES = ["https://www.googleapis.com/auth/chat.memberships.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then gets details about a specified membership. ''' # Authenticate with Google Workspace # and get user authorization. flow = InstalledAppFlow.from_client_secrets_file( 'client_secrets.json', SCOPES) creds = flow.run_local_server() # Build a service endpoint for Chat API. chat = build('chat', 'v1', credentials=creds) # Use the service endpoint to call Chat API. result = chat.spaces().members().get( # The membership to get. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. # # Replace MEMBER with a membership name. # Obtain the membership name from the memberships resource of # Chat API. name='spaces/SPACE/members/MEMBER' ).execute() # Prints details about the created membership. print(result) if __name__ == '__main__': main()
In the code, replace the following:
SPACE: a space name, which you can obtain from the
spaces.listmethod in the Chat API, or from a space's URL.
MEMBER: a membership name, which you can obtain from the
spaces.members.listmethod in the Chat API.
In your working directory, build and run the sample:
The Chat API returns an instance of
detailing the specified membership.