本指南介绍了如何对以下资源的 Space
资源使用 delete
方法:
使用 Google Chat API 删除不再需要的已命名聊天室。删除
也会删除其中包含的所有内容,包括消息和
附件。
通过
Space
资源
代表用户和 Chat 扩展应用能够发送消息的位置,
共享文件和协作聊天室分为以下几种类型:
- 私信 (DM) 是指两位用户或一位用户与 Chat 应用。
- 群聊是三位或更多用户之间的对话, 聊天应用。
- 已命名的聊天室是用户永久保留的存储位置,供他们发送消息、共享文件 和协作。
前提条件
Python
- Python 3.6 或更高版本
- pip 软件包管理工具
适用于 Python 的最新 Google 客户端库。若要安装或更新这些应用 在命令行界面中运行以下命令:
pip3 install --upgrade google-api-python-client google-auth-oauthlib
- 一个已启用并配置了 Google Chat API 的 Google Cloud 项目。有关具体步骤,请参阅 构建 Google Chat 应用。
为 Chat 应用配置的授权。正在删除 创建聊天室 用户身份验证 在拥有权限的用户那里获得
chat.delete
授权范围 删除指定的聊天室。
Node.js
- Node.js 和npm
最新的 Node.js 版 Google 客户端库。如需安装它们,请运行 在命令行界面中运行以下命令:
npm install @google-cloud/local-auth @googleapis/chat
- 一个已启用并配置了 Google Chat API 的 Google Cloud 项目。有关具体步骤,请参阅 构建 Google Chat 应用。
为 Chat 应用配置的授权。正在删除 创建聊天室 用户身份验证 在拥有权限的用户的
chat.delete
授权范围内 删除指定的聊天室。
删除已命名的聊天室
如需在 Google Chat 中删除现有聊天室,请传递以下内容 :
如需删除聊天室,请按以下步骤操作:
Python
- 在您的工作目录中,创建一个名为
chat_space_delete.py
的文件。 在
chat_space_delete.py
中添加以下代码:from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.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.delete"] def main(): ''' Authenticates with Chat API via user credentials, then deletes the specified space. ''' # Authenticate with Google Workspace # and get user authorization. flow = InstalledAppFlow.from_client_secrets_file( 'client_secrets.json', SCOPES) creds = flow.run_local_server() # 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()
在代码中,将
SPACE
替换为聊天室名称。 您可以从spaces.list
方法(在 Chat API,或通过聊天室网址添加。在您的工作目录中,构建并运行该示例:
python3 chat_space_delete.py
Node.js
- 在您的工作目录中,创建一个名为
delete-space.js
的文件。 在
delete-space.js
中添加以下代码:const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Deletes a Chat space. * @return {!Promise<!Object>} */ async function deleteSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.delete', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.delete({name: 'spaces/SPACE'}); } deleteSpace().then(console.log);
在代码中,将
SPACE
替换为聊天室名称, 您可以从spaces.list
方法 或通过聊天室网址发送。在您的工作目录中,运行该示例:
node delete-space.js
如果成功,响应正文为空,这表示该空格 已删除。