このガイドでは、Google Chat API の membership
リソースの get
メソッドを使用して、スペース内のユーザーまたは Chat アプリのメンバーシップの詳細を取得する方法について説明します。
Membership
リソースは、人間のユーザーまたは Google Chat アプリがスペースに招待されているか、スペースに含まれているか、またはスペースから退出しているかを表します。
アプリの認証で認証すると、Chat アプリは、Google Chat でアクセスできるスペース(メンバーとなっているスペースなど)からメンバーシップを取得できますが、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 アプリのメンバーシップに関する詳細情報を取得する
Google Chat のメンバーシップの詳細を取得するには、リクエストに次のものを渡します。
- アプリ認証で、
chat.bot
承認スコープを指定します。ユーザー認証で、chat.memberships.readonly
またはchat.memberships
承認スコープを指定します。アプリを機能させるため、最も制限の厳しいスコープを選択することをおすすめします。 membership
リソースでget
メソッドを呼び出します。- 取得するメンバーシップの
name
を渡します。Google Chat のメンバーシップ リソースからメンバーシップ名を取得します。
ユーザー認証を使用してメンバーシップを取得する方法は次のとおりです。
Python
- 作業ディレクトリに、
chat_membership_get.py
という名前のファイルを作成します。 chat_membership_get.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.memberships.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then gets details about a specified membership. ''' # 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().members().get( # The membership 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. # # Replace MEMBER with a membership name. # Obtain the membership name from the memberships resource of # Chat API. name='spaces/SPACE/members/MEMBER' ).execute() # Prints details about the membership. print(result) if __name__ == '__main__': main()
コードで、次のように置き換えます。
SPACE
: スペース名。Chat API のspaces.list
メソッドまたはスペースの URL から取得できます。MEMBER
: メンバーシップ名。Chat API のspaces.members.list
メソッドから取得できます。
作業ディレクトリで、サンプルをビルドして実行します。
python3 chat_membership_get.py
Chat API は、指定されたメンバーシップの詳細を含む membership
のインスタンスを返します。
関連トピック
- スペースのメンバーを一覧表示する。
- ユーザーまたは Chat アプリをスペースに招待または追加する。
- Google Chat スペースでユーザーのメンバーシップを更新する。
- スペースからユーザーまたは Chat アプリを削除する