取得會員資格詳情

本指南說明如何針對 membership 資源使用 get 方法 取得聊天室成員詳細資料。

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

使用以下憑證進行驗證: 應用程式驗證 可讓 Chat 應用程式從自身擁有的聊天室取得成員資格 可存取 Google Chat 內容 (例如成員的聊天室),但不包含 即時通訊應用程式會員資格,包括專屬會員方案。驗證中 同時 使用者驗證 傳回已驗證使用者可存取的聊天室成員資格。

必要條件

Python

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

取得會員資格詳情

如要取得 Google Chat 會員詳細資料,請在 要求:

  • 取代為 應用程式驗證,指定 chat.bot 授權範圍。取代為 使用者驗證 指定 chat.memberships.readonlychat.memberships 授權 範圍。最佳做法是選擇仍然最嚴格的範圍 可讓您的應用程式正常運作
  • get 方法membership 項資源
  • 傳遞會員 name 即可享有優惠。從 Google Chat 的成員資源

以下是加入會員的方式 使用者驗證

Python

  1. 在工作目錄中,建立名為 chat_membership_get.py 的檔案。
  2. 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()
    
  3. 請在程式碼中替換下列內容:

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

    python3 chat_membership_get.py
    

Chat API 會傳回 membership敬上 列出指定的成員資格