本指南介绍了如何对 Google Chat API 的 Section 资源使用
patch
方法,以更新 Google Chat 中的自定义栏目。
只有类型为 CUSTOM_SECTION 的部分可以更新。如需了解详情,请参阅
在 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授权范围。 - 调用
UpdateSection方法。 - 在请求正文中,提供
Section资源和字段掩码:- 将要更新的部分的
name设置为。 - 将
displayName设置为该部分的新名称。 - 将
updateMask设置为displayName。
- 将要更新的部分的
以下示例会更新一个部分:
Python
from google.cloud import chat_v1
from google.protobuf import field_mask_pb2
def update_section():
# Create a client
client = chat_v1.ChatServiceClient()
# Initialize request
request = chat_v1.UpdateSectionRequest(
section=chat_v1.Section(
name="SECTION_NAME",
display_name="NEW_SECTION_DISPLAY_NAME"
),
update_mask=field_mask_pb2.FieldMask(paths=["display_name"])
)
# Make the request
response = client.update_section(request=request)
print(response)
如需运行此示例,请替换以下内容:
SECTION_NAME:部分的资源名称。您可以通过调用ListSections方法获取 资源名称。NEW_SECTION_DISPLAY_NAME:该部分的新名称。
Chat API 会返回更新后的
Section实例。