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.
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:
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.