取得聊天室詳細資料

本指南說明如何針對以下項目的 Space 資源使用 get 方法: Google Chat API,可查看聊天室的詳細資料,例如顯示名稱、說明 和規範

Space 項資源 代表使用者和 Chat 擴充應用程式可以傳送訊息的地方 分享檔案及協同合作聊天室分為以下幾類:

  • 即時訊息 (DM) 是指兩位使用者或使用者之間的對話, Chat 應用程式。
  • 群組通訊是 3 位以上使用者之間的對話, Chat 擴充應用程式。
  • 已命名的聊天室是使用者傳送訊息、共用檔案、 這項原則著重於透過對開放式探究 嚴謹治學、誠信與合作的堅持來達到上述目標

使用以下憑證進行驗證: 應用程式驗證 可讓 Chat 應用程式取得 Chat 擴充應用程式可以存取 Google Chat (例如 所屬聊天室)。使用以下憑證進行驗證: 使用者驗證 可讓你取得已驗證使用者可存取的聊天室。

必要條件

Python

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

Node.js

  • Node.js 14 或以上版本
  • npm 套件管理工具
  • 最新的 Google 用戶端程式庫。安裝或更新 在指令列介面中執行下列指令:
    npm install @google-cloud/local-auth @googleapis/chat
    

取得聊天室

如要使用 Google Chat 聊天室,請在 要求:

  • 取代為 應用程式驗證, 請指定 chat.bot 授權範圍。取代為 使用者驗證 請指定 chat.spaces.readonlychat.spaces 授權範圍。
  • get 方法Space 項資源, 傳遞空格的 name。取得聊天室中的聊天室名稱 Google Chat 資源或聊天室網址。

透過使用者驗證機制取得聊天室詳細資料

以下說明如何取得 使用者驗證

Python

  1. 在工作目錄中,建立名為 chat_space_get_user.py 的檔案。
  2. chat_space_get_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.spaces.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then gets details about 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().get(
    
              # The space 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.
              name='spaces/SPACE'
    
          ).execute()
    
        # Prints details about the space.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. 將程式碼中的 SPACE 替換成空格名稱, 您可以從中取得 spaces.list 方法 或聊天室網址傳送

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

    python3 chat_space_get_user.py
    

Node.js

  1. 在工作目錄中,建立名為 get-space.js 的檔案。
  2. get-space.js 中加入下列程式碼:

    const chat = require('@googleapis/chat');
    const {authenticate} = require('@google-cloud/local-auth');
    
    /**
    * Gets details about a Chat space by name.
    * @return {!Object}
    */
    async function getSpace() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.spaces.readonly',
      ];
    
      const authClient =
          await authenticate({scopes, keyfilePath: 'client_secrets.json'});
    
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.get({name: 'spaces/SPACE'});
    }
    
    getSpace().then(console.log);
    
  3. 將程式碼中的 SPACE 替換成空格名稱, 您可以從中取得 spaces.list 方法 或聊天室網址傳送

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

    node get-space.js
    

Chat API 會傳回 Space,用於提供指定空間的詳細資料。

透過應用程式驗證功能取得聊天室詳細資料

以下說明如何取得 應用程式驗證

Python

  1. 在工作目錄中,建立名為 chat_space_get_app.py 的檔案。
  2. chat_space_get_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().get(
    
        # The space 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.
        name='spaces/SPACE'
    
    ).execute()
    
    print(result)
    
  3. 將程式碼中的 SPACE 替換成空格名稱, 您可以從中取得 呼叫 spaces.list() 方法 Chat API 或聊天室網址。

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

    python3 chat_space_get_app.py
    

Node.js

  1. 在工作目錄中,建立名為 app-get-space.js 的檔案。
  2. app-get-space.js 中加入下列程式碼:

    const chat = require('@googleapis/chat');
    
    /**
    * Gets details about a Chat space by name.
    * @return {!Promise<!Object>}
    */
    async function getSpace() {
      const scopes = [
        'https://www.googleapis.com/auth/chat.bot',
      ];
    
      const auth = new chat.auth.GoogleAuth({
        scopes,
        keyFilename: 'credentials.json',
      });
    
      const authClient = await auth.getClient();
      const chatClient = await chat.chat({version: 'v1', auth: authClient});
    
      return await chatClient.spaces.get({name: 'spaces/SPACE'});
    }
    
    getSpace().then(console.log);
    
  3. 將程式碼中的 SPACE 替換成空格名稱, 您可以從中取得 spaces.list 方法 或聊天室網址傳送

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

    node app-get-space.js
    

Chat API 會傳回 Space敬上 該物件會詳細列出指定空間