本指南將說明如何使用
已啟用 get
方法
SpaceEvent
資源
可透過 Google Chat API 取得 Google Chat 聊天室中的活動詳細資料。
SpaceEvent
資源代表聊天室或其子項的變更
來分享相關資源,例如訊息、回應和會員資格學習
有關支援的事件類型,請參閱 eventType
欄位
SpaceEvent
項資源
參考文件
您最早可以在要求時間的 28 天前要求事件。事件
包含已變更資源的最新版本。舉例來說
您要求取得新訊息的事件,但訊息之後更新,
伺服器會在事件酬載中傳回更新後的 Message
資源。
如要呼叫這個方法,您必須使用 user 驗證。若要取得 事件,已驗證的使用者必須是活動聊天室成員。 發生。
必要條件
Python
- 企業或企業 具有存取權的 Google Workspace 帳戶 Google Chat。
- 設定環境:
- 建立 Google Cloud 專案。
- 設定 OAuth 同意畫面。
- 啟用並設定 Google Chat API。 圖示和說明
-
為電腦版應用程式建立 OAuth 用戶端 ID 憑證。如要在此環境中執行範例
指引,將憑證儲存為名為
client_secrets.json
的 JSON 檔案,並儲存至 本機目錄
- 選擇支援使用者驗證的授權範圍。
取得聊天室活動的詳細資料
如要在 Google Chat 中取得SpaceEvent
的詳細資料,請按照下列步驟操作:
- 呼叫
get
方法SpaceEvent
資源 - 傳遞
SpaceEvent
的name
即可取得。取得SpaceEvent
名稱 來自SpaceEvent
資源 Google Chat。 - 與使用者分享 驗證,指定 提供支援要求中事件類型的授權範圍。身為 最佳做法,選擇目前仍允許的最大限制範圍 才能正常運作
以下說明如何SpaceEvent
使用者驗證:
Python
- 在工作目錄中,建立名為
chat_space_event_get.py
的檔案。 在
chat_space_event_get.py
中加入下列程式碼:"""Gets a SpaceEvent resource from the Chat API.""" 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 = ['SCOPE'] # 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() .spaceEvents() .get( # The space event 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. # # Replace SPACE_EVENT with a SpaceEvent name. # Obtain the spaceEvent name from the SpaceEvent resource of # Chat API. name='spaces/SPACE/spaceEvents/SPACE_EVENT' ) .execute() ) # Prints details about the created spaceEvent. print(result)
請在程式碼中替換下列內容:
SCOPE
:根據 事件類型。舉例來說,如果你收到一場太空活動 請使用chat.memberships.readonly
範圍 格式為https://www.googleapis.com/auth/chat.memberships.readonly
。 事件類型可從spaces.spaceEvents.list
方法。 如要瞭解如何使用這個方法,請參閱 列出聊天室中的事件。SPACE
:聊天室名稱,可從中取得spaces.list
方法 或聊天室網址傳送SPACE_EVENT
:聊天室的名稱 事件,可從spaces.spaceEvents.list
方法。
在工作目錄中建構並執行範例:
python3 chat_space_event_get.py
Chat API 會傳回
SpaceEvent
敬上
並附上活動詳情