ユーザーまたは Google Chat アプリをスペースから削除する

このガイドでは、Google Chat API の membership リソースの delete メソッドを使用して、スペースからユーザーまたは Chat アプリを削除する方法について説明します(メンバーシップの削除とも呼ばれます)。スペース内の唯一のスペース管理者であるスペース管理者は削除できません。メンバーシップを削除する前に、別のユーザーをスペースの管理者に割り当ててください。

Membership リソースは、人間のユーザーまたは Google 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.memberships または chat.memberships.app 承認スコープを持つユーザー認証が必要です。

スペースからユーザーまたは Chat アプリを削除する

スペースからユーザーまたは Chat アプリを削除するには:

  • ユーザーを削除するには、chat.memberships 承認スコープを指定します。Chat アプリを削除するには、chat.memberships.app 承認スコープを指定します(アプリが削除できるのは自分のメンバーシップのみであり、他のアプリのメンバーシップは削除できません)。アプリを機能させる最も制限の厳しいスコープを選択することをおすすめします。
  • membership リソースdelete メソッドを呼び出します。
  • 削除するメンバーシップの name を渡します。メンバーシップがスペース内の唯一のスペース管理者に属している場合は、このメンバーシップを削除する前に、別のユーザーをスペース管理者として割り当ててください。

メンバーシップを削除する方法は次のとおりです。

Python

  1. 作業ディレクトリに、chat_membership_delete.py という名前のファイルを作成します。
  2. chat_membership_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.memberships.app"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then deletes the 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().delete(
    
            # The membership 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.
            #
            # Replace MEMBER with a membership name.
            # Obtain the membership name from the memberships resource of
            # Chat API. To delete a Chat app's membership, replace MEMBER
            # with app; an alias for the app calling the API.
            name='spaces/SPACE/members/MEMBER'
    
        ).execute()
    
        # Print Chat API's response in your command line interface.
        # When deleting a membership, the response body is empty.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. コードで、次のように置き換えます。

    • SPACE: スペース名。Chat API の spaces.list メソッドまたはスペースの URL から取得できます。

    • MEMBER: メンバーシップ名。Chat API の spaces.members.list メソッドから取得できます。アプリのメンバーシップを削除するには、MEMBERapp に置き換えます。

  4. 作業ディレクトリで、サンプルをビルドして実行します。

    python3 chat_membership_delete.py
    

成功すると、レスポンスの本文で 'state': 'NOT_A_MEMBER' を含むメンバーシップが返されます。これは、そのメンバーがスペースに存在しなくなったことを示します。

{
    "name": "spaces/SPACE/members/MEMBER",
    "state": "NOT_A_MEMBER"
}