本頁說明如何使用 subscriptions.list()
方法列出 Google Workspace 訂閱項目。
使用使用者驗證呼叫這個方法時,該方法會傳回使用者授權的訂閱項目清單。使用應用程式驗證時,這個方法可以傳回包含應用程式所有訂閱項目的清單。
必要條件
Apps Script
- Google Workspace 訂閱方案。如要建立訂閱項目,請參閱「建立訂閱項目」一文。
需要使用一或多個支援訂閱項目所有事件類型的範圍進行使用者驗證。
- Apps Script 專案:
- 使用 Google Cloud 專案,而非 Apps Script 自動建立的預設專案。
- 針對您新增的所有範圍,以設定 OAuth 同意畫面,您也必須將範圍新增至 Apps Script 專案的
appsscript.json
檔案。舉例來說,如果您指定chat.messages
範圍,請新增下列項目: - 啟用
Google Workspace Events
進階服務。
"oauthScopes": [ "https://www.googleapis.com/auth/chat.messages" ]
Python
- Python 3.6 以上版本
- pip 套件管理工具
- 最新版 Python 適用的 Google 用戶端程式庫。如要安裝或更新這些工具,請在指令列介面中執行下列指令:
pip3 install --upgrade google-api-python-client google-auth-oauthlib
- Google Workspace 訂閱方案。如要建立訂閱項目,請參閱「建立訂閱項目」一文。
需要驗證:
- 如要進行使用者驗證,必須使用至少支援一項訂閱事件類型的範圍。如要找出範圍,請參閱「依事件類型劃分的範圍」。
- 如要進行應用程式驗證,必須使用
chat.bot
範圍 (僅限 Google Chat 應用程式)。
列出使用者授權的訂閱項目
如要列出訂閱項目,您必須至少依一個事件類型進行篩選。您也可以依一或多個目標資源篩選查詢。如要瞭解支援的查詢篩選器,請參閱 list()
方法說明文件。
下列程式碼範例會傳回 Subscription
物件的陣列,並依事件類型和目標資源篩選。以使用者身分驗證時,這個方法只會傳回使用者授權應用程式建立的訂閱項目清單。
如要列出特定事件類型和目標資源的訂閱項目,請執行下列操作:
Apps Script
在 Apps Script 專案中,建立名為
listSubscriptions
的新指令碼檔案,並新增下列程式碼:function listSubscriptions() { // Filter for event type (required). const eventType = 'EVENT_TYPE'; // Filter for target resource (optional). const targetResource = 'TARGET_RESOURCE'; const filter = `event_types:"${eventType}" AND target_resource="${targetResource}"` // Call the Workspace Events API using the advanced service. const response = WorkspaceEvents.Subscriptions.list({ filter }); console.log(response); }
更改下列內容:
如要列出訂閱項目,請在 Apps Script 專案中執行
listSubscriptions
函式。
Python
在工作目錄中,建立名為
list_subscriptions.py
的檔案,並加入下列程式碼:"""List subscriptions.""" from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build # Specify required scopes. SCOPES = ['SCOPE'] # Authenticate with Google Workspace and get user authentication. flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES) CREDENTIALS = flow.run_local_server() # Call the Workspace Events API using the service endpoint. service = build( 'workspaceevents', 'v1', credentials=CREDENTIALS, ) # Filter for event type (required). EVENT_TYPE = 'EVENT_TYPE' # Filter for target resource (optional). TARGET_RESOURCE = 'TARGET_RESOURCE' FILTER = f'event_types:"{EVENT_TYPE}" AND target_resource="{TARGET_RESOURCE}"' response = service.subscriptions().list(filter=FILTER).execute() print(response)
更改下列內容:
SCOPE
:至少支援訂閱項目中一個事件類型的 OAuth 範圍。舉例來說,如果訂閱項目收到事件,例如更新後的 Chat 聊天室https://www.googleapis.com/auth/chat.spaces.readonly
。EVENT_TYPE
:事件類型,格式符合 CloudEvents 規格。舉例來說,如要篩選出接收 Google Chat 聊天室新成員事件的訂閱項目,請使用google.workspace.chat.message.v1.created
。TARGET_RESOURCE
:目標資源,格式為完整資源名稱。舉例來說,如要依 Google Chat 聊天室的訂閱項目篩選,請使用//chat.googleapis.com/spaces/SPACE_ID
,其中spaces/SPACE_ID
代表Space
資源的name
欄位。
請確認工作目錄中已儲存 OAuth 用戶端 ID 憑證,且檔案名為
credentials.json
。程式碼範例會使用這個 JSON 檔案向 Google Workspace 進行驗證,並取得使用者憑證。如需操作說明,請參閱「建立 OAuth 用戶端 ID 憑證」。如要列出訂閱項目,請在終端機中執行下列指令:
python3 list_subscriptions.py
Google Workspace Events API 會傳回分頁陣列的 Subscription
物件,這些物件符合查詢的篩選條件。