This guide explains how to use the list
method on the membership
resource
of the Google Chat API to list members in a space as a paginated, filterable list
of memberships in a space. Listing memberships with
app authentication
lists memberships in spaces that the Chat app has
access to, but excludes Chat app memberships, including
its own. Listing memberships with
User authentication
lists memberships in spaces that the authenticated user has access to.
The
Membership
resource
represents whether a human user or Google Chat app is invited to,
part of, or absent from a space.
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.
List members in a space with user authentication
To list users, Google Groups, and Chat app in a space that the authenticated user has access to, pass the following in your request:
- With
user authentication,
specify the
chat.memberships.readonly
orchat.memberships
authorization scope. - Call the
list
method on themembership
resource. - To list Google Groups, set the query parameter
showGroups
totrue
.
The following example lists Google Group, human, and app members visible to the authenticated user.
Python
- In your working directory, create a file named
chat_member_list_user.py
. Include the following code in
chat_member_list_user.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 lists Google Group, human, and app members in a specified space. ''' # 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().list( # The space for which to list memberships. parent = 'spaces/SPACE', # Set this parameter to list Google Groups. showGroups = 'true' ).execute() # Prints the list of memberships. print(result) if __name__ == '__main__': main()
In the code, replace
SPACE
with a space name, which you can obtain from thespaces.list
method in the Chat API, or from a space's URL.In your working directory, build and run the sample:
python3 chat_member_list_user.py
The Google Chat API returns a list of Google Group, human, and app members from the specified space.
List members in a space with app authentication
To list users and Chat app in a space that the authenticated app has access to, pass the following in your request:
- With
app authentication,
specify the
chat.bot
authorization scope. - Call the
list
method on themember
resource. - To list Google Groups, set the query parameter
showGroups
totrue
.
The following example lists human space members (not space managers) visible to the Chat app:
Python
- In your working directory, create a file named
chat_member_list_app.py
. Include the following code in
chat_member_list_app.py
:from google.oauth2 import service_account from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ( service_account.Credentials.from_service_account_file('credentials.json') .with_scopes(SCOPES) ) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', credentials=CREDENTIALS) # Use the service endpoint to call Chat API. result = chat.spaces().members().list( # The space for which to list memberships. parent = 'spaces/SPACE', # An optional filter that returns only human space members. filter = 'member.type = "HUMAN" AND role = "ROLE_MEMBER"' ).execute() print(result)
In the code, replace
SPACE
with a space name, which you can obtain from thespaces.list
method in the Chat API, or from a space's URL.In your working directory, build and run the sample:
python3 chat_member_list_app.py
The Google Chat API returns a list of human space members (excluding space managers) from the specified space.
Customize pagination or filter the list
To list the memberships, pass the following query parameters to customize pagination of, or filter, listed memberships:
pageSize
: The maximum number of memberships to return. The service might return fewer than this value. If unspecified, at most 100 spaces are returned. The maximum value is 1,000; values above 1,000 are automatically changed to 1,000.pageToken
: A page token, received from a previous list spaces call. Provide this token to retrieve the subsequent page. When paginating, the filter value should match the call that provided the page token. Passing a different value might lead to unexpected results.filter
: A query filter. Requires user authentication. For supported query details, see thespaces.members.list
method.
Related topics
- Get details about a user's or Chat app's membership.
- 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.