このガイドでは、Google Chat API の Space
リソースの delete
メソッドを使用して、不要になった名前付きスペースを削除する方法について説明します。スペースを削除すると、メッセージや添付ファイルなど、スペースに含まれるすべての内容も削除されます。
Space
リソースは、ユーザーと Chat アプリがメッセージの送信、ファイルの共有、コラボレーションを行う場所を表します。スペースにはいくつかのタイプがあります。
- ダイレクト メッセージ(DM)は、2 人のユーザー、またはユーザーと Chat アプリ間の会話です。
- グループ チャットとは、3 人以上のユーザーと 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 で既存のスペースを削除するには、リクエストに次の内容を渡します。
chat.delete
認可スコープを指定します。Space
リソースのdelete
メソッドを呼び出します。- 削除するスペースの
name
を渡します。
スペースを削除する方法は次のとおりです。
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
をスペース名に置き換えます。スペースは、Chat API のspaces.list
メソッドまたはスペースの URL から取得します。作業ディレクトリでサンプルをビルドして実行します。
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
をスペース名に置き換えます。スペースは、Chat API のspaces.list
メソッドまたはスペースの URL から取得します。作業ディレクトリでサンプルを実行します。
node delete-space.js
成功すると、レスポンスの本文は空になります。これは、スペースが削除されたことを示します。