本指南說明如何使用 Google Chat API SectionItem 資源的 list 方法,列出區段中的項目 (例如聊天室)。
只有聊天室可以做為區段項目。詳情請參閱「在 Google Chat 中建立及整理區段」。
必要條件
Python
- 具有 Google Chat 存取權的 Business 或 Enterprise 版 Google Workspace 帳戶。
- 設定環境:
- 建立 Google Cloud 專案。
- 設定 OAuth 同意畫面。
- 啟用及設定 Google Chat API,並為 Chat 應用程式命名、設定圖示和說明。
- 安裝 Python Cloud 用戶端程式庫。
-
為電腦應用程式建立 OAuth 用戶端 ID 憑證。如要在本指南中執行範例,請將憑證儲存為名為
credentials.json的 JSON 檔案,並儲存至本機目錄。
- 選擇支援使用者驗證的授權範圍。
列出某個版面中的空間
如要在具有使用者驗證的區段中列出空間,請在要求中傳遞下列項目:
- 指定
chat.users.sections或chat.users.sections.readonly授權範圍。 - 呼叫
ListSectionItems方法。 - 將
parent設為區段的資源名稱。
以下範例會列出某個區段中的空格:
Python
from google.cloud import chat_v1
def list_section_items():
# Create a client
client = chat_v1.ChatServiceClient()
# Initialize request
request = chat_v1.ListSectionItemsRequest(
parent="SECTION_NAME"
)
# Make the request
page_result = client.list_section_items(request=request)
# Handle the response
for item in page_result:
print(item)
如要執行這個範例,請替換下列項目:
SECTION_NAME:區段的資源名稱。您可以呼叫ListSections方法來取得資源名稱。
Chat API 會傳回 SectionItem 資源清單。