このガイドでは、Google Chat API の Space
リソースの get
メソッドを使用して、スペースの詳細(表示名、説明、ガイドラインなど)を確認する方法について説明します。
Space
リソースは、ユーザーと Chat アプリがメッセージの送信、ファイル共有、コラボレーションを行う場所を表します。スペースには次の種類があります。
- ダイレクト メッセージ(DM)は、2 人のユーザー間の会話、またはユーザーと Chat アプリ間の会話です。
- グループ チャットとは、3 人以上のユーザーと Chat アプリの間で交わされる会話のことです。
- 名前付きスペースは、メッセージの送信、ファイルの共有、共同編集を行う永続的な場所です。
アプリの認証で認証すると、Chat アプリが Google Chat 内でアクセスできるスペース(アプリがメンバーになっているスペースなど)を取得できます。ユーザー認証で認証すると、認証されたユーザーがアクセスできるスペースを取得できます。
前提条件
Python
- Python 3.6 以降
- pip パッケージ管理ツール
Python 用の最新の Google クライアント ライブラリ。これらをインストールまたは更新するには、コマンドライン インターフェースで次のコマンドを実行します。
pip3 install --upgrade google-api-python-client google-auth-oauthlib google-auth
- Google Chat API が有効で構成された Google Cloud プロジェクト。手順については、Google Chat アプリを作成するをご覧ください。
Chat アプリに対して認可が構成されています。スペースを取得するには、次の両方の認証方法がサポートされます。
Node.js
- Node.js と npm
Node.js 用の最新の Google クライアント ライブラリ。これらをインストールするには、コマンドライン インターフェースで次のコマンドを実行します。
npm install @google-cloud/local-auth @googleapis/chat
- Google Chat API が有効で構成された Google Cloud プロジェクト。手順については、Google Chat アプリを作成するをご覧ください。
Chat アプリに対して認可が構成されています。スペースを取得するには、次の両方の認証方法がサポートされます。
スペースを取得
Google Chat でスペースを取得するには、リクエストに次の内容を渡します。
- アプリ認証で、
chat.bot
承認スコープを指定します。ユーザー認証で、chat.spaces.readonly
またはchat.spaces
承認スコープを指定します。 Space
リソースでget
メソッドを呼び出し、取得するスペースのname
を渡します。スペース名は Google Chat のスペース リソースまたはスペースの URL から取得します。
ユーザー認証を使用してスペースの詳細を取得する
ユーザー認証を使用してスペースの詳細を取得する方法は次のとおりです。
Python
- 作業ディレクトリに、
chat_space_get_user.py
という名前のファイルを作成します。 chat_space_get_user.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.spaces.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then gets details about a 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().get( # The space to get. # # 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() # Prints details about the space. print(result) if __name__ == '__main__': main()
コードで
SPACE
をスペース名に置き換えます。スペース名は、Chat API のspaces.list
メソッドまたはスペースの URL から取得できます。作業ディレクトリで、サンプルをビルドして実行します。
python3 chat_space_get_user.py
Node.js
- 作業ディレクトリに、
get-space.js
という名前のファイルを作成します。 get-space.js
に次のコードを追加します。const chat = require('@googleapis/chat'); const {authenticate} = require('@google-cloud/local-auth'); /** * Gets details about a Chat space by name. * @return {!Object} */ async function getSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.spaces.readonly', ]; const authClient = await authenticate({scopes, keyfilePath: 'client_secrets.json'}); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.get({name: 'spaces/SPACE'}); } getSpace().then(console.log);
コードで
SPACE
をスペース名に置き換えます。スペース名は、Chat API のspaces.list
メソッドまたはスペースの URL から取得できます。作業ディレクトリでサンプルを実行します。
node get-space.js
Chat API は、指定されたスペースの詳細を示す Space
のインスタンスを返します。
アプリの認証でスペースの詳細を取得する
アプリ認証を使用してスペースの詳細を取得する方法は次のとおりです。
Python
- 作業ディレクトリに、
chat_space_get_app.py
という名前のファイルを作成します。 chat_space_get_app.py
に次のコードを追加します。from google.oauth2 import service_account from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ( service_account.Credentials.from_service_account_file('credentials.json') .with_scopes(SCOPES) ) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', credentials=CREDENTIALS) # Use the service endpoint to call Chat API. result = chat.spaces().get( # The space to get. # # 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(result)
コードで
SPACE
をスペース名に置き換えます。スペース名は、Chat API のspaces.list()
メソッドまたはスペースの URL から取得できます。作業ディレクトリで、サンプルをビルドして実行します。
python3 chat_space_get_app.py
Node.js
- 作業ディレクトリに、
app-get-space.js
という名前のファイルを作成します。 app-get-space.js
に次のコードを追加します。const chat = require('@googleapis/chat'); /** * Gets details about a Chat space by name. * @return {!Promise<!Object>} */ async function getSpace() { const scopes = [ 'https://www.googleapis.com/auth/chat.bot', ]; const auth = new chat.auth.GoogleAuth({ scopes, keyFilename: 'credentials.json', }); const authClient = await auth.getClient(); const chatClient = await chat.chat({version: 'v1', auth: authClient}); return await chatClient.spaces.get({name: 'spaces/SPACE'}); } getSpace().then(console.log);
コードで
SPACE
をスペース名に置き換えます。スペース名は、Chat API のspaces.list
メソッドまたはスペースの URL から取得できます。作業ディレクトリでサンプルを実行します。
node app-get-space.js
Chat API は、指定されたスペースの詳細を示す Space
のインスタンスを返します。