本指南介绍了如何使用 Google Chat API 的 Membership
资源中的 delete()
方法从聊天室中移除成员(也称为删除成员资格)。如果聊天室管理员是聊天室中唯一的管理员,则无法移除该管理员。请先将其他用户指定为聊天室管理员,然后再移除这些成员资格。
如果您是 Google Workspace 管理员,则可以从 Google Workspace 组织中的任何聊天室移除用户、Google 群组或 Chat 应用。
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 应用身份进行身份验证。
以用户身份从聊天室中移除成员
如需从具有用户身份验证的聊天室中移除用户、Google 群组或 Chat 应用,请在请求中传递以下内容:
- 指定
chat.memberships
授权范围。 授权用户必须拥有从聊天室中移除用户或 Google 群组的权限。如需移除 Chat 应用,请指定chat.memberships.app
授权范围(应用只能删除自己的会员资格,而不能删除其他应用的会员资格)。最佳实践是选择最严格的范围,同时确保应用仍能正常运行。 - 调用
DeleteMembership()
方法。 - 传递要删除的会员的
name
。如果相应成员是聊天室中唯一的聊天室管理员,请先将其他用户分配为聊天室管理员,然后再删除相应成员。
以下是使用用户身份验证删除会员资格的方法:
Node.js
如需运行此示例,请替换以下内容:
SPACE_NAME
:来自空间name
的 ID。 您可以通过调用ListSpaces()
方法或从会议室的网址中获取 ID。MEMBER_NAME
:来自成员的name
的 ID。 您可以通过调用ListMemberships()
方法来获取 ID。
如果成功,响应正文会返回 'state': 'NOT_A_MEMBER'
的会员资格,表明相应会员已不再是相应会议室的成员。
{ "name": "spaces/SPACE_NAME/members/MEMBER_NAME", "state": "NOT_A_MEMBER" }
以 Chat 应用的身份从聊天室中移除成员
应用身份验证需要管理员一次性批准。
如需从启用应用身份验证的聊天室中移除用户、Google 群组或 Chat 应用,请在请求中传递以下内容:
- 指定
chat.app.memberships
授权范围。仅在 Chat 应用创建的聊天室中支持删除聊天室管理员的成员身份。 - 对
membership
资源调用delete
方法。 - 传递要删除的会员的
name
。如果相应成员是聊天室中唯一的聊天室管理员,请先将其他用户分配为聊天室管理员,然后再删除相应成员。
编写调用 Chat API 的脚本
如需删除通过应用身份验证购买的会员资格,请按以下步骤操作:
Python
- 在工作目录中,创建一个名为
chat_membership_delete_app.py
的文件。 在
chat_membership_delete_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 deletes the specified membership. ''' # 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().delete( # The membership to delete. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. # # Replace MEMBER with a membership name. # Obtain the membership name from the memberships resource of # Chat API. To delete a Chat app's membership, replace MEMBER # with app; an alias for the app calling the API. name='spaces/SPACE/members/MEMBER' ).execute() # Print Chat API's response in your command line interface. # When deleting a membership, the response body is empty. print(result) if __name__ == '__main__': main()
在代码中,替换以下内容:
SPACE
:聊天室名称,您可以通过 Chat API 中的spaces.list
方法或从聊天室的网址中获取该名称。MEMBER
:会员名称,您可以通过 Chat API 中的spaces.members.list
方法获取。如需删除应用的会员资格,请将MEMBER
替换为app
。
在工作目录中,构建并运行示例:
python3 chat_membership_delete_app.py
如果成功,响应正文会返回 'state': 'NOT_A_MEMBER'
的会员资格,表明相应会员已不再是相应会议室的成员。
{ "name": "spaces/SPACE/members/MEMBER", "state": "NOT_A_MEMBER" }
以 Google Workspace 管理员身份从聊天室中移除用户或 Google 群组
如果您是 Google Workspace 管理员,可以调用 DeleteMembership()
方法从 Google Workspace 组织中的任何聊天室移除用户、Google 群组或 Chat 应用。
如需以 Google Workspace 管理员身份调用此方法,请执行以下操作:
如需了解详情和示例,请参阅以 Google Workspace 管理员身份管理 Google Chat 聊天室。
限制和注意事项
- 借助应用身份验证,Chat 应用可以移除用户,但不能移除 Google 群组或 Chat 应用。
相关主题
- 获取用户或 Chat 应用的会员资格详情。
- 列出聊天室中的成员。
- 更新用户在 Google Chat 聊天室中的成员资格。
- 邀请用户或 Chat 应用加入聊天室,或将用户或 Chat 应用添加到聊天室。