驗證及授權即時通訊應用程式和 Google Chat API 要求

驗證和授權機制是用來驗證身分和 還能分別存取資源本文件將概述驗證 適用於 Chat 應用程式和 Chat API 要求的權限。

流程總覽

下圖顯示驗證和 Google Chat 授權:

Google Chat 驗證與授權的高階步驟
圖 1. Google Chat 驗證與授權的高階步驟。

  1. 設定 Google Cloud 專案、啟用 Chat API,並設定 Chat 擴充應用程式:在開發期間,您會建立 存取 Google Cloud 專案在 Google Cloud 專案中,您啟用 Chat API。 設定 Chat 應用程式,並設定驗證機制。 若需更多資訊,請參閲 在 Google Workspace 中開發應用程式建構 Chat 擴充應用程式

  2. Call Chat API:當應用程式呼叫 Chat API,會將驗證憑證傳送給 Chat API。如果 應用程式會透過服務帳戶進行驗證,而憑證會以 即可。如果您的應用程式需要呼叫 Chat API 就會提示使用者 使用者登入。

  3. 要求資源:應用程式要求存取以下項目 範圍 您在設定驗證時指定的 IP 位址。

  4. 徵求同意:如果應用程式是以使用者的身分進行驗證,Google 會顯示 OAuth 同意畫面,讓使用者決定是否要授予您的應用程式 要求取得所要求資料的存取權。使用服務帳戶進行驗證 必須取得使用者同意

  5. 傳送已核准的資源要求:如果使用者同意 授權範圍,您應用程式會將憑證和使用者通過核准 轉換為要求要求傳送至 Google 授權伺服器 取得存取權杖

  6. Google 傳回存取權杖:存取權杖包含 如果傳回的範圍清單比 要求範圍,您的應用程式會關閉受到權杖限制的所有功能。

  7. 存取要求的資源:應用程式會使用 Google 提供的存取權杖 叫用 Chat API 並存取 Chat API 資源。

  8. 取得更新權杖 (選用):如果應用程式必須存取 相較於單一存取權杖的生命週期,Google Chat API 也能取得 重新整理權杖。若需更多資訊,請參閲 使用 OAuth 2.0 存取 Google API

  9. 要求更多資源:如果您的應用程式需要更多存取權,系統會要求使用者授予更多資源 授予新範圍,導致收到新的要求來取得存取權杖 (步驟 3-6)。

當 Chat 擴充應用程式需要驗證時

即時通訊應用程式可以根據使用者互動傳送訊息,或 以非同步方式載入物件也能代表使用者完成工作,例如 建立 Chat 聊天室或取得使用者清單 Chat 聊天室。

即時通訊應用程式不需要驗證,就能回應 使用者互動,除非 Chat 應用程式呼叫 Chat API 或其他 Google API 來處理回應

如要傳送非同步訊息或代表使用者執行工作 即時通訊應用程式會向 Chat API 需要驗證和授權

回應使用者互動時不需要驗證

Google Chat 應用程式不需要以使用者的身分進行驗證 同步接收及回覆通訊對象的即時通訊應用程式 互動事件

每當有使用者互動或應用程式互動時,Google Chat 應用程式就會收到互動事件 叫用 Chat 應用程式,包括:

  • 使用者傳送訊息至 Chat 應用程式。
  • 使用者使用 @號提及 Chat 應用程式。
  • 使用者叫用其中一個 Chat 應用程式 斜線指令

下圖顯示了 Chat 使用者和 Chat 應用程式:

使用 Chat 應用程式互動事件時不需要授權
圖 2:Chat 應用程式互動事件不需要驗證。

  1. 使用者透過以下方式傳送訊息給 Chat 應用程式: Google Chat。
  2. Google Chat 會將訊息轉寄至應用程式。
  3. 應用程式接收、處理訊息,並將回應傳回給 Google Chat。
  4. Google Chat 會為使用者或聊天室顯示回覆。

每次即時通訊應用程式互動都會重複這個序列 活動。

非同步郵件需要驗證

當 Chat 應用程式 要求 Chat API 需要驗證和授權

藉由呼叫 Chat API,即時通訊應用程式就可以張貼訊息 加入 Google Chat,或代表使用者完成工作及存取資料。適用對象 舉例來說,偵測到伺服器服務中斷後,Chat 應用程式可以呼叫 透過即時通訊 API 執行以下操作:

  • 建立專門用來調查及修正的 Chat 聊天室 以及服務中斷情形
  • 將使用者加入 Chat 聊天室。
  • 在 Chat 聊天室中張貼訊息,說明 以及服務中斷情形

下圖顯示非同步訊息序列, Chat 應用程式和 Chat 聊天室:

非同步訊息需要驗證
圖 3. 非同步訊息需要驗證。

  1. Chat 應用程式透過呼叫 透過下列 API 使用 spaces.messages.create 方法, 並在 HTTP 要求中納入使用者憑證
  2. Google Chat 會使用 或使用者憑證
  3. Google Chat 會在特定 Chat 中顯示應用程式訊息 空白鍵。

Chat API 範圍

設定 OAuth 同意畫面並選擇範圍 定義要對使用者和應用程式審查者顯示的資訊,並註冊 供您日後發布

如要定義授予應用程式的存取權層級,您必須找出 宣告授權範圍。授權範圍是 OAuth 2.0 URI 字串,內含 Google Workspace 應用程式名稱和資料類型 存取權和存取層級

非機密範圍

範圍程式碼 說明
https://www.googleapis.com/auth/chat.bot

chat.bot 範圍僅支援服務帳戶。個人中心 無法使用使用者憑證進行驗證 全網域委派 使用這個範圍

允許 Chat 擴充應用程式查看即時通訊及傳送訊息。提供 可使用所有 Chat 擴充應用程式可用的功能。

機密範圍

範圍程式碼 說明
https://www.googleapis.com/auth/chat.spaces 建立對話和聊天室,以及查看或編輯中繼資料 (包括 對話記錄設定及存取權設定) 同步處理資料。
https://www.googleapis.com/auth/chat.spaces.create 在 Chat 中建立新對話。
https://www.googleapis.com/auth/chat.spaces.readonly 在 Chat 中查看即時通訊和聊天室。
https://www.googleapis.com/auth/chat.memberships 在 Chat 對話中查看、新增、更新及移除成員。
https://www.googleapis.com/auth/chat.memberships.app 在 Google Chat 的對話中新增及移除本應用程式。
https://www.googleapis.com/auth/chat.memberships.readonly 查看 Chat 對話中的成員。
https://www.googleapis.com/auth/chat.messages.create 在 Chat 中撰寫及傳送訊息。
https://www.googleapis.com/auth/chat.messages.reactions 在 Chat 中查看、新增和刪除訊息回應。
https://www.googleapis.com/auth/chat.messages.reactions.create 在 Chat 中回應訊息。
https://www.googleapis.com/auth/chat.messages.reactions.readonly 在 Chat 中查看訊息的回應。
https://www.googleapis.com/auth/chat.users.readstate 查看及修改 Chat 對話的上次已讀時間。
https://www.googleapis.com/auth/chat.users.readstate.readonly 查看 Chat 對話的上次讀取時間。
https://www.googleapis.com/auth/chat.admin.spaces.readonly 在 Chat 中查看管理員網域擁有的即時通訊和聊天室。
https://www.googleapis.com/auth/chat.admin.spaces 在 Chat 中查看或編輯管理員網域擁有的即時通訊和聊天室。
https://www.googleapis.com/auth/chat.admin.memberships.readonly 在 Chat 中查看管理員網域所擁有對話的成員和管理員。
https://www.googleapis.com/auth/chat.admin.memberships 在 Chat 中查看、新增、更新及移除管理員網域對話中的成員和管理員。

受限制的範圍

範圍程式碼 說明
https://www.googleapis.com/auth/chat.delete 刪除對話和聊天室,以及移除相關檔案的存取權 。
https://www.googleapis.com/auth/chat.import 將聊天室、訊息和成員資格匯入 Chat。適用對象 如需更多資訊,請參閱 授權 Chat 擴充應用程式匯入資料
https://www.googleapis.com/auth/chat.messages 查看、撰寫、傳送、更新及刪除郵件,還能新增、查看和 刪除訊息的回應。
https://www.googleapis.com/auth/chat.messages.readonly 在 Chat 中查看訊息和回應。
https://www.googleapis.com/auth/chat.admin.delete 刪除管理員網域擁有的對話和聊天室,以及移除相關檔案的存取權 。

上表的範圍指出此等機密程度,取決於 下列定義:

如果應用程式需要存取任何其他 Google API,您可以新增這些範圍 如要進一步瞭解 Google API 範圍,請參閱將 OAuth 2.0 用於 存取 Google API。

如要進一步瞭解 Google Workspace API 的範圍,請參閱 設定 OAuth 同意畫面並選擇範圍

必要驗證類型

透過兩種方式來驗證和授權 Chat 擴充應用程式 Chat API:使用者憑證或服務帳戶

透過使用者憑證授權,Chat 應用程式可以 代表使用者存取使用者資料,並代表使用者完成動作。 OAuth 範圍會指定經過授權的資料和動作。

透過應用程式授權,Chat 應用程式 以使用服務帳戶憑證一律應用程式授權 使用 chat.bot 授權範圍。

決定特定 API 要求要使用的憑證類型時, 請注意,部分 API 方法僅支援特定類型的 憑證如果 API 方法同時支援這兩種憑證,就會使用憑證類型。 ,會影響傳回的結果:

  • 如果使用應用程式授權,這些方法只會傳回應用程式可 資源存取權
  • 如果使用者授權,這些方法只會傳回 user 都能在 Chat UI 中使用

舉例來說,使用應用程式授權呼叫 ListSpaces 方法會傳回 應用程式所屬的聊天室清單。向使用者呼叫 ListSpaces License 會傳回使用者所屬的聊天室清單。於 方法,應用程式可能會在呼叫 Chat API (視您想使用的功能而定)。

非同步 Chat API 呼叫

下表列出 Chat API 方法及其支援的版本 授權範圍:

方法 支援使用者驗證功能 支援應用程式驗證功能 支援的授權範圍
聊天室  
建立聊天室 透過使用者驗證
  • chat.spaces.create
  • chat.spaces
  • chat.import
設定聊天室 透過使用者驗證
  • chat.spaces.create
  • chat.spaces
取得聊天室 透過使用者驗證
  • chat.spaces.readonly
  • chat.spaces
透過應用程式驗證功能:
  • chat.bot
透過管理員權限進行使用者驗證
  • chat.admin.spaces.readonly
列出聊天室 透過使用者驗證
  • chat.spaces.readonly
  • chat.spaces
透過應用程式驗證功能:
  • chat.bot
搜尋聊天室 透過管理員權限進行使用者驗證
  • chat.admin.spaces.readonly
更新聊天室 透過使用者驗證
  • chat.spaces
  • chat.import
透過管理員權限進行使用者驗證
  • chat.admin.spaces
刪除聊天室 透過使用者驗證
  • chat.delete
  • chat.import
透過管理員權限進行使用者驗證
  • chat.admin.delete
完成聊天室匯入程序 透過使用者驗證
  • chat.import
尋找即時訊息 透過使用者驗證
  • chat.spaces.readonly
  • chat.spaces
透過應用程式驗證功能:
  • chat.bot
成員  
建立成員 透過使用者驗證
  • chat.memberships
  • chat.memberships.app
  • chat.import
透過管理員權限進行使用者驗證
  • chat.admin.memberships
加入會員 透過使用者驗證
  • chat.memberships.readonly
  • chat.memberships
透過應用程式驗證功能:
  • chat.bot
透過管理員權限進行使用者驗證
  • chat.admin.memberships.readonly
名單成員 透過使用者驗證
  • chat.memberships.readonly
  • chat.memberships
  • chat.import
透過應用程式驗證功能:
  • chat.bot
透過管理員權限進行使用者驗證
  • chat.admin.memberships.readonly
刪除成員 透過使用者驗證
  • chat.memberships
  • chat.memberships.app
  • chat.import
透過管理員權限進行使用者驗證
  • chat.admin.memberships
更新成員 透過使用者驗證
  • chat.memberships
  • chat.import
透過管理員權限進行使用者驗證
  • chat.admin.memberships
訊息  
建立訊息 透過使用者驗證
  • chat.messages.create
  • chat.messages
  • chat.import
透過應用程式驗證功能:
  • chat.bot
接收訊息 透過使用者驗證
  • chat.messages.readonly
  • chat.messages
透過應用程式驗證功能:
  • chat.bot
列出訊息 透過使用者驗證
  • chat.messages.readonly
  • chat.messages
  • chat.import
更新訊息 透過使用者驗證
  • chat.messages
  • chat.import
透過應用程式驗證功能:
  • chat.bot
刪除訊息 透過使用者驗證
  • chat.messages
  • chat.import
透過應用程式驗證功能:
  • chat.bot
反應  
建立回應 透過使用者驗證
  • chat.messages.reactions.create
  • chat.messages.reactions
  • chat.messages
  • chat.import
清單回應 透過使用者驗證
  • chat.messages.reactions.readonly
  • chat.messages.reactions
  • chat.messages.readonly
  • chat.messages
刪除回應 透過使用者驗證
  • chat.messages.reactions
  • chat.messages
  • chat.import
媒體與附件  
以檔案附件上傳媒體 透過使用者驗證
  • chat.messages.create
  • chat.messages
  • chat.import
下載媒體 透過使用者驗證
  • chat.messages.readonly
  • chat.messages
透過應用程式驗證功能:
  • chat.bot
取得訊息附件 透過應用程式驗證功能:
  • chat.bot
使用者讀取狀態
取得使用者的聊天室讀取狀態 透過使用者驗證
  • chat.users.readstate
  • chat.users.readstate.readonly
更新使用者的聊天室讀取狀態 透過使用者驗證
  • chat.users.readstate
取得使用者的執行緒讀取狀態 透過使用者驗證
  • chat.users.readstate
  • chat.users.readstate.readonly
聊天室活動
取得太空活動 透過使用者驗證
  • chat.messages
  • chat.messages.readonly
  • chat.messages.reactions
  • chat.messages.reactions.readonly
  • chat.memberships
  • chat.memberships.readonly
  • chat.spaces
  • chat.spaces.readonly
列出聊天室事件 透過使用者驗證
  • chat.messages
  • chat.messages.readonly
  • chat.messages.reactions
  • chat.messages.reactions.readonly
  • chat.memberships
  • chat.memberships.readonly
  • chat.spaces
  • chat.spaces.readonly

適用於 Chat 應用程式互動事件

下表列出使用者與 Chat 擴充應用程式互動的常見方式,以及是否需要或支援驗證:

情境 不需要驗證 支援使用者驗證功能 支援應用程式驗證功能
訊息來源:
Chat 應用程式互動事件
Apps Script 回呼
Google Cloud Pub/Sub
回覆訊息:
使用 Chat 應用程式互動事件進行同步
同步,使用 Apps Script 回呼傳回值
傳送新訊息:
含有連入 Webhook