Update a space (original) (raw)
This guide explains how to use thepatch()method on the Space resource of the Google Chat API to update a space. Update a space to change attributes about a space, like its user-visible display name, description, and guidelines.
If you're a Google Workspace administrator, you can call the patch() method to update any existing space in your Google Workspace organization.
TheSpace resourcerepresents a place where people and Chat apps can send messages, share files, and collaborate. There are several types of spaces:
- Direct messages (DMs) are conversations between two users or a user and a Chat app.
- Group chats are conversations between three or more users and Chat apps.
- Named spaces are persistent places where people send messages, share files, and collaborate.
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 OAuth client ID credentials for a desktop application. To run the sample in this guide, save the credentials as a JSON file named
credentials.jsonto your local directory.
For guidance, complete the steps for setting up your environment in this[quickstart](/workspace/chat/api/guides/quickstart/nodejs
#set-up-environment).
- Choose an authorization scope that supports user authentication.
- A Google Chat space. To create one using the Google Chat API, seeCreate a space. To create one in Chat, visit theHelp Center documentation.
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 OAuth client ID credentials for a desktop application. To run the sample in this guide, save the credentials as a JSON file named
credentials.jsonto your local directory.
For guidance, complete the steps for setting up your environment in this[quickstart](/workspace/chat/api/guides/quickstart/python
#set-up-environment).
- Choose an authorization scope that supports user authentication.
- A Google Chat space. To create one using the Google Chat API, seeCreate a space. To create one in Chat, visit theHelp Center documentation.
Java
- 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 Java Cloud Client Library.
- Create OAuth client ID credentials for a desktop application. To run the sample in this guide, save the credentials as a JSON file named
credentials.jsonto your local directory.
For guidance, complete the steps for setting up your environment in this[quickstart](/workspace/chat/api/guides/quickstart/java
#set-up-environment).
- Choose an authorization scope that supports user authentication.
- A Google Chat space. To create one using the Google Chat API, seeCreate a space. To create one in Chat, visit theHelp Center documentation.
Apps Script
- 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.
- Create a standalone Apps Script project, and turn on the Advanced Chat Service.
For steps to set up your environment, complete thisquickstart.
- Choose an authorization scope that supports user authentication.
- A Google Chat space. To create one using the Google Chat API, seeCreate a space. To create one in Chat, visit theHelp Center documentation.
Update a space as a user
To update an existing space in Google Chat withuser authentication, pass the following in your request:
- Specify the
chat.spacesauthorization scope. - Call theUpdateSpace()method. In your request, you specify the space
namefield, theupdateMaskfield with one or more fields to update, and abodywith the updated space information.
You can update things like the display name, space type, history state, and more. To see all the fields you can update, see the reference documentation.
Here's how to update the displayName field of an existing space:
Node.js
Python
Java
Apps Script
To run this sample, replace SPACE_NAME with the ID from the space'snamefield. You can obtain the ID by calling theListSpaces()method or from the space's URL.
The Google Chat API returns an instance of theSpace reflecting the updates.
Update a space as a Google Workspace administrator
If you're a Google Workspace administrator, you can call theUpdateSpace() method to update any space 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 anauthorization scopethat supports calling the method usingadministrator privileges.
- In your request, specify the query parameter
useAdminAccesstotrue.
For more information and examples, seeManage Google Chat spaces as a Google Workspace administrator.
Update a space as a Chat app
App authentication requires one-timeadministrator approval.
To update an existing space in Google Chat withapp authentication, pass the following in your request:
- Specify the
chat.app.spacesauthorization scope. With app authentication, you can only update spaces created by Chat apps. - Call thepatch methodon the Space resource. In your request, you specify the space
namefield, theupdateMaskfield with one or more fields to update, and abodywith the updated space information.
You can update things like the display name, space type, history state, permission settings, and more. To see all the fields you can update, see the reference documentation.
Write a script that calls Chat API
Here's how to update the spaceDetails field of an existing space:
Python
- In your working directory, create a file named
chat_space_update_app.py. - Include the following code in
chat_space_update_app.py:
from google.oauth2 import service_account
from apiclient.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.app.spaces"]
def main():
'''
Authenticates with Chat API using app authentication,
then updates the specified space description and guidelines.
'''
# 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().patch(
# The space to update, and the updated space details.
#
# Replace {space} with a space name.
# Obtain the space name from the spaces resource of Chat API,
# or from a space's URL.
name='spaces/SPACE',
updateMask='spaceDetails',
body={
'spaceDetails': {
'description': 'This description was updated with Chat API!',
'guidelines': 'These guidelines were updated with Chat API!'
}
}
).execute()
# Prints details about the updated space.
print(result)
if __name__ == '__main__':
main() - In the code, replace the following:
SPACEwith a space name, which you can obtain from thespaces.list methodin the Chat API, or from a space's URL.
- In your working directory, build and run the sample:
python3 chat_space_update_app.py The Google Chat API returns an instance of theSpace resource reflecting the updates.
Limitations and considerations
- With app authentication, Chat apps:
- Can only updatepredefinedPermissionSettingsorpermissionSettingsin spaces they created.
- Can't update accessSettings.audience.
- Get details about a space.
- List spaces.
- Delete a space.
- Set up a space.
- Find a direct message space.
- Make a space discoverable to specific users.