This guide explains how to use the get
method on a Space
resource of the
Google Chat API to see details about a space, like its display name, description,
and guidelines.
The
Space
resource
represents 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.
Authenticating with app authentication lets a Chat app get spaces that the Chat app has access to in Google Chat (for example, spaces that the app is a member of). Authenticating with user authentication lets you get spaces that the authenticated user has access to.
Prerequisites
Python
- A Business or Enterprise Google Workspace account with access to Google Chat.
- Python 3.6 or greater
- The pip package management tool
- The latest Google client libraries. To install or update them,
run the following command in your command-line interface:
pip3 install --upgrade google-api-python-client google-auth-oauthlib
- 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 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.
Node.js
- A Business or Enterprise Google Workspace account with access to Google Chat.
- Node.js 14 or greater
- The npm package management tool
- The latest Google client libraries. To install or update them,
run the following command in your command-line interface:
npm install @google-cloud/local-auth @googleapis/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 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 a space
To get a space in Google Chat, pass the following in your request:
- With
app authentication,
specify the
chat.bot
authorization scope. With user authentication, specify thechat.spaces.readonly
orchat.spaces
authorization scope. - Call the
get
method on theSpace
resource, passing thename
of the space to get. Obtain the space name from the spaces resource of Google Chat, or from a space's URL.
Get space details with user authentication
Here's how to get space details with user authentication:
Python
- In your working directory, create a file named
chat_space_get_user.py
. Include the following code in
chat_space_get_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.spaces.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then gets details about 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().get( # The space 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. name='spaces/SPACE' ).execute() # Prints details about the space. 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_space_get_user.py
Node.js
- In your working directory, create a file named
get-space.js
. Include the following code in
get-space.js
:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Gets details about a Chat space by name. * @return {!Object} */ async function getSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.spaces.readonly', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.get({name: 'spaces/SPACE'}); } getSpace().then(console.log);
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, run the sample:
node get-space.js
The Chat API returns an instance of
Space
that details the specified space.
Get space details with app authentication
Here's how to get space details with app authentication:
Python
- In your working directory, create a file named
chat_space_get_app.py
. Include the following code in
chat_space_get_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().get( # The space 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. name='spaces/SPACE' ).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_space_get_app.py
Node.js
- In your working directory, create a file named
app-get-space.js
. Include the following code in
app-get-space.js
:const chat = require('@googleapis/chat'); /** * Gets details about a Chat space by name. * @return {!Promise<!Object>} */ async function getSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.bot', ]; const auth = new chat.auth.GoogleAuth({ scopes, keyFilename: 'credentials.json', }); const authClient = await auth.getClient(); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.get({name: 'spaces/SPACE'}); } getSpace().then(console.log);
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, run the sample:
node app-get-space.js
The Chat API returns an instance of
Space
that details the specified space.
Related topics
- Create a space.
- Set up a space.
- List spaces.
- Update a space.
- Delete a space.
- Find a direct message space.