删除聊天室

本指南介绍了如何使用 Google Chat API 的 Space 资源中的 delete() 方法在不再需要命名聊天室时将其删除。删除聊天室还会删除其中包含的所有内容,包括消息和附件。

如果您是 Google Workspace 管理员,可以调用 delete() 方法来删除 Google Workspace 组织中的任何命名聊天室。

Space 资源表示用户和 Chat 应用可以在其中发送消息、共享文件和协作处理事务。聊天室有多种类型:

  • 私信 (DM) 是两位用户之间或用户与 Chat 应用之间的对话。
  • 群聊是指三位或更多用户与聊天应用之间的对话。
  • 命名聊天室是持久存在的聊天室,用户可以在其中发送消息、分享文件和协作。

前提条件

Node.js

以用户身份删除命名空间

如需在 Google Chat 中删除具有用户身份验证的现有聊天室,请在请求中传递以下内容:

  • 指定 chat.delete 授权范围。
  • 调用 DeleteSpace() 方法。
  • 传递要删除的聊天室的 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); }  main().catch(console.error);

如需运行此示例,请将 SPACE_NAME 替换为空间 name 字段中的 ID。您可以通过调用 ListSpaces() 方法或从空间的网址中获取 ID。

以 Chat 应用的身份删除已命名的聊天室

应用身份验证需要管理员一次性批准

使用应用身份验证时,您只能删除由 Chat 应用创建的聊天室。

如需在 Google Chat 中删除具有应用身份验证的现有聊天室,请在请求中传递以下内容:

编写调用 Chat API 的脚本

删除聊天室的方法如下:

Python

  1. 在工作目录中,创建一个名为 chat_space_delete_app.py 的文件。
  2. chat_space_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.delete"]  def main():     '''     Authenticates with Chat API using app authentication,     then deletes the specified space.     '''      # 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().delete(            # The space 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.           name='spaces/SPACE'        ).execute()      # Print Chat API's response in your command line interface.     # When deleting a space, the response body is empty.     print(result)  if __name__ == '__main__':     main() 
  3. 在代码中,替换以下内容:

    • SPACE,其中包含聊天室名称,您可以通过 Chat API 中的 spaces.list 方法或从聊天室的网址获取该名称。
  4. 在工作目录中,构建并运行示例:

    python3 chat_space_delete_app.py

如果成功,则响应正文为空,表示空间已删除。

以 Google Workspace 管理员身份删除已命名的聊天室

如果您是 Google Workspace 管理员,可以调用 DeleteSpace() 方法来删除 Google Workspace 组织中的任何命名空间。

如需以 Google Workspace 管理员身份调用此方法,请执行以下操作:

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

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