Servizio avanzato per eventi Google Workspace
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Il servizio avanzato Google Workspace Events ti consente di utilizzare l'API Google Workspace Events in Apps Script. Questa API ti consente di iscriverti alle risorse di
Google Workspace per ricevere gli eventi pertinenti che ti
interessano. Gli eventi rappresentano le modifiche alle risorse, ad esempio quando le risorse vengono
create, aggiornate o eliminate.
Prerequisiti
Riferimento
Per saperne di più su questo servizio, consulta la
documentazione di riferimento dell'API Google Workspace Events.
Come tutti i servizi avanzati in Apps Script, il servizio
Google Workspace Events utilizza gli stessi oggetti, metodi e
parametri dell'API pubblica.
Codice di esempio
Questi esempi mostrano come eseguire azioni comuni dell'API Google Workspace Events utilizzando il servizio avanzato.
Creare una sottoscrizione
Per creare un abbonamento a una risorsa Google Workspace, aggiungi
la seguente funzione al codice del progetto Apps Script:
Elenco sottoscrizioni
Per elencare gli abbonamenti filtrati in base ai tipi di eventi e alla risorsa di destinazione,
aggiungi la seguente funzione al codice del progetto Apps Script:
Recupero sottoscrizione
Per ottenere informazioni su un abbonamento, aggiungi la seguente funzione al codice del progetto Apps Script:
Aggiorna abbonamento
Per aggiornare o rinnovare un abbonamento, aggiungi la seguente funzione al codice del progetto Apps Script:
Riattiva abbonamento
Per riattivare un abbonamento, aggiungi la seguente funzione al codice del progetto Apps Script:
Eliminazione di una sottoscrizione
Per eliminare un abbonamento, aggiungi la seguente funzione al codice del progetto Apps Script:
Recupera operazione
La maggior parte dei metodi dell'API Google Workspace Events restituisce un'operazione a lunga esecuzione.
Per determinare lo stato dell'operazione, puoi utilizzare il metodo
operations.get()
.
Per ottenere informazioni su un'operazione, aggiungi la seguente funzione
al codice del progetto Apps Script:
Per ottenere il nome di un'operazione, utilizza il valore del campo name
restituito
da uno dei metodi dell'API Google Workspace Events, ad esempio
subscriptions.create()
o
subscriptions.patch()
.
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
Ultimo aggiornamento 2025-08-31 UTC.
[null,null,["Ultimo aggiornamento 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)."]]