スペースからメンバーを削除する

このガイドでは、membership リソースで delete メソッドを使用する方法について説明します スペースからメンバーを削除できます。「スペースから削除」も できます。スペースの管理者のみである場合、その管理者は削除できません 方法について説明します。これらを削除する前に、別のユーザーをスペースの管理者として割り当ててください できます。

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 の承認スコープ 権限のあるユーザーを指定できます。

スペースからメンバーを削除する

スペースからユーザー、Google グループ、Chat 用アプリを削除するには、 スペース:

  • ユーザーまたは Google グループを削除するには、chat.memberships 認証を指定します。 あります。Chat 用アプリを削除するには、 chat.memberships.app 承認スコープ(アプリは自身のスコープのみを削除可能) メンバーシップ。ベスト プラクティスとして、 範囲が制限されます。
  • delete メソッドを呼び出す 日付 membership リソース
  • 削除するメンバーシップの 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: スペース名。 spaces.list メソッド スペースの URL から取得できます。

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

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

    python3 chat_membership_delete.py
    

成功すると、レスポンスの本文ではメンバーシップと 'state': 'NOT_A_MEMBER' は、メンバーがスペースからなくなったことを示します。

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