本指南介绍如何对 Google Chat API 的 Space
资源使用 get
方法,以查看聊天室的详细信息,例如显示名称、说明和指南。
Space
资源代表着用户和 Chat 应用可以发送消息、共享文件以及开展协作的位置。聊天室有以下几种类型:
- 私信 (DM) 是指两个用户或用户与某个 Chat 应用之间的对话。
- 群聊是指三位或更多用户与 Chat 应用之间的对话。
- 已命名空间是用户在其中发送消息、共享文件和协作的永久位置。
使用应用身份验证进行身份验证时,Chat 应用可以获得 Chat 应用有权访问 Google Chat 的聊天室(例如,该应用所属的聊天室)。通过用户身份验证进行身份验证,您就可以获得通过身份验证的用户有权访问的聊天室。
前提条件
Python
- Python 3.6 或更高版本
- pip 软件包管理工具
适用于 Python 的最新 Google 客户端库。如需安装或更新这些软件包,请在命令行界面中运行以下命令:
pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib oauth2client
已发布的 Chat 应用。如需创建和发布 Chat 应用,请参阅构建 Google Chat 应用。
为 Chat 应用配置授权。获取聊天室支持以下两种身份验证方法:
获取聊天室
如需在 Google Chat 中获取聊天室,请在请求中传递以下内容:
- 使用应用身份验证时,指定
chat.bot
授权范围。使用用户身份验证时,指定chat.spaces.readonly
或chat.spaces
授权范围。 - 对
Space
资源调用get
方法,并传递要获取的空间的name
。请通过 Google Chat 的聊天室资源或聊天室的网址获取聊天室名称。
通过用户身份验证获取聊天室详细信息
以下是通过用户身份验证获取聊天室详细信息的方法:
Python
- 在您的工作目录中,创建一个名为
chat_space_get_user.py
的文件。 在
chat_space_get_user.py
中添加以下代码:import os.path from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build from googleapiclient.errors import HttpError # 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()
在代码中,将
SPACE
替换为聊天室名称,您可以通过 Chat API 中的spaces.list
方法或聊天室网址获取该名称。在您的工作目录中,构建并运行示例:
python3 chat_space_get_user.py
Chat API 会返回 Space
的实例,用于详细说明指定聊天室。
通过应用身份验证获取聊天室详细信息
以下是通过应用身份验证获取聊天室详细信息的方法:
Python
- 在您的工作目录中,创建一个名为
chat_space_get_app.py
的文件。 在
chat_space_get_app.py
中添加以下代码:from httplib2 import Http from oauth2client.service_account import ServiceAccountCredentials from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name( 'credentials.json', SCOPES) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', http=CREDENTIALS.authorize(Http())) # 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)
在代码中,将
SPACE
替换为聊天室名称,您可以通过 Chat API 中的spaces.list()
方法或聊天室的网址获取该名称。在您的工作目录中,构建并运行示例:
python3 chat_space_get_app.py
Chat API 会返回 Space
的实例,用于详细说明指定聊天室。