本指南介绍了如何使用
list()
方法,通过 Google Chat API 的 SpaceEvent 资源列出空间中
资源的更改。
SpaceEvent 资源
表示对目标空间的更改,包括空间的子资源
例如消息、反应和成员资格。如需详细了解支持的
事件类型和事件载荷列表,请参阅 SpaceEvent 资源参考文档的
eventType
和
payload
字段。
您可以列出请求时间之前最多 28 天的事件。服务器会返回包含受影响资源的最新版本的事件。
例如,如果您列出有关新空间成员的事件,服务器会返回包含最新成员资格详细信息的 Membership 资源。如果在请求的时间段内移除了新成员,事件载荷会包含一个空的 Membership 资源。
如需列出空间中的事件,经过身份验证的用户或 Chat 应用必须是该空间的成员。
前提条件
Node.js
- 拥有 Google Workspace访问权限的 Business 或 Enterprise 账号,可访问 Google Chat。
- 设置环境:
- 创建 Google Cloud 项目。
- 配置 OAuth 权限请求页面。
- 启用并配置 Google Chat API,为 Chat 应用设置名称、 图标和说明。
- 安装 Node.js 版 Cloud 客户端库。
- 根据您希望在 Google Chat API
请求中进行身份验证的方式创建访问凭据:
- 如需以 Chat 用户身份进行身份验证,
请创建 OAuth 客户端 ID
凭据,并将凭据另存为本地目录中名为
credentials.json的 JSON 文件。 - 如需以 Chat 应用身份进行身份验证,
请创建服务账号
凭据,并将凭据另存为名为
credentials.json的 JSON 文件。
- 如需以 Chat 用户身份进行身份验证,
请创建 OAuth 客户端 ID
凭据,并将凭据另存为本地目录中名为
- 根据您希望以用户身份还是 Chat 应用 身份进行身份验证,选择授权范围。
Python
- 拥有 Google Workspace访问权限的 Business 或 Enterprise 账号,可访问 Google Chat。
- 设置环境:
- 创建 Google Cloud 项目。
- 配置 OAuth 权限请求页面。
- 启用并配置 Google Chat API,为 Chat 应用设置名称、 图标和说明。
- 安装 Python 版 Cloud 客户端库。
- 根据您希望在 Google Chat API
请求中进行身份验证的方式创建访问凭据:
- 如需以 Chat 用户身份进行身份验证,
请创建 OAuth 客户端 ID
凭据,并将凭据另存为本地目录中名为
credentials.json的 JSON 文件。 - 如需以 Chat 应用身份进行身份验证,
请创建服务账号
凭据,并将凭据另存为名为
credentials.json的 JSON 文件。
- 如需以 Chat 用户身份进行身份验证,
请创建 OAuth 客户端 ID
凭据,并将凭据另存为本地目录中名为
- 根据您希望以用户身份还是 Chat 应用 身份进行身份验证,选择授权范围。
列出空间事件(用户身份验证)
如需列出 Chat 聊天室中的空间事件,请在请求中传递以下内容:
指定一个或多个授权范围,以支持请求中的每种事件类型。最佳做法是,选择限制性最强的范围,但仍允许应用正常运行。如需选择范围,请参阅 身份验证和授权概览。
调用
ListSpaceEvents()方法,并传递要列出的事件类型的filter。 您必须至少指定一种事件类型,还可以按日期进行过滤。 如需查看支持的事件类型列表,请参阅eventType资源SpaceEvent参考文档的 字段。
以下示例列出了有关空间中新成员资格和消息的事件:
Node.js
如需运行此示例,请将 SPACE_NAME 替换为空间
`name`
中的 IDname。
您可以通过调用
ListSpaces()
方法或从空间的网址获取 ID。
Chat API 会返回一个 分页列表,其中列出了有关新成员资格和消息的空间事件 。
列出空间事件(Chat 应用身份验证)
应用身份验证需要管理员一次性 批准。
如需使用 应用身份验证和 Chat REST API列出空间中的空间事件,请在请求中传递 以下内容:
- 指定一个或多个授权范围,以支持请求中的每种事件类型。最佳做法是,选择限制性最强的范围,但仍允许应用正常运行。如需详细了解如何选择范围,请参阅
身份验证和授权概览。
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
- 对
spaceEvents资源调用list方法 on the 。 - 传递要从中列出消息的空间的
name。 - 传递
filter以查询特定事件类型。
编写调用 Chat API 的脚本
以下介绍了如何使用 应用身份验证和 Chat REST API列出空间事件:
Python
- 在工作目录中,创建一个名为
chat_spaceevents_list_app.py的文件。 在
chat_spaceevents_list_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().list( # The space to list events 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. parent='spaces/SPACE_NAME', # A required filter. Filters events by event type. # # Update this filter to match your requirements. filter='eventTypes:"google.workspace.chat.message.v1.created"' ).execute() # Print Chat API's response in your command line interface. print(result) if __name__ == '__main__': main()在代码中,替换以下内容:
SPACE_NAME:空间名称,您可以从 Chat API 中的spaces.list方法 或从空间的网址获取该名称。
在工作目录中,构建并运行示例:
python3 chat_spaceevents_list_app.py
Chat API 会返回一个 分页列表,其中列出了有关新成员资格和消息的空间事件 。