This page explains how to delete a Google Workspace subscription using the
When you delete a subscription, your app no longer receives any events. If a subscription expires, the Google Workspace Events API deletes it automatically.
- Python 3.6 or greater
- The pip package management tool
- A Google Workspace subscription. To create one, see Create a subscription.
- 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.
- For app authentication, requires the
chat.botscope (Google Chat apps only).
Generate an API key
For Developer Preview, you use a non-public version of the Google Workspace Events API's discovery document. To access this discovery document when you call the API, you must pass an API key in your request to authenticate as your app. You use this key in the code sample later in this guide.
To create an API key, open the Google Cloud project for your app and do the following
- In the Google Cloud console, go to Menu > APIs & Services > Credentials.
- Click Create credentials > API key.
- Your new API key is displayed.
- Click Copy to copy your API key for use in your app's code. The API key can also be found in the "API keys" section of your project's credentials.
- Click Restrict key to update advanced settings and limit use of your API key. For more details, see Applying API key restrictions.
Delete a subscription authorized by a user
The following code sample deletes a
using user authentication.
To delete a subscription:
In your working directory, create a file named
delete_subscription.pyand add the following code:
"""Delete subscription.""" 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('client_secrets.json', SCOPES) CREDENTIALS = flow.run_local_server() # Call the Workspace Events API using the service endpoint. DISCOVERY_SERVICE_URL = 'https://workspaceevents.googleapis.com/$discovery/rest?version=v1beta&labels=DEVELOPER_PREVIEW&key=API_KEY' service = build( 'workspaceevents', 'v1beta', credentials=CREDENTIALS, discoveryServiceUrl=DISCOVERY_SERVICE_URL, ) NAME = 'subscriptions/SUBSCRIPTION_ID' response = service.subscriptions().delete(name=NAME).execute() print(response)
Replace the following:
SCOPE: An OAuth scope that supports at least one event type from the subscription. For example, if your subscription receives events an updated Chat space,
API_KEY: The API key that you generated for the Google Workspace Events API.
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 delete the subscription, run the following in your terminal:
The Google Workspace Events API returns a completed long-running
operation that contains the
instance of the
Subscription resource that you deleted.