本指南介绍了如何使用 Google Chat API 的 SectionItem 资源中的
list
方法列出部分中的项(例如空间)。
只有空间可以作为部分项。如需了解详情,请参阅 在 Google Chat 中创建和整理部分。
前提条件
Python
- 拥有 Google Workspace访问权限的 Business 或 Enterprise 账号,可访问 Google Chat。
- 设置环境:
- 创建 Google Cloud 项目。
- 配置 OAuth 权限请求页面。
- 为 Chat 应用启用并配置 Google Chat API,包括名称、 图标和说明。
- 安装 Python Cloud 客户端库。
-
为桌面应用创建 OAuth 客户端 ID 凭据。如需运行本
指南中的示例,请将凭据另存为名为
credentials.json的 JSON 文件,并保存到本地 目录中。
- 选择支持用户身份验证的授权范围。
列出部分中的空间
如需列出部分中经过 用户身份验证的空间,请在请求中传递以下内容:
- 指定
chat.users.sections或chat.users.sections.readonly授权范围。 - 调用
ListSectionItems方法。 - 将
parent设置为部分的资源名称。
以下示例列出了部分中的空间:
Python
from google.cloud import chat_v1
def list_section_items():
# Create a client
client = chat_v1.ChatServiceClient()
# Initialize request
request = chat_v1.ListSectionItemsRequest(
parent="SECTION_NAME"
)
# Make the request
page_result = client.list_section_items(request=request)
# Handle the response
for item in page_result:
print(item)
如需运行此示例,请替换以下内容:
SECTION_NAME:部分的资源名称。您可以通过调用ListSections方法获取 资源名称。
Chat API 会返回
SectionItem 资源列表。