本指南說明如何使用 Google Chat API Section 資源的 list 方法,列出 Google Chat 中可供已驗證使用者存取的區段。
詳情請參閱「在 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授權範圍。 - 呼叫
ListSections方法。 - 將
parent設為users/me。
以下範例會列出章節:
Python
from google.cloud import chat_v1
def list_sections():
# Create a client
client = chat_v1.ChatServiceClient()
# Initialize request
request = chat_v1.ListSectionsRequest(
parent="users/me"
)
# Make the request
page_result = client.list_sections(request=request)
# Handle the response
for section in page_result:
print(section)
Chat API 會傳回 Section 資源清單。