This page explains how to renew a Google Workspace subscription using the
subscriptions.update()
method. You can use this method to update the expiration time of a subscription,
including renewing the subscription for the maximum expiration time possible, or
to update the list of event types to receive about the target resource.
Apps Script
- A Google Workspace subscription. To create one, see Create a subscription.
Requires user authentication with one or more scopes that support all event types for the subscription.
- An Apps Script project:
- Use your Google Cloud project instead of the default one created automatically by Apps Script.
- For any 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: - Enable
the
Google Workspace Events
advanced service.
"oauthScopes": [ "https://www.googleapis.com/auth/chat.messages.readonly" ]
Python
- Python 3.6 or greater
- The pip package management tool
- The latest Google client libraries for Python. To install or update them, run the following
command in your command-line interface:
pip3 install --upgrade google-api-python-client google-auth-oauthlib
- A Google Workspace subscription. To create one, see Create a subscription.
Requires user authentication with one or more scopes that support all event types for the subscription.
Renew a Google Workspace subscription
In this section, you use the Google Workspace Events API's
subscriptions.update()
method to renew a subscription to its maximum
expiration time. To specify the maximum expiration time, you update the ttl
field of the
Subscription
resource
to 0
.
The maximum expiration time depends on what resource data is included in the event payload. To learn more about expiration times, see Event data for Google Workspace events.
To renew a Google Workspace subscription:
Apps Script
In your Apps Script project, create a new script file named
updateSubscription
and add the following code:function updateSubscription() { // The name of the subscription to update. const name = 'subscriptions/SUBSCRIPTION_ID'; // Call the Workspace Events API using the advanced service. const response = WorkspaceEvents.Subscriptions.patch({ ttl: '0s', }, name); console.log(response); }
Replace the following:
To update the Google Workspace subscription, run the function
updateSubscription
in your Apps Script project.
Python
In your working directory, create a file named
update_subscription.py
and add the following code:"""Update subscription.""" from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build # Specify required scopes. SCOPES = [SCOPES] # Authenticate with Google Workspace and get user authentication. flow = InstalledAppFlow.from_client_secrets_file('client_secrets.json', SCOPES) CREDENTIALS = flow.run_local_server() # Call the Workspace Events API using the service endpoint. service = build( 'workspaceevents', 'v1', credentials=CREDENTIALS, ) BODY = { 'ttl': {'seconds': 0}, } NAME = 'subscriptions/SUBSCRIPTION_ID' response = ( service.subscriptions() .patch(name=NAME, updateMask='ttl', body=BODY) .execute() ) print(response)
Replace the following:
SCOPES
: One or more OAuth scopes that support each event type for the subscription. Formatted as an array of strings. To list multiple scopes, separate by commas. For example,'https://www.googleapis.com/auth/chat.spaces.readonly', 'https://www.googleapis.com/auth/chat.memberships.readonly'
.SUBSCRIPTION_ID
: The ID of the subscription. To get the ID, you can use any of the following:
In your working directory, make sure you've stored your OAuth client ID credentials and named the file
client_secrets.json
. The code sample uses this JSON file to authenticate with Google Workspace and get user credentials. For instructions, see Create OAuth client ID credentials.To update the Google Workspace subscription, run the following in your terminal:
python3 update_subscription.py
Subscription
resource.
To get details about the updated Subscription
resource, use the
operations.get()
method
and specify the Operation
resource returned from your subscriptions.update()
request. Otherwise, if you specify a Operation
resource from a previous
version of the subscription, the response is empty.