本指南介绍了如何使用 Google Chat API 的 Membership
资源中的 update()
方法来更改成员资格的相关属性,例如将聊天室成员更改为聊天室管理员,或将聊天室管理员更改为聊天室成员。
如果您是 Google Workspace 管理员,可以调用 update()
方法来更新 Google Workspace 组织中任何聊天室的成员资格。
Membership
资源表示人工用户或 Google Chat 应用是否受邀加入聊天室、是否是聊天室的成员,或者是否不在聊天室中。
前提条件
Node.js
- 拥有可访问 Google Chat 的 Google Workspace 商务版或企业版账号。
- 设置环境:
- 创建 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 应用身份进行身份验证。
更新会员资格
如需更新空间会员资格,请在请求中传递以下内容:
- 指定授权范围:
- 调用
UpdateMembership()
方法。 - 将
membership
作为Membership
的实例传递,并包含以下内容:- 要更新的成员资格的
name
字段,包括聊天室 ID 和成员 ID。 - 要更新的会员资格字段设置为新值。
- 要更新的成员资格的
- 传递
updateMask
以指定要更新的会员资格方面,其中包括以下内容:role
:用户在 Chat 聊天室中的角色,决定了用户在聊天室中可执行的操作。可能的值包括:ROLE_MEMBER
:聊天室的成员。用户拥有基本权限,例如向聊天室发送消息。在 1 对 1 对话和未命名的群组对话中,所有人都有此角色。ROLE_MANAGER
:聊天室管理员。用户拥有所有基本权限,以及允许其管理聊天室(例如添加或移除成员)的管理权限。仅在spaceType
为SPACE
(已命名的聊天室)的聊天室中受支持。
以用户身份将普通聊天室成员设为聊天室管理员
以下示例使用用户身份验证调用 Chat API,通过将 role
指定为 ROLE_MANAGER
,将常规空间成员设为空间管理员:
Node.js
如需运行示例,请替换以下内容:
SPACE_NAME
:来自空间的name
的 ID。 您可以通过调用ListSpaces()
方法或从空间的网址中获取 ID。MEMBER_NAME
:会员资格的name
中的 ID。 您可以通过调用ListMemberships()
方法获取 ID,也可以通过使用 Chat API 异步创建会员资格后返回的响应正文获取 ID。ROLE_NAME
:更新后的角色,ROLE_MANAGER
。
Google Chat API 会将指定成员资格更新为聊天室管理员,并返回 Membership
的实例。
以用户身份将聊天室管理员设为普通成员
以下示例使用用户身份验证调用 Chat API,通过将 role
指定为 ROLE_MEMBER
,使空间管理员成为常规空间成员:
Node.js
如需运行示例,请替换以下内容:
SPACE_NAME
:来自空间的name
的 ID。 您可以通过调用ListSpaces()
方法或从空间的网址中获取 ID。MEMBER_NAME
:会员资格的name
中的 ID。 您可以通过调用ListMemberships()
方法获取 ID,也可以通过使用 Chat API 异步创建会员资格后返回的响应正文获取 ID。ROLE_NAME
:更新后的角色,ROLE_MEMBER
。
Google Chat API 会将指定成员资格更新为聊天室管理员,并返回 Membership
的实例。
以 Chat 应用身份将常规聊天室成员设为聊天室管理员
应用身份验证需要管理员一次性批准。
编写一个调用 Chat API 的脚本
以下示例使用应用身份验证调用 Chat API,通过在 body
中将 role
指定为 ROLE_MANAGER
来将常规空间成员设为空间管理员,该 body
用于指定更新后的成员资格属性:
Python
- 在工作目录中,创建一个名为
chat_membership_update_to_manager_app.py
的文件。 在
chat_membership_update_to_manager_app.py
中添加以下代码:from google.oauth2 import service_account from apiclient.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.app.memberships"] def main(): ''' Authenticates with Chat API using app authentication, then updates a specified space member to change it from a regular member to a space manager. ''' # 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().members().patch( # The membership to update, and the updated role. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. # # Replace MEMBERSHIP with a membership name. # Obtain the membership name from the membership of Chat API. name='spaces/SPACE/members/MEMBERSHIP', updateMask='role', body={'role': 'ROLE_MANAGER'} ).execute() # Prints details about the updated membership. print(result) if __name__ == '__main__': main()
在代码中,替换以下内容:
SPACE
:聊天室名称,您可以通过 Chat API 中的spaces.list
方法或从聊天室的网址中获取。MEMBERSHIP
:会员名称,您可以通过 Chat API 中的spaces.members.list
方法获取该名称。
在工作目录中,构建并运行示例:
python3 chat_membership_update_to_manager_app.py
以 Chat 应用的身份将聊天室管理员设为普通成员
应用身份验证需要管理员一次性批准。
编写一个调用 Chat API 的脚本
以下示例使用应用身份验证调用 Chat API,通过在指定更新后的会员属性的 body
中将 role
指定为 ROLE_MEMBER
,使空间管理员成为常规空间成员:
Python
- 在工作目录中,创建一个名为
chat_membership_update_to_member_app.py
的文件。 在
chat_membership_update_to_member_app.py
中添加以下代码:from google.oauth2 import service_account from apiclient.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.app.memberships"] def main(): ''' Authenticates with Chat API via user credentials, then updates a specified space member to change it from a regular member to a space manager. ''' # 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().members().patch( # The membership to update, and the updated role. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. # # Replace MEMBERSHIP with a membership name. # Obtain the membership name from the membership of Chat API. name='spaces/SPACE/members/MEMBERSHIP', updateMask='role', body={'role': 'ROLE_MEMBER'} ).execute() # Prints details about the updated membership. print(result) if __name__ == '__main__': main()
在代码中,替换以下内容:
SPACE
:聊天室名称,您可以通过 Chat API 中的spaces.list
方法或从聊天室的网址中获取。MEMBERSHIP
:会员名称,您可以通过 Chat API 中的spaces.members.list
方法获取该名称。
在工作目录中,构建并运行示例:
python3 chat_membership_update_to_member_app.py
以 Google Workspace 管理员身份更新成员资格
如果您是 Google Workspace 管理员,可以调用 update()
方法来更新 Google Workspace 组织中任何聊天室的成员资格。
如需以 Google Workspace 管理员身份调用此方法,请执行以下操作:
如需了解详情和示例,请参阅以 Google Workspace 管理员身份管理 Google Chat 聊天室。
相关主题
- 邀请用户或 Google Chat 应用加入聊天室,或将用户或 Google Chat 应用添加到聊天室。
- 获取用户或 Chat 应用的会员资格详情。
- 列出聊天室中的成员。
- 从聊天室中移除用户或 Chat 应用。