Get details about a Google Workspace subscription

This page explains how to get details about a Google Workspace subscription using the subscriptions.get() method.

When you call this method with user authentication, the method returns details about a subscription authorized by the user. When you use app authentication, the method can return details about any subscription for the app.

Prerequisites

Apps Script

  • 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:
    • "oauthScopes": [
        "https://www.googleapis.com/auth/chat.messages.readonly"
      ]
          
    • Enable the Google Workspace Events advanced service.

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

    • 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.bot scope (Google Chat apps only).

Get a subscription authorized by a user

The following code sample gets details about a Subscriptionresource using user authentication. When authenticated as a user, the method returns a subscription that the user authorized the app to create.

To get a subscription authorized by a user:

Apps Script

  1. In your Apps Script project, create a new script file named getSubscription and add the following code:

    function getSubscription() {
      // The name of the subscription to get.
      const name = 'subscriptions/SUBSCRIPTION_ID';
    
      // Call the Workspace Events API using the advanced service.
      const subscription = WorkspaceEvents.Subscriptions.get(name);
      console.log(subscription);
    }
    

    Replace the following:

    • SUBSCRIPTION_ID: The ID of the subscription. To get the ID, you can use any of the following:
      • The value of the uid field.
      • The ID of the resource name represented in the name field. For example, if the resource name is subscriptions/subscription-123, use subscription-123.
  2. To get the subscription, run the function getSubscription in your Apps Script project.

Python

  1. In your working directory, create a file named get_subscription.py and add the following code:

    """Get 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.
    service = build(
        'workspaceevents',
        'v1',
        credentials=CREDENTIALS,
    )
    
    NAME = 'subscriptions/SUBSCRIPTION_ID'
    subscription = service.subscriptions().get(name=NAME).execute()
    print(subscription)
    

    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, https://www.googleapis.com/auth/chat.spaces.readonly.
    • SUBSCRIPTION_ID: The ID of the subscription. To get the ID, you can use any of the following:
      • The value of the uid field.
      • The ID of the resource name represented in the name field. For example, if the resource name is subscriptions/subscription-123, use subscription-123.
  2. 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.

  3. To get the subscription, run the following in your terminal:

    python3 get_subscription.py