列出 Google Workspace 订阅
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本页面介绍如何使用 subscriptions.list()
方法列出 Google Workspace 订阅。
当您使用用户身份验证调用此方法时,该方法会返回用户授权的订阅列表。使用应用身份验证时,该方法可以返回包含应用的所有订阅的列表。
前提条件
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 脚本
在您的 Apps 脚本项目中,创建一个名为 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);
}
替换以下内容:
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
字段。
如需列出订阅,请在您的 Apps 脚本项目中运行函数 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
对象数组,其中包含与查询过滤条件匹配的事件。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-26。
[null,null,["最后更新时间 (UTC):2025-08-26。"],[[["\u003cp\u003eThis page provides instructions on how to list Google Workspace subscriptions using the \u003ccode\u003esubscriptions.list()\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eThe method returns a list of authorized subscriptions when using user authentication and may return any app subscription when using app authentication.\u003c/p\u003e\n"],["\u003cp\u003eCode samples are provided for both Apps Script and Python to demonstrate listing subscriptions based on event type and target resource.\u003c/p\u003e\n"],["\u003cp\u003ePrerequisites include having a Google Workspace subscription, proper authentication and setup for the chosen scripting language.\u003c/p\u003e\n"],["\u003cp\u003eThe response is a paginated array of \u003ccode\u003eSubscription\u003c/code\u003e objects matching the query filter.\u003c/p\u003e\n"]]],["The `subscriptions.list()` method retrieves Google Workspace subscriptions, returning user-authorized subscriptions with user authentication or any app subscriptions with app authentication. Listing subscriptions requires filtering by event type and optionally by target resource. Apps Script and Python examples detail this process: defining event type and target resource filters, setting OAuth scopes, and running the code to retrieve and print a filtered array of `Subscription` objects using the Workspace Events API. User or app authentication is required.\n"],null,["# List Google Workspace subscriptions\n\nThis page explains how to list Google Workspace subscriptions using the\n[`subscriptions.list()`](/workspace/events/reference/rest/v1/subscriptions/list)\nmethod.\n\nWhen you call this method with user authentication, the method returns\na list of subscriptions authorized by the user. When you use app authentication,\nthe method can return a list that contains any subscription for the app.\n\nPrerequisites\n-------------\n\n### Apps Script\n\n- \u003cbr /\u003e\n\n A Google Workspace subscription. To create one, see [Create a subscription](/workspace/events/guides/create-subscription).\n\n \u003cbr /\u003e\n\n- Requires user authentication with one or more\n [scopes that support all event types for\n the subscription](/workspace/events/guides/auth#scopes-event-type).\n\n | **Note:** To run the code sample in this guide, you must use the same [OAuth client\n | ID credentials](/workspace/events/guides/create-subscription#create-oauth) that you used to create the subscription.\n\n\u003c!-- --\u003e\n\n- An Apps Script project:\n - Use your Google Cloud project instead of the default one created automatically by Apps Script.\n - For all scopes that you added to configure the OAuth consent screen, you must also add the scopes to the `appsscript.json` file in your Apps Script project. For example, if you specified the `chat.messages` scope, then add the following: \n\n ```console\n \"oauthScopes\": [\n \"https://www.googleapis.com/auth/chat.messages\"\n ]\n \n ```\n - [Enable](/apps-script/guides/services/advanced#enable_advanced_services) the `Google Workspace Events` advanced service.\n\n### Python\n\n- Python 3.6 or greater\n- The [pip](https://pypi.org/project/pip/) package management tool\n- The latest Google client libraries for Python. To install or update them, run the following command in your command-line interface: \n\n ```console\n pip3 install --upgrade google-api-python-client google-auth-oauthlib\n \n ```\n\n\u003c!-- --\u003e\n\n- \u003cbr /\u003e\n\n A Google Workspace subscription. To create one, see [Create a subscription](/workspace/events/guides/create-subscription).\n\n \u003cbr /\u003e\n\n- Requires [authentication](/workspace/events/guides/auth):\n\n - For user authentication, requires a scope that supports at least one of the event types for the subscription. To identify a scope, see [Scopes by event type](/workspace/events/guides/auth#scopes-event-type). **Note:** To run the code sample in this guide, you must use the same [OAuth\n | client ID credentials](/workspace/events/guides/create-subscription#create-oauth) that you used to create the subscription.\n - For app authentication, requires the `chat.bot` scope (Google Chat apps only).\n\nList subscriptions authorized by a user\n---------------------------------------\n\nTo list subscriptions, you must filter by at least one event type. You can also\nfilter your query by one or more target resources. To learn about supported\nquery filters, see the [`list()` method\ndocumentation](/workspace/events/reference/rest/v1/subscriptions/list).\n\nThe following code sample returns an array of\n[`Subscription`](/workspace/events/reference/rest/v1/subscriptions) objects\nfiltered by event type and target resource. When authenticated as a user, the\nmethod only returns a list of subscriptions that the user authorized the app to\ncreate.\n\nTo list subscriptions for a specified event type and target resource: \n\n### Apps Script\n\n1. In your Apps Script project, create a new script file\n named `listSubscriptions` and add the following code:\n\n function listSubscriptions() {\n // Filter for event type (required).\n const eventType = &\u003cvar translate=\"no\"\u003e#39;EVENT_\u003c/var\u003eTYPE';\n\n // Filter for target resource (optional).\n const targetReso\u003cvar translate=\"no\"\u003eurce\u003c/var\u003e = 'TARGET_RESOURCE';\n\n const filter = `event_types:\"${eventType}\" AND target_resource=\"${targetResource}\"`\n\n // Call the Workspace Events API using the advanced service.\n const response = WorkspaceEvents.Subscriptions.list({ filter });\n console.log(response);\n }\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eEVENT_TYPE\u003c/var\u003e: An [event type](/workspace/events/reference/rest/v1/subscriptions#Subscription.FIELDS.event_types) formatted according to the CloudEvents specification. For example, to filter for subscriptions that receive events about new memberships to a Google Chat space, `google.workspace.chat.message.v1.created`.\n - \u003cvar translate=\"no\"\u003eTARGET_RESOURCE\u003c/var\u003e: A [target resource](/workspace/events/reference/rest/v1/subscriptions#Subscription.FIELDS.target_resource), formatted as its full resource name. For example, to filter by subscriptions for a Google Chat space, use `//chat.googleapis.com/spaces/`\u003cvar translate=\"no\"\u003eSPACE_ID\u003c/var\u003e where `spaces/`\u003cvar translate=\"no\"\u003eSPACE_ID\u003c/var\u003e represents the [`name`](/workspace/chat/api/reference/rest/v1/spaces#Space.FIELDS.name) field for the `Space` resource.\n2. To list subscriptions, run the function `listSubscriptions` in\n your Apps Script project.\n\n### Python\n\n1. In your working directory, create a file named `list_subscriptions.py`\n and add the following code:\n\n \"\"\"List subscriptions.\"\"\"\n\n from google_auth_oauthlib.flow import InstalledAppFlow\n from googleapiclient.discovery import build\n\n # Specif\u003cvar translate=\"no\"\u003ey req\u003c/var\u003euired scopes.\n SCOPES = ['SCOPE']\n\n # Authenticate with Google Workspace and get user authentication.\n flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)\n CREDENTIALS = flow.run_local_server()\n\n # Call the Workspace Events API using the service endpoint.\n service = build(\n 'workspaceevents',\n 'v1',\n credentials=CRED\u003cvar translate=\"no\"\u003eENTIALS\u003c/var\u003e,\n )\n\n # Filter for event type (required).\n EVENT_TYPE = 'EVENT_T\u003cvar translate=\"no\"\u003eYPE'\u003c/var\u003e\n\n # Filter for target resource (optional).\n TARGET_RESOURCE = 'TARGET_RESOURCE'\n\n FILTER = f'event_types:\"{EVENT_TYPE}\" AND target_resource=\"{TARGET_RESOURCE}\"'\n response = service.subscriptions().list(filter=FILTER).execute()\n print(response)\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eSCOPE\u003c/var\u003e: An OAuth scope that [supports at least\n one event type from the subscription](/workspace/events/guides/auth#scopes-event-type). For example, if your subscription receives events an updated Chat space, `https://www.googleapis.com/auth/chat.spaces.readonly`.\n - \u003cvar translate=\"no\"\u003eEVENT_TYPE\u003c/var\u003e: An [event type](/workspace/events/reference/rest/v1/subscriptions#Subscription.FIELDS.event_types) formatted according to the CloudEvents specification. For example, to filter for subscriptions that receive events about new memberships to a Google Chat space, `google.workspace.chat.message.v1.created`.\n - \u003cvar translate=\"no\"\u003eTARGET_RESOURCE\u003c/var\u003e: A [target resource](/workspace/events/reference/rest/v1/subscriptions#Subscription.FIELDS.target_resource), formatted as its full resource name. For example, to filter by subscriptions for a Google Chat space, use `//chat.googleapis.com/spaces/`\u003cvar translate=\"no\"\u003eSPACE_ID\u003c/var\u003e where `spaces/`\u003cvar translate=\"no\"\u003eSPACE_ID\u003c/var\u003e represents the [`name`](/workspace/chat/api/reference/rest/v1/spaces#Space.FIELDS.name) field for the `Space` resource.\n2. In your working directory, make sure you've stored your OAuth client ID\n credentials and named the file `credentials.json`. The code sample uses this JSON\n file to authenticate with Google Workspace and get user credentials. For instructions,\n see [Create OAuth client ID\n credentials](/workspace/events/guides/create-subscription#create-oauth).\n\n3. To list subscriptions, run the following in your terminal:\n\n python3 list_subscriptions.py\n\nThe Google Workspace Events API returns a [paginated array of `Subscription`\nobjects](/workspace/events/reference/rest/v1/subscriptions/list#response-body)\nthat match the filter for your query.\n\nRelated topics\n--------------\n\n- [Update or renew a subscription](/workspace/events/guides/update-subscription)\n- [Resolve errors and reactivate a subscription](/workspace/events/guides/reactivate-subscription)\n- [Delete a subscription](/workspace/events/guides/delete-subscription)\n- [Get a subscription](/workspace/events/guides/get-subscription)\n- [Create a subscription](/workspace/events/guides/create-subscription)"]]