列出聊天室中的成員

本指南說明如何針對 membership 資源使用 list 方法 使用 Google Chat API,以分頁形式列出聊天室中的成員 成為頻道會員的一分子透過以下角色列出成員: 應用程式驗證 列出 Chat 應用程式擁有的聊天室 可存取 (不含 Chat 應用程式成員資格) 的存取權,但不包括 基礎架構透過以下角色列出成員: 使用者驗證 列出已驗證使用者可存取的聊天室中的成員資格。

Membership 項資源 代表受邀參加的使用者或 Google Chat 應用程式 屬於或不存在於空格中。

必要條件

Python

  • Python 3.6 以上版本
  • pip 套件管理工具
  • 最新的 Google 用戶端程式庫。安裝或更新 在指令列介面中執行下列指令:
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

透過使用者驗證機制列出聊天室中的成員

如何在聊天室中列出使用者、Google 網路論壇和 Chat 應用程式 且已驗證使用者可存取,並在要求中傳遞以下指令:

下列範例會列出 Google 群組、真人成員和應用程式成員,對象包括: 通過驗證的使用者

Python

  1. 在工作目錄中,建立名為 chat_member_list_user.py 的檔案。
  2. chat_member_list_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.memberships.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then lists Google Group, human, and app members in 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().members().list(
    
            # The space for which to list memberships.
            parent = 'spaces/SPACE',
    
            # Set this parameter to list Google Groups.
            showGroups = 'true'
    
        ).execute()
    
        # Prints the list of memberships.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 將程式碼中的 SPACE 替換成空格名稱, 您可以從中取得 spaces.list 方法 或聊天室網址傳送

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

    python3 chat_member_list_user.py
    

Google Chat API 會傳回 Google 網路論壇、人工和應用程式成員的清單 在指定的空間中運作

透過應用程式驗證功能列出聊天室成員

如何列出聊天室中的使用者和 Chat 應用程式 已通過驗證的應用程式可存取,請在要求中傳遞下列資訊:

以下範例列出了可檢視的人類聊天室成員 (非聊天室管理員),以及 執行下列操作:

Python

  1. 在工作目錄中,建立名為 chat_member_list_app.py 的檔案。
  2. chat_member_list_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().members().list(
    
            # The space for which to list memberships.
            parent = 'spaces/SPACE',
    
            # An optional filter that returns only human space members.
            filter = 'member.type = "HUMAN" AND role = "ROLE_MEMBER"'
    
        ).execute()
    
    print(result)
    
  3. 將程式碼中的 SPACE 替換成空格名稱, 您可以從中取得 spaces.list 方法 或聊天室網址傳送

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

    python3 chat_member_list_app.py
    

Google Chat API 會傳回聊天室成員清單 (不含聊天室) 指派的用戶端 ID。

自訂分頁或篩選清單

如要列出成員資格,請將下列查詢參數傳送至 自訂或篩選已列出成員資格的分頁:

  • pageSize:傳回的會員數量上限。服務可能會 傳回的值小於此值如果未指定,則最多有 100 個空格 。最大值是 1,000;超過 1,000 個價值時 將變更為 1,000
  • pageToken:從先前的 list_space 呼叫接收的頁面符記。 提供這個權杖即可擷取後續網頁。進行分頁時, 篩選器值應與提供網頁符記的呼叫相符。傳送 否則可能會導致非預期的結果
  • filter:查詢篩選器。必要條件 使用者驗證。 如要進一步瞭解支援的查詢詳細資料,請參閱 spaces.members.list 方法