Get details about a Google Chat space event (original) (raw)
This guide explains how to use theget()method on the SpaceEvent resource of the Google Chat API to get details about an event from a Google Chat space.
TheSpaceEvent resourcerepresents a change to a space or its child resources, such as messages, reactions, and memberships. To learn about the supported event types, see theeventType field of the SpaceEvent resourcereference 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 useuser authentication. To get an event, the authenticated user must be a member of the space where the event occurred.
Prerequisites
Node.js
- A Business or EnterpriseGoogle Workspace account with access toGoogle 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 Node.js Cloud 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 namedcredentials.jsonto your local directory.
* To authenticate as the Chat app,create service account credentials and save the credentials as a JSON file namedcredentials.json.
- Choose an authorization scope based on whether you want to authenticate as a user or the Chat app.
Python
- A Business or EnterpriseGoogle Workspace account with access toGoogle 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 Cloud 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 namedcredentials.jsonto your local directory.
* To authenticate as the Chat app,create service account credentials and save the credentials as a JSON file namedcredentials.json.
- Choose an authorization scope based on whether you want to authenticate as a user or the Chat app.
Get details about a space event (user authentication)
To get details about a space event in Google Chat, pass the following in your request:
- 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.
- Call theGetSpaceEvent()method, passing the
nameof the space event to get.
The following example gets a space event:
To run this sample, replace the following:
SCOPE_NAME: an authorization scope based on the event type. For example, if you are getting a space event about a new membership, use thechat.memberships.readonlyscope, formatted ashttps://www.googleapis.com/auth/chat.memberships.readonly. You can obtain the event type from theListSpaceEvents()method. To learn how to use this method, seeList events from a space.SPACE_NAME: the ID from the space'sname. You can obtain the ID by calling theListSpaces()method or from the space's URL.SPACE_EVENT_NAME: the ID from the space event'sname. You can obtain the ID from theListSpaceEvents()method. To learn how to use this method, seeList events from a space.
The Chat API returns an instance ofSpaceEventwith details about the event.
Get details about a space event (Chat app authentication)
App authentication requires one-timeadministrator approval.
To get details about a space event from a space withapp authenticationusing the Chat REST API, pass the following in your request:
- Specify one or more authorization scopes to support each event type in your request. As a best practice, choose the most restrictive scope that still allows your app to function. To learn more about choosing a scope, see theAuthentication and authorization overview.
https://www.googleapis.com/auth/chat.app.membershipshttps://www.googleapis.com/auth/chat.app.memberships.readonlyhttps://www.googleapis.com/auth/chat.app.messages.readonlyhttps://www.googleapis.com/auth/chat.app.spaceshttps://www.googleapis.com/auth/chat.app.spaces.readonly
- Call the get methodon thespaceEvents resource.
- Pass the
nameof the space to get event details from.
Write a script that calls Chat API
Here's how to get details about a space event withapp authentication and theChat REST API:
Python
This Python code sample uses theChat REST API.
- In your working directory, create a file named
chat_spaceevents_get_app.py. - Include the following code in
chat_spaceevents_get_app.py:
from google.oauth2 import service_account
from apiclient.discovery import build
# Define your app's authorization scopes.
# Set authorization scopes based on the
# event type. For example, if you are getting a space event
# about a new membership, use the `chat.app.memberships` scope.
#
# When modifying these scopes, delete the file token.json, if it exists.
SCOPES = ["https://www.googleapis.com/auth/chat.app.memberships",
"https://www.googleapis.com/auth/chat.app.memberships.readonly",
"https://www.googleapis.com/auth/chat.app.messages.readonly",
"https://www.googleapis.com/auth/chat.app.spaces",
"https://www.googleapis.com/auth/chat.app.spaces.readonly"]
def main():
'''
Authenticates with Chat API using app authentication,
then lists space events from a specified space.
'''
# Specify service account details.
creds = (
service_account.Credentials.from_service_account_file('credentials.json')
.with_scopes(SCOPES)
)
# 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 to get event details from.
#
# Replace SPACE_NAME with a space name.
# Obtain the space name from the spaces resource of Chat API,
# or from a space's URL.
name='spaces/SPACE_NAME/spaceEvents/SPACE_EVENT_NAME',
).execute()
# Print Chat API's response in your command line interface.
print(result)
if __name__ == '__main__':
main() - In the code, replace the following:
SPACE_NAME: a space name, which you can obtain from thespaces.list methodin the Chat API, or from a space's URL.SPACE_EVENT_NAME: the ID from the space event'sname. You can obtain the ID from theListSpaceEvents()method. To learn how to use this method, seeList events from a space.
- In your working directory, build and run the sample:
python3 chat_spaceevents_get_app.py The Chat API returns a list ofpaginated list of space eventsabout new memberships and messages.