ดูรายละเอียดเกี่ยวกับพื้นที่ทำงาน

คำแนะนำนี้จะอธิบายวิธีใช้เมธอด get ในทรัพยากร Space ของ Google Chat API เพื่อดูรายละเอียดเกี่ยวกับพื้นที่ทำงาน เช่น ชื่อที่แสดง คำอธิบาย และหลักเกณฑ์

แหล่งข้อมูล Space รายการ แสดงถึงสถานที่ที่ผู้คนและแอป Chat ส่งข้อความได้ แชร์ไฟล์ และทำงานร่วมกัน พื้นที่ทำงานมีหลายประเภทดังนี้

  • ข้อความส่วนตัว (DM) คือการสนทนาระหว่างผู้ใช้ 2 คนหรือผู้ใช้ 1 ราย แอปใน Chat
  • แชทเป็นกลุ่มคือการสนทนาระหว่างผู้ใช้ 3 คนขึ้นไปและ แอปแชท
  • พื้นที่ทำงานที่มีชื่อคือที่ที่ผู้คนส่งข้อความ แชร์ไฟล์ และทำงานร่วมกัน

กำลังตรวจสอบสิทธิ์กับ การตรวจสอบสิทธิ์แอป จะช่วยให้แอป Chat รับพื้นที่ทำงานที่ แอป Chat มีสิทธิ์เข้าถึงใน Google Chat (เช่น พื้นที่ทำงานที่แอปเป็นสมาชิก) กำลังตรวจสอบสิทธิ์กับ การตรวจสอบสิทธิ์ผู้ใช้ ช่วยให้คุณรับพื้นที่ทำงานที่ผู้ใช้ที่ได้รับการตรวจสอบสิทธิ์มีสิทธิ์เข้าถึงได้

ข้อกำหนดเบื้องต้น

Python

  • ธุรกิจหรือองค์กร บัญชี Google Workspace ที่มีสิทธิ์เข้าถึง Google Chat
  • Python 3.6 ขึ้นไป
  • เครื่องมือจัดการแพ็กเกจ pip
  • ไลบรารีของไคลเอ็นต์ Google ล่าสุด หากต้องการติดตั้งหรืออัปเดตส่วนขยาย เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง
    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    

Node.js

  • ธุรกิจหรือองค์กร บัญชี Google Workspace ที่มีสิทธิ์เข้าถึง Google Chat
  • Node.js 14 ขึ้นไป
  • npm เครื่องมือจัดการแพ็กเกจ
  • ไลบรารีของไคลเอ็นต์ Google ล่าสุด หากต้องการติดตั้งหรืออัปเดตส่วนขยาย เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง
    npm install @google-cloud/local-auth @googleapis/chat
    

รับพื้นที่ทำงาน

หากต้องการใช้พื้นที่ทำงานใน 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 วิธี ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

  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 วิธี ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

  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 หรือจาก URL ของพื้นที่ทำงาน

  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 วิธี ใน Chat API หรือจาก URL ของพื้นที่ทำงาน

  4. ในไดเรกทอรีการทำงาน ให้เรียกใช้ตัวอย่าง

    node app-get-space.js
    

Chat API จะแสดงผลอินสแตนซ์ Space ซึ่งระบุรายละเอียดของพื้นที่ทำงานที่ระบุ