คำแนะนำนี้จะอธิบายวิธีใช้เมธอด get
ในทรัพยากร Space
ของ Google Chat API เพื่อดูรายละเอียดเกี่ยวกับพื้นที่ทำงาน เช่น ชื่อที่แสดง คำอธิบาย และหลักเกณฑ์
แหล่งข้อมูล Space
เป็นพื้นที่ที่ผู้คนและแอป Chat ใช้ในการส่งข้อความ แชร์ไฟล์ และทำงานร่วมกัน พื้นที่ทำงานมีหลายประเภทดังนี้
- ข้อความส่วนตัว (DM) คือการสนทนาระหว่างผู้ใช้ 2 คนหรือผู้ใช้กับแอป Chat
- การแชทเป็นกลุ่มคือการสนทนาระหว่างผู้ใช้ 3 คนขึ้นไปกับแอป Chat
- พื้นที่ทำงานที่มีชื่อจะเป็นพื้นที่ทำงานถาวรที่ผู้คนส่งข้อความ แชร์ไฟล์ และทำงานร่วมกัน
การตรวจสอบสิทธิ์ด้วยการตรวจสอบสิทธิ์แอปช่วยให้แอป Chat ได้รับพื้นที่ทำงานที่แอป Chat มีสิทธิ์เข้าถึงใน Google Chat (เช่น พื้นที่ทำงานที่แอปเป็นสมาชิก) การตรวจสอบสิทธิ์ด้วยการตรวจสอบสิทธิ์ผู้ใช้ทำให้คุณมีพื้นที่ที่ผู้ใช้ซึ่งตรวจสอบสิทธิ์แล้วมีสิทธิ์เข้าถึง
ข้อกำหนดเบื้องต้น
Python
- Python 3.6 ขึ้นไป
- เครื่องมือการจัดการแพ็กเกจ pip
ไลบรารีของไคลเอ็นต์ Google ล่าสุดสำหรับ Python หากต้องการติดตั้งหรืออัปเดต ให้เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง
pip3 install --upgrade google-api-python-client google-auth-oauthlib google-auth
- โปรเจ็กต์ Google Cloud ที่เปิดใช้และกำหนดค่า Google Chat API โปรดดูขั้นตอนที่หัวข้อสร้างแอป Google Chat
กำหนดค่าการให้สิทธิ์สำหรับแอป Chat แล้ว การรับพื้นที่ทำงานรองรับวิธีการตรวจสอบสิทธิ์ทั้ง 2 วิธีต่อไปนี้
- การตรวจสอบสิทธิ์ผู้ใช้
ด้วยขอบเขตการให้สิทธิ์
chat.spaces.readonly
หรือchat.spaces
- การตรวจสอบสิทธิ์แอป
ด้วยขอบเขตการให้สิทธิ์
chat.bot
- การตรวจสอบสิทธิ์ผู้ใช้
ด้วยขอบเขตการให้สิทธิ์
Node.js
- Node.js และ npm
ไลบรารีของไคลเอ็นต์ Google ล่าสุดสำหรับ Node.js หากต้องการติดตั้ง ให้เรียกใช้คำสั่งต่อไปนี้ในอินเทอร์เฟซบรรทัดคำสั่ง
npm install @google-cloud/local-auth @googleapis/chat
- โปรเจ็กต์ Google Cloud ที่เปิดใช้และกำหนดค่า Google Chat API โปรดดูขั้นตอนที่หัวข้อสร้างแอป Google Chat
กำหนดค่าการให้สิทธิ์สำหรับแอป Chat แล้ว การรับพื้นที่ทำงานรองรับวิธีการตรวจสอบสิทธิ์ทั้ง 2 วิธีต่อไปนี้
- การตรวจสอบสิทธิ์ผู้ใช้
ด้วยขอบเขตการให้สิทธิ์
chat.spaces.readonly
หรือchat.spaces
- การตรวจสอบสิทธิ์แอป
ด้วยขอบเขตการให้สิทธิ์
chat.bot
- การตรวจสอบสิทธิ์ผู้ใช้
ด้วยขอบเขตการให้สิทธิ์
รับพื้นที่ทำงาน
หากต้องการรับพื้นที่ทำงานใน Google Chat ให้ส่งข้อมูลต่อไปนี้ในคำขอ
- เมื่อใช้การตรวจสอบสิทธิ์แอป ให้ระบุขอบเขตการให้สิทธิ์
chat.bot
เมื่อใช้การตรวจสอบสิทธิ์ผู้ใช้ ให้ระบุขอบเขตการให้สิทธิ์chat.spaces.readonly
หรือchat.spaces
- เรียกใช้เมธอด
get
ในทรัพยากรSpace
โดยส่งผ่านname
ของพื้นที่ทำงานที่จะรับ รับชื่อพื้นที่ทำงานจากทรัพยากรพื้นที่ทำงานของ Google Chat หรือจาก URL ของพื้นที่ทำงาน
ดูรายละเอียดของพื้นที่ทำงานด้วยการตรวจสอบสิทธิ์ผู้ใช้
วิธีรับรายละเอียดพื้นที่ทำงานด้วยการตรวจสอบสิทธิ์ผู้ใช้มีดังนี้
Python
- สร้างไฟล์ชื่อ
chat_space_get_user.py
ในไดเรกทอรีการทำงาน ใส่รหัสต่อไปนี้ใน
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()
ในโค้ด ให้แทนที่
SPACE
ด้วยชื่อพื้นที่ทำงานซึ่งคุณดูได้จากเมธอดspaces.list
ใน Chat API หรือจาก URL ของพื้นที่ทำงานสร้างและเรียกใช้ตัวอย่างในไดเรกทอรีการทำงาน
python3 chat_space_get_user.py
Node.js
- สร้างไฟล์ชื่อ
get-space.js
ในไดเรกทอรีการทำงาน ใส่รหัสต่อไปนี้ใน
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);
ในโค้ด ให้แทนที่
SPACE
ด้วยชื่อพื้นที่ทำงานซึ่งคุณดูได้จากเมธอดspaces.list
ใน Chat API หรือจาก URL ของพื้นที่ทำงานเรียกใช้ตัวอย่างในไดเรกทอรีการทำงาน
node get-space.js
Chat API แสดงผลอินสแตนซ์ของ Space
ซึ่งแสดงรายละเอียดพื้นที่ทำงานที่ระบุ
ดูรายละเอียดของพื้นที่ทำงานด้วยการตรวจสอบสิทธิ์แอป
วิธีรับรายละเอียดพื้นที่ทำงานด้วยการตรวจสอบสิทธิ์แอปมีดังนี้
Python
- สร้างไฟล์ชื่อ
chat_space_get_app.py
ในไดเรกทอรีการทำงาน ใส่รหัสต่อไปนี้ใน
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)
ในโค้ด ให้แทนที่
SPACE
ด้วยชื่อพื้นที่ทำงานซึ่งคุณดูได้จากเมธอดspaces.list()
ใน Chat API หรือจาก URL ของพื้นที่ทำงานสร้างและเรียกใช้ตัวอย่างในไดเรกทอรีการทำงาน
python3 chat_space_get_app.py
Node.js
- สร้างไฟล์ชื่อ
app-get-space.js
ในไดเรกทอรีการทำงาน ใส่รหัสต่อไปนี้ใน
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);
ในโค้ด ให้แทนที่
SPACE
ด้วยชื่อพื้นที่ทำงานซึ่งคุณดูได้จากเมธอดspaces.list
ใน Chat API หรือจาก URL ของพื้นที่ทำงานเรียกใช้ตัวอย่างในไดเรกทอรีการทำงาน
node app-get-space.js
Chat API จะส่งคืนอินสแตนซ์ของ Space
ซึ่งแสดงรายละเอียดของพื้นที่ทำงานที่ระบุ
หัวข้อที่เกี่ยวข้อง
- สร้างพื้นที่ทำงาน
- ตั้งค่าพื้นที่ทำงาน
- แสดงรายการพื้นที่ทำงาน
- อัปเดตพื้นที่ทำงาน
- ลบพื้นที่ทำงาน
- ค้นหาพื้นที่ข้อความส่วนตัว