從聊天室中移除成員

本指南說明如何針對 membership 資源使用 delete 方法 使用 Google Chat API 從聊天室移除成員,也就是刪除 會員。如果聊天室管理員是唯一的聊天室管理員,就無法移除管理員 空間請先指派其他使用者擔任聊天室管理員,再移除這些使用者 會員功能。

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.membershipschat.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 方法 或聊天室網址傳送

    • MEMBER:您可以取得的會員名稱 透過 spaces.members.list 方法 或是透過 Chat API 掌握這類模型如要刪除應用程式的會員資格,請將 app會員價 MEMBER

  4. 在工作目錄中建構並執行範例:

    python3 chat_membership_delete.py
    

如果成功,回應主體會傳回包含 'state': 'NOT_A_MEMBER':表示成員已不在聊天室中。

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