This guide explains how to use the
get
method on
the SpaceEvent
resource of the
Google Chat API to get details about an event from a Google Chat space.
The SpaceEvent
resource represents a change to a space or its child
resources, such as messages, reactions, and memberships. To learn
about the supported event types, see the eventType
field of the
SpaceEvent
resource
reference documentation.
You can request events up to 28 days before the time of the request. The event
contains the most recent version of the resource that changed. For example, if
you request an event about a new message but the message was later updated, the
server returns the updated Message
resource in the event payload.
To call this method, you must use user authentication. To get an event, the authenticated user must be a member of the space where the event occurred.
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.
-
Create OAuth client ID credentials for a desktop application. To run the sample in this
guide, save the credentials as a JSON file named
client_secrets.json
to your local directory.
- Choose an authorization scope that supports user authentication.
Get details about a space event
To get details about a SpaceEvent
in Google Chat, do the following:
- Call the
get
method on theSpaceEvent
resource. - Pass the
name
of theSpaceEvent
to get. Obtain theSpaceEvent
name from theSpaceEvent
resource of Google Chat. - With user authentication, specify an authorization scope that supports the event type in your request. As a best practice, choose the most restrictive scope that still allows your app to function.
Here's how to get a SpaceEvent
with
user authentication:
Python
- In your working directory, create a file named
chat_space_event_get.py
. Include the following code in
chat_space_event_get.py
:"""Gets a SpaceEvent resource from the Chat API.""" 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 = ['SCOPE'] # 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() .spaceEvents() .get( # The space event 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 SPACE_EVENT with a SpaceEvent name. # Obtain the spaceEvent name from the SpaceEvent resource of # Chat API. name='spaces/SPACE/spaceEvents/SPACE_EVENT' ) .execute() ) # Prints details about the created spaceEvent. print(result)
In the code, replace the following:
SCOPE
: An authorization scope based on the event type. For example, if you are getting a space event about a new membership, use thechat.memberships.readonly
scope, formatted ashttps://www.googleapis.com/auth/chat.memberships.readonly
. You can obtain the event type from thespaces.spaceEvents.list
method. To learn how to use this method, see List events from a space.SPACE
: A space name, which you can obtain from thespaces.list
method in the Chat API, or from a space's URL.SPACE_EVENT
: The name of the space event, which you can obtain from thespaces.spaceEvents.list
method.
In your working directory, build and run the sample:
python3 chat_space_event_get.py
The Chat API returns an instance of
SpaceEvent
with details about the event.