Dịch vụ sự kiện nâng cao của Google Workspace
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Dịch vụ Sự kiện nâng cao của Google Workspace cho phép bạn sử dụng Google Workspace Events API trong Apps Script. API này cho phép bạn đăng ký nhận các tài nguyên trên Google Workspace để nhận được các sự kiện liên quan mà bạn quan tâm. Sự kiện biểu thị những thay đổi đối với tài nguyên, chẳng hạn như khi tài nguyên được tạo, cập nhật hoặc xoá.
Điều kiện tiên quyết
Tài liệu tham khảo
Để biết thêm thông tin về dịch vụ này, hãy xem tài liệu tham khảo về Google Workspace Events API.
Giống như tất cả các dịch vụ nâng cao trong Apps Script, dịch vụ Google Workspace Events sử dụng cùng các đối tượng, phương thức và tham số như API công khai.
Mã mẫu
Các mẫu này cho thấy cách thực hiện các thao tác phổ biến của Google Workspace Events API bằng dịch vụ nâng cao.
Tạo gói thuê bao
Để tạo một lượt đăng ký nhận thông báo về một tài nguyên trên Google Workspace, hãy thêm hàm sau vào mã của dự án Apps Script:
Danh sách gói thuê bao
Để liệt kê các sự kiện được lọc theo loại sự kiện và tài nguyên đích, hãy thêm hàm sau vào mã của dự án Apps Script:
Nhận gói thuê bao
Để lấy thông tin về một gói thuê bao, hãy thêm hàm sau vào mã của dự án Apps Script:
Cập nhật đăng ký
Để cập nhật hoặc gia hạn gói thuê bao, hãy thêm hàm sau vào mã của dự án Apps Script:
Kích hoạt lại gói thuê bao
Để kích hoạt lại một gói thuê bao, hãy thêm hàm sau vào mã của dự án Apps Script:
Xoá gói thuê bao
Để xoá một gói thuê bao, hãy thêm hàm sau vào mã của dự án Apps Script:
Nhận thao tác
Hầu hết các phương thức của Google Workspace Events API đều trả về một thao tác diễn ra trong thời gian dài.
Để xác định trạng thái của thao tác, bạn có thể sử dụng phương thức operations.get()
.
Để biết thông tin về một thao tác, hãy thêm hàm sau vào mã của dự án Apps Script:
Để lấy tên của một thao tác, hãy sử dụng giá trị từ trường name
do một trong các phương thức Google Workspace Events API trả về, chẳng hạn như subscriptions.create()
hoặc subscriptions.patch()
.
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-31 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\u003cp\u003eThe Advanced Google Workspace Events service enables you to use the Google Workspace Events API in Apps Script to subscribe to Google Workspace resources and receive relevant events.\u003c/p\u003e\n"],["\u003cp\u003eTo utilize this service, you need an Apps Script project linked to a standard Google Cloud project, a Pub/Sub topic for receiving events, and specific authorization scopes in your project's \u003ccode\u003eappsscript.json\u003c/code\u003e file.\u003c/p\u003e\n"],["\u003cp\u003eThe service offers functionality to create, list, get, update, reactivate, and delete subscriptions, as well as retrieve information about long-running operations through the provided sample code snippets.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Workspace Events represent changes to resources such as creation, updates, or deletions, providing insights into resource activities within your Workspace environment.\u003c/p\u003e\n"]]],[],null,["The Advanced Google Workspace Events service lets you use the\n[Google Workspace Events API](/workspace/events) in\nApps Script. This API lets you subscribe to\nGoogle Workspace resources so that you receive relevant events that you're\ninterested in. Events represent changes to resources, such as when resources are\ncreated, updated, or deleted.\n\nPrerequisites\n\n- An Apps Script project using a standard Google Cloud project instead of the default one created automatically by Apps Script.\n- A [Pub/Sub](https://cloud.google.com/pubsub/docs/) topic created in the same Google Cloud project to receive subscription events. To create a Pub/Sub topic, see [Create and subscribe to a Pub/Sub topic](/workspace/events/guides/create-subscription#pubsub).\n- To subscribe to Chat events, you must have a Google Chat app configured on the Chat API configuration page in the Google Cloud console. To create a Google Chat app, see [Build a Google Chat app with Apps Script](/apps-script/quickstart/chat-app).\n- The necessary authorization scopes added to the Apps Script\n project's `appsscript.json` file. The necessary scopes depend on the types of\n the subscriptions' target resources and events. For details, see\n [Choose Google Workspace Events API scopes](/workspace/events/guides/auth).\n For example:\n\n \"oauthScopes\": [\n \"https://www.googleapis.com/auth/chat.messages.readonly\"\n ]\n\n| **Note:** This is an advanced service that you must [turn on before use](/apps-script/guides/services/advanced).\n\nReference\n\nFor more information about this service, see the\n[Google Workspace Events API reference documentation](/workspace/events/reference/rest/v1).\nLike all advanced services in Apps Script, the\nGoogle Workspace Events service uses the same objects, methods, and\nparameters as the public API.\n\nSample code\n\nThese samples show you how to perform common\n[Google Workspace Events API](/workspace/events)\nactions using the advanced service.\n\nCreate a subscription\n\nTo create a subscription to a Google Workspace resource, add\nthe following function to the Apps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```javascript\n/**\n * Creates a subscription to receive events about a Google Workspace resource.\n * For a list of supported resources and event types, see the\n * [Google Workspace Events API Overview](https://developers.google.com/workspace/events#supported-events).\n * For additional information, see the\n * [subscriptions.create](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/create)\n * method reference.\n * @param {!string} targetResource The full resource name of the Google Workspace resource to subscribe to.\n * @param {!string|!Array\u003cstring\u003e} eventTypes The types of events to receive about the resource.\n * @param {!string} pubsubTopic The resource name of the Pub/Sub topic that receives events from the subscription.\n */\nfunction createSubscription(targetResource, eventTypes, pubsubTopic) {\n try {\n const operation = WorkspaceEvents.Subscriptions.create({\n targetResource: targetResource,\n eventTypes: eventTypes,\n notificationEndpoint: {\n pubsubTopic: pubsubTopic,\n },\n });\n console.log(operation);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to create subscription with error %s', err.message);\n }\n}\n```\n\nList subscriptions\n\nTo list subscriptions filtered by event types and target resource,\nadd the following function to the Apps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```javascript\n/**\n * Lists subscriptions created by the calling app filtered by one or more event types and optionally by a target resource.\n * For additional information, see the\n * [subscriptions.list](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/list)\n * method reference.\n * @param {!string} filter The query filter.\n */\nfunction listSubscriptions(filter) {\n try {\n const response = WorkspaceEvents.Subscriptions.list({ filter });\n console.log(response);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to list subscriptions with error %s', err.message);\n }\n}\n```\n\nGet subscription\n\nTo get information about a subscription, add the following function to\nthe Apps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```javascript\n/**\n * Gets details about a subscription.\n * For additional information, see the\n * [subscriptions.get](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/get)\n * method reference.\n * @param {!string} name The resource name of the subscription.\n */\nfunction getSubscription(name) {\n try {\n const subscription = WorkspaceEvents.Subscriptions.get(name);\n console.log(subscription);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to get subscription with error %s', err.message);\n }\n}\n```\n\nUpdate subscription\n\nTo update or renew a subscription, add the following function to the\nApps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```javascript\n/**\n * Updates an existing subscription.\n * This can be used to renew a subscription that is about to expire.\n * For additional information, see the\n * [subscriptions.patch](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/patch)\n * method reference.\n * @param {!string} name The resource name of the subscription.\n */\nfunction patchSubscription(name) {\n try {\n const operation = WorkspaceEvents.Subscriptions.patch({\n // Setting the TTL to 0 seconds extends the subscription to its maximum expiration time.\n ttl: '0s',\n }, name);\n console.log(operation);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to update subscription with error %s', err.message);\n }\n}\n```\n\nReactivate subscription\n\nTo reactivate a subscription, add the following function to the\nApps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```javascript\n/**\n * Reactivates a suspended subscription.\n * Before reactivating, you must resolve any errors with the subscription.\n * For additional information, see the\n * [subscriptions.reactivate](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/reactivate)\n * method reference.\n * @param {!string} name The resource name of the subscription.\n */\nfunction reactivateSubscription(name) {\n try {\n const operation = WorkspaceEvents.Subscriptions.reactivate({}, name);\n console.log(operation);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to reactivate subscription with error %s', err.message);\n }\n}\n```\n\nDelete subscription\n\nTo delete a subscription, add the following function to the\nApps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```javascript\n/**\n * Deletes a subscription.\n * For additional information, see the\n * [subscriptions.delete](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/delete)\n * method reference.\n * @param {!string} name The resource name of the subscription.\n */\nfunction deleteSubscription(name) {\n try {\n const operation = WorkspaceEvents.Subscriptions.remove(name);\n console.log(operation);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to delete subscription with error %s', err.message);\n }\n}\n```\n\nGet operation\n\nMost Google Workspace Events API methods return a\n[long-running operation](/workspace/events/reference/rest/v1/operations).\nTo determine the status of the operation, you can use the\n[`operations.get()`](/workspace/events/reference/rest/v1/operations/get)\nmethod.\n\nTo get information about an operation, add the following function\nto the Apps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```javascript\n/**\n * Gets details about an operation returned by one of the methods on the subscription\n * resource of the Google Workspace Events API.\n * For additional information, see the\n * [operations.get](https://developers.google.com/workspace/events/reference/rest/v1/operations/get)\n * method reference.\n * @param {!string} name The resource name of the operation.\n */\nfunction getOperation(name) {\n try {\n const operation = WorkspaceEvents.Operations.get(name);\n console.log(operation);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to get operation with error %s', err.message);\n }\n}\n```\n\nTo get the name of an operation, use the value from the `name` field returned\nfrom one of the Google Workspace Events API methods, such as\n[`subscriptions.create()`](#create-subscription) or\n[`subscriptions.patch()`](#update-subscription)."]]