本指南介绍了如何使用
update()
Google Chat API 的 Membership 资源中的方法来更改成员资格的相关属性,例如将聊天室成员更改为聊天室管理员或
聊天室所有者。
如果您是 Google Workspace 管理员,则可以调用 update() 方法来更新 Google Workspace 组织中任何聊天室的成员资格。
The
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 聊天室中的角色,决定了他们在聊天室中的允许操作。如需详细了解权限,请参阅MembershipRoleChat API 参考文档中的 。可能的值包括:ROLE_MEMBER:聊天室的成员。在 Chat 界面中,此角色称为成员 。ROLE_ASSISTANT_MANAGER:聊天室管理员。在 Chat 界面中,此角色称为管理员 。ROLE_MANAGER:聊天室所有者。在 Chat 界面中,此角色称为所有者 。
将成员更改为所有者(用户身份验证)
以下示例使用
用户身份验证
调用 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 会将指定的成员资格更新为聊天室所有者,并返回
an instance of
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 会将指定的成员资格更新为聊天室所有者,并返回
an instance of
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 应用。