从聊天室中移除成员

本指南介绍了如何使用 Google Chat API 的 Membership 资源中的 delete() 方法从聊天室中移除成员(也称为删除成员资格)。如果聊天室管理员是聊天室中唯一的聊天室管理员,则无法将其移除。请先将其他用户指定为聊天室管理员,然后再移除这些成员资格。

如果您是 Google Workspace 管理员,则可以从 Google Workspace 组织中的任何聊天室移除用户、Google 群组或 Chat 应用。

Membership 资源表示人工用户或 Google Chat 应用是否受邀加入聊天室、是否是聊天室的成员,或者是否不在聊天室中。

前提条件

Node.js

以用户身份从聊天室中移除成员

如需从启用用户身份验证的聊天室中移除用户、Google 群组或 Chat 应用,请在请求中传递以下内容:

  • 指定 chat.memberships 授权范围。 授权用户必须拥有从聊天室中移除用户或 Google 群组的权限。如需移除 Chat 应用,请指定 chat.memberships.app 授权范围(应用只能删除自己的会员资格,而不能删除其他应用的会员资格)。最佳实践是选择最严格的范围,同时仍允许应用正常运行。
  • 调用 DeleteMembership() 方法。
  • 传递要删除的会员的 name。如果相应成员是聊天室中唯一的聊天室管理员,请先将其他用户指定为聊天室管理员,然后再删除相应成员。

以下是使用用户身份验证删除会员资格的方法:

Node.js

chat/client-libraries/cloud/delete-space-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';  const USER_AUTH_OAUTH_SCOPES = ['https://www.googleapis.com/auth/chat.delete'];  // This sample shows how to delete a space with user credential async function main() {   // Create a client   const chatClient = await createClientWithUserCredentials(     USER_AUTH_OAUTH_SCOPES,   );    // Initialize request argument(s)   const request = {     // Replace SPACE_NAME here     name: 'spaces/SPACE_NAME',   };    // Make the request   const response = await chatClient.deleteSpace(request);    // Handle the response   console.log(response); }  await main();

如需运行此示例,请替换以下内容:

  • 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

  1. 在工作目录中,创建一个名为 chat_membership_delete_app.py 的文件。
  2. 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() 
  3. 在代码中,替换以下内容:

    • SPACE:聊天室名称,您可以通过 Chat API 中的 spaces.list 方法或从聊天室的网址中获取该名称。

    • MEMBER:会员名称,您可以通过 Chat API 中的 spaces.members.list 方法获取该名称。如需删除应用的会员资格,请将 MEMBER 替换为 app

  4. 在工作目录中,构建并运行示例:

    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 管理员身份调用此方法,请执行以下操作:

  • 使用用户身份验证调用该方法,并指定支持使用管理员权限调用该方法的授权范围
  • 在请求中,将查询参数 useAdminAccess 指定为 true

如需了解详情和示例,请参阅以 Google Workspace 管理员身份管理 Google Chat 聊天室

限制和注意事项

  • 借助应用身份验证,Chat 应用可以移除用户,但不能移除 Google 群组或 Chat 应用。