本指南介绍了如何使用 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 聊天室中的角色,决定了用户在聊天室中可执行的操作。如需详细了解权限,请参阅 Chat API 参考文档中的MembershipRole。 可能的值包括:ROLE_MEMBER:聊天室的成员。在 Chat 界面中,此角色称为成员。ROLE_ASSISTANT_MANAGER:聊天室管理员。在聊天界面中,此角色称为管理员。ROLE_MANAGER:空间所有者。在聊天界面中,此角色称为所有者。
将成员更改为所有者(用户身份验证)
以下示例使用用户身份验证调用 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。 您可以将此值设置为MembershipRole的任意值。例如,如需将普通成员设为聊天室管理员,请将ROLE_NAME更改为ROLE_ASSISTANT_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,使常规空间成员成为空间所有者:
Python
- 在工作目录中,创建一个名为
chat_membership_update_to_owner_app.py的文件。 在
chat_membership_update_to_owner_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 owner. ''' # 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', # Replace ROLE with a MembershipRole value. # Obtain the MembershipRole values from the membership of Chat API. body={'role': 'ROLE'} ).execute() # Prints details about the updated membership. print(result) if __name__ == '__main__': main()在代码中,替换以下内容:
SPACE:聊天室名称,您可以通过 Chat API 中的spaces.list方法或从聊天室的网址中获取。MEMBERSHIP:会员名称,您可以通过 Chat API 中的spaces.members.list方法获取该名称。ROLE:更新后的角色,ROLE_MANAGER。 您可以将此值设置为MembershipRole的任意值。例如,如需将普通成员设为聊天室管理员,请将ROLE更改为ROLE_ASSISTANT_MANAGER。
在工作目录中,构建并运行示例:
python3 chat_membership_update_to_owner_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 owner to change it to a regular member. ''' # 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 应用。