This guide explains how to use the get()
method on the membership
resource
of the Google Chat API to get details about a membership in a space.
If you're a Google Workspace administrator, you can call the get()
method
to retrieve details about any membership in your Google Workspace
organization.
The
Membership
resource
represents whether a human user or Google Chat app is invited to,
part of, or absent from a space.
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.
Prerequisites
Python
- A Business or Enterprise Google Workspace account with access to Google Chat.
- Set up your environment:
- Create a Google Cloud project.
- Configure the OAuth consent screen.
- Enable and configure the Google Chat API with a name, icon, and description for your Chat app.
- Install the Python Google API Client Library.
- Create access credentials based on how you want to authenticate in your Google Chat API
request:
- To authenticate as a Chat user,
create OAuth client ID
credentials and save the credentials as a JSON file named
client_secrets.json
to your local directory. - To authenticate as the Chat app,
create service account
credentials and save the credentials as a JSON file named
credentials.json
.
- To authenticate as a Chat user,
create OAuth client ID
credentials and save the credentials as a JSON file named
- Choose an authorization scope based on whether you want to authenticate as a user or the Chat app.
Get details about a membership
To get details about a membership in Google Chat, pass the following in your request:
- With
app authentication, specify
the
chat.bot
authorization scope. With user authentication, specify thechat.memberships.readonly
orchat.memberships
authorization scope. As a best practice, choose the most restrictive scope that still allows your app to function. - Call the
get()
on themembership
resource. - Pass the
name
of 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:
Python
- In your working directory, create a file named
chat_membership_get.py
. Include the following code in
chat_membership_get.py
:from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build # 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 membership. print(result) if __name__ == '__main__': main()
In the code, replace the following:
SPACE
: a space name, which you can obtain from thespaces.list
method in the Chat API, or from a space's URL.MEMBER
: a membership name, which you can obtain from thespaces.members.list
method in the Chat API.
In your working directory, build and run the sample:
python3 chat_membership_get.py
The Chat API returns an instance of
membership
detailing the specified membership.
Get details about memberships as a Google Workspace administrator
If you're a Google Workspace administrator, you can call the get()
method to retrieve details about a membership for any user in your
Google Workspace organization.
To call this method as a Google Workspace administrator, do the following:
- Call the method using user authentication, and specify an authorization scope that supports calling the method using administrator privileges.
- In your request, specify the query parameter
useAdminAccess
totrue
.
For more information and examples, see Manage Google Chat spaces as a Google Workspace administrator.
Related topics
- List members in a space.
- Invite or add a user or Chat app to a space.
- Update a user's membership in a Google Chat space.
- Remove a user or Chat app from a space.