คู่มือนี้อธิบายวิธีใช้เมธอด
search()ในทรัพยากร
Spaceของ
Google Chat API เพื่อค้นหาพื้นที่ทำงานที่มีชื่อในองค์กร Google Workspace
Spaceทรัพยากร
แสดงถึงพื้นที่ที่ผู้คนและแอป Chat สามารถส่งข้อความ
แชร์ไฟล์ และทำงานร่วมกันได้ พื้นที่ทำงานมีหลายประเภท ดังนี้
- ข้อความส่วนตัว (DM) คือการสนทนาระหว่างผู้ใช้ 2 คน หรือระหว่างผู้ใช้กับแอป Chat
- การแชทเป็นกลุ่มคือการสนทนาระหว่างผู้ใช้ 3 คนขึ้นไปกับแอป Chat
- พื้นที่ทำงานที่มีชื่อเป็นพื้นที่ทำงานถาวรที่ผู้คนส่งข้อความ แชร์ไฟล์ และทำงานร่วมกัน
หากคุณเป็นผู้ดูแลระบบ Google Workspace และต้องการค้นหาในพื้นที่ทำงานทั้งหมดในองค์กร รวมถึงพื้นที่ทำงานส่วนตัวที่คุณไม่ได้เข้าร่วม โปรดดูค้นหาและจัดการพื้นที่ทำงานในฐานะผู้ดูแลระบบ Google Workspace เพื่อเรียก API ด้วยสิทธิ์ของผู้ดูแลระบบ (useAdminAccess=true)
เมื่อค้นหาพื้นที่ทำงานด้วยการตรวจสอบสิทธิ์ของผู้ใช้โดยไม่มีสิทธิ์ของผู้ดูแลระบบ เมธอดจะค้นหาพื้นที่ทำงานที่มีชื่อ (spaceType เป็น SPACE) ที่ผู้ใช้ที่ตรวจสอบสิทธิ์แล้วมีสิทธิ์เข้าถึง เช่น พื้นที่ทำงานที่ผู้ใช้เป็นสมาชิกในองค์กร
ข้อกำหนดเบื้องต้น
Node.js
- บัญชี Google Workspace เวอร์ชัน Business หรือ Enterprise ที่มีสิทธิ์เข้าถึง Google Chat
- ตั้งค่าสภาพแวดล้อมโดยทำดังนี้
- สร้างโปรเจ็กต์ Google Cloud
- กำหนดค่าหน้าจอขอความยินยอม OAuth
- เปิดใช้และกำหนดค่า Google Chat API ด้วยชื่อ ไอคอน และคำอธิบายสำหรับแอป Chat
- ติดตั้งไลบรารีของไคลเอ็นต์ Google Cloud สำหรับ Node.js
-
สร้างข้อมูลเข้าสู่ระบบรหัสไคลเอ็นต์ OAuth สำหรับแอปพลิเคชันบนเดสก์ท็อป หากต้องการเรียกใช้ตัวอย่างใน
คู่มือนี้ ให้บันทึกข้อมูลเข้าสู่ระบบเป็นไฟล์ JSON ชื่อ
credentials.jsonใน ไดเรกทอรีภายในเครื่อง
- เลือกขอบเขตการให้สิทธิ์ที่รองรับการตรวจสอบสิทธิ์ของผู้ใช้
Python
- บัญชี Google Workspace เวอร์ชัน Business หรือ Enterprise ที่มีสิทธิ์เข้าถึง Google Chat
- ตั้งค่าสภาพแวดล้อมโดยทำดังนี้
- สร้างโปรเจ็กต์ Google Cloud
- กำหนดค่าหน้าจอขอความยินยอม OAuth
- เปิดใช้และกำหนดค่า Google Chat API ด้วยชื่อ ไอคอน และคำอธิบายสำหรับแอป Chat
- ติดตั้ง ไลบรารีของไคลเอ็นต์ Google Cloud สำหรับ Python
-
สร้างข้อมูลเข้าสู่ระบบรหัสไคลเอ็นต์ OAuth สำหรับแอปพลิเคชันบนเดสก์ท็อป หากต้องการเรียกใช้ตัวอย่างใน
คู่มือนี้ ให้บันทึกข้อมูลเข้าสู่ระบบเป็นไฟล์ JSON ชื่อ
credentials.jsonใน ไดเรกทอรีภายในเครื่อง
- เลือกขอบเขตการให้สิทธิ์ที่รองรับการตรวจสอบสิทธิ์ของผู้ใช้
Java
- บัญชี Google Workspace เวอร์ชัน Business หรือ Enterprise ที่มีสิทธิ์เข้าถึง Google Chat
- ตั้งค่าสภาพแวดล้อมโดยทำดังนี้
- สร้างโปรเจ็กต์ Google Cloud
- กำหนดค่าหน้าจอขอความยินยอม OAuth
- เปิดใช้และกำหนดค่า Google Chat API ด้วยชื่อ ไอคอน และคำอธิบายสำหรับแอป Chat
- ติดตั้ง ไลบรารีของไคลเอ็นต์ Google Cloud สำหรับ Java
-
สร้างข้อมูลเข้าสู่ระบบรหัสไคลเอ็นต์ OAuth สำหรับแอปพลิเคชันบนเดสก์ท็อป หากต้องการเรียกใช้ตัวอย่างใน
คู่มือนี้ ให้บันทึกข้อมูลเข้าสู่ระบบเป็นไฟล์ JSON ชื่อ
credentials.jsonใน ไดเรกทอรีภายในเครื่อง
- เลือกขอบเขตการให้สิทธิ์ที่รองรับการตรวจสอบสิทธิ์ของผู้ใช้
Apps Script
- บัญชี Google Workspace เวอร์ชัน Business หรือ Enterprise ที่มีสิทธิ์เข้าถึง Google Chat
- ตั้งค่าสภาพแวดล้อมโดยทำดังนี้
- สร้างโปรเจ็กต์ Google Cloud
- กำหนดค่าหน้าจอขอความยินยอม OAuth
- เปิดใช้และกำหนดค่า Google Chat API ด้วยชื่อ ไอคอน และคำอธิบายสำหรับแอป Chat
- สร้างโปรเจ็กต์ Apps Script แบบสแตนด์อโลน, แล้วเปิดใช้บริการ Chat ขั้นสูง
- เลือกขอบเขตการให้สิทธิ์ที่รองรับการตรวจสอบสิทธิ์ของผู้ใช้
ค้นหาพื้นที่ทำงานด้วยการตรวจสอบสิทธิ์ของผู้ใช้
หากต้องการค้นหาพื้นที่ทำงานใน Google Chat โดยไม่มีสิทธิ์ของผู้ดูแลระบบ ให้ส่งข้อมูลต่อไปนี้ในคำขอ
- เมื่อใช้การตรวจสอบสิทธิ์ของผู้ใช้ ให้ระบุขอบเขตการให้สิทธิ์
chat.spaces.readonlyหรือchat.spaces - เรียกเมธอด
search()ในทรัพยากรSpace - ตั้งค่า
useAdminAccessเป็นfalse(หรือละเว้นพารามิเตอร์) - ระบุพารามิเตอร์
queryในการค้นหาเพื่อกรองผลลัพธ์ ดังนี้spaceType = "SPACE"- ต้องระบุเมื่อระบุqueryและค่าที่รองรับมีเพียงSPACEdisplayName- กรองตามชื่อที่แสดงของพื้นที่ทำงานโดยใช้โอเปอเรเตอร์HAS(:) เช่นdisplayName:"Project"ระบบจะแยกข้อความที่จะค้นหาออกเป็นโทเค็น และจับคู่คำนำหน้าของแต่ละโทเค็นโดยไม่คำนึงถึงตัวพิมพ์เล็กและตัวพิมพ์ใหญ่ รวมถึงจับคู่โทเค็นแต่ละรายการอย่างอิสระเป็นสตริงย่อยที่ใดก็ได้ในdisplayNameของพื้นที่ทำงาน หมายเหตุ: เมื่อuseAdminAccessเป็นfalseคุณต้องระบุdisplayNameในการค้นหาเพื่อดึงข้อมูลผลลัพธ์ที่มีความหมาย ไม่เช่นนั้นเมธอดจะแสดงการตอบกลับที่ว่างเปล่าexternalUserAllowed- กรองตามตัวเลือกที่อนุญาตให้แขกภายนอกเข้าพื้นที่ทำงาน (trueหรือfalse)
- ระบุ
pageSizeเพื่อจำกัดจำนวนพื้นที่ทำงานสูงสุดที่จะแสดง (สูงสุด1000) หรือpageTokenเพื่อดึงข้อมูลผลลัพธ์ในหน้าถัดไป (ไม่บังคับ) - ระบุ
orderByเพื่อจัดเรียงผลการค้นหา (createTime descหรือrelevance desc) (ไม่บังคับ) หมายเหตุ:relevance descพร้อมใช้งานผ่านโปรแกรม Google Workspace Developer Preview
ระบบรองรับเฉพาะโอเปอเรเตอร์ AND ในช่องต่างๆ ของการค้นหา เช่น spaceType = "SPACE" AND displayName:"Hello" AND externalUserAllowed = "true" ใน displayName และ externalUserAllowed ระบบรองรับโอเปอเรเตอร์ OR หากคุณต้องการจับคู่ตามเกณฑ์หลายรายการ
ตัวอย่างต่อไปนี้จะค้นหาพื้นที่ทำงานที่มีชื่อซึ่งมีคำว่า "Project" ในชื่อที่แสดง
Node.js
/**
* This sample shows how to search for spaces without administrator privileges.
*
* It relies on the @google-apps/chat npm package.
*/
// Read the documentation for more details:
// https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces/search
const {ChatServiceClient} = require('@google-apps/chat');
const {auth} = require('google-auth-library');
async function main() {
// Create a client
const chatClient = new ChatServiceClient({
authClient: await auth.getClient({
scopes: ['https://www.googleapis.com/auth/chat.spaces.readonly']
})
});
// Initialize request arguments.
// When useAdminAccess is false, spaceType and displayName are required in query.
const request = {
query: 'spaceType = "SPACE" AND displayName:"Project"',
useAdminAccess: false
};
// Call the API and iterate over the paginated response
const iterable = chatClient.searchSpacesAsync(request);
for await (const result of iterable) {
console.log('Found space:', result.space.displayName, result.space.name);
}
}
main().catch(console.error);
Python
"""
This sample shows how to search for spaces without administrator privileges.
"""
from google.apps import chat_v1
import google.auth
# Read the documentation for more details:
# https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces/search
def search_spaces():
# Create a client
scopes = ["https://www.googleapis.com/auth/chat.spaces.readonly"]
credentials, _ = google.auth.default(scopes=scopes)
client = chat_v1.ChatServiceClient(credentials=credentials)
# Initialize request arguments.
# When use_admin_access is False, space_type and display_name are required in query.
request = chat_v1.SearchSpacesRequest(
query='spaceType = "SPACE" AND displayName:"Project"',
use_admin_access=False
)
# Make the request and iterate over the paginated results.
page_result = client.search_spaces(request)
for result in page_result.results:
print(f"Found space: {result.space.display_name} ({result.space.name})")
if __name__ == "__main__":
search_spaces()
Java
/**
* This sample shows how to search for spaces without administrator privileges.
*/
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.ChatServiceClient.SearchSpacesPagedResponse;
import com.google.chat.v1.SearchSpacesRequest;
import com.google.chat.v1.SearchSpaceResult;
// Read the documentation for more details:
// https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces/search
public class SearchSpaces {
public static void main(String[] args) throws Exception {
// See https://github.com/googleworkspace/java-samples/blob/main/chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/AuthenticationUtils.java
// for an example of how to authenticate the request.
try (ChatServiceClient chatServiceClient = AuthenticationUtils.createClientWithUserCredentials(
ImmutableList.of("https://www.googleapis.com/auth/chat.spaces.readonly"))) {
SearchSpacesRequest request = SearchSpacesRequest.newBuilder()
.setQuery("spaceType = \"SPACE\" AND displayName:\"Project\"")
.setUseAdminAccess(false)
.build();
SearchSpacesPagedResponse response = chatServiceClient.searchSpaces(request);
for (SearchSpaceResult result : response.iterateAll()) {
System.out.printf("Found space: %s (%s)\n", result.space.getDisplayName(), result.space.getName());
}
}
}
}
Apps Script
/**
* This sample shows how to search for spaces without administrator privileges.
*/
// Read the documentation for more details:
// https://developers.google.com/workspace/chat/api/reference/rest/v1/spaces/search
function searchSpaces() {
try {
// Call the API
// When useAdminAccess is false, spaceType and displayName are required in query.
const response = Chat.Spaces.search({
query: 'spaceType = "SPACE" AND displayName:"Project"',
useAdminAccess: false
});
if (response.results && response.results.length > 0) {
response.results.forEach(result => {
console.log('Found space: %s (%s)', result.space.displayName, result.space.name);
});
} else {
console.log('No matching spaces found.');
}
} catch (err) {
console.log('Failed to search spaces: ' + err.message);
}
}
Chat API จะแสดงรายการพื้นที่ทำงานแบบแบ่งหน้า ซึ่งตรงกับการค้นหาและผู้ใช้ที่เรียกใช้มีสิทธิ์เข้าถึง
หัวข้อที่เกี่ยวข้อง
- สร้างพื้นที่ทำงาน
- ตั้งค่าพื้นที่ทำงาน
- ดูรายละเอียดเกี่ยวกับพื้นที่ทำงาน
- แสดงรายการพื้นที่ทำงาน
- อัปเดตพื้นที่ทำงาน
- ลบพื้นที่ทำงาน
- ค้นหาและจัดการพื้นที่ทำงานในฐานะผู้ดูแลระบบ Google Workspace