Perform Google Workspace Domain-Wide Delegation of Authority

In enterprise applications you may want to programmatically access a user's data without any manual authorization on their part. In Google Workspace domains, the domain administrator can grant third-party applications with domain-wide access to its users' data — this is referred as domain-wide delegation of authority. To delegate authority this way, domain administrators can use service accounts with OAuth 2.0.

Create the service account and its credentials

You need to create a service account and its credentials. During this procedure you need to gather information that will be used later for the Google Workspace domain-wide delegation of authority and in your code to authorize with your service account. The three items you need are your service account’s:

  • Client ID.
  • Private key file.
  • Email address.

To get started using Admin SDK, you need to first use the setup tool, which guides you through creating a project in the Google API Console, enabling the API, and creating credentials.

To set up a new service account, do the following:

  1. Click Create credentials > Service account key.
  2. Choose whether to download the service account's public/private key as a standard P12 file, or as a JSON file that can be loaded by a Google API client library.

Your new public/private key pair is generated and downloaded to your machine; it serves as the only copy of this key. You are responsible for storing it securely.

  1. From the Credentials page, click Create credentials > OAuth client ID to create your OAuth 2.0 credentials or Create credentials > Service account key to create a service account.
  2. If you created an OAuth client ID, then select your application type.
  3. Fill in the form and click Create.

Your application's client IDs and service account keys are now listed on the Credentials page. For details, click a client ID; parameters vary depending on the ID type, but might include email address, client secret, JavaScript origins, or redirect URIs.

After downloading the file and closing the dialog, you will be able to get the service account's email address and client ID.

You should now have gathered your service account's Private Key file, Client ID and email address. You are ready to delegate domain-wide authority to your service account.

Delegate domain-wide authority to your service account

To access user data on a Google Workspace domain, the service account that you created needs to be granted access by a super administrator for the domain. For more information about domain-wide delegation, see Control Google Workspace API access with domain-wide delegation.

To delegate domain-wide authority to a service account:

  1. From your Google Workspace domain’s Admin console, go to Main menu > Security > Access and data control > API controls.
  2. In the Domain wide delegation pane, select Manage Domain Wide Delegation.
  3. Click Add new.
  4. In the Client ID field, enter the client ID obtained from the service account creation steps above.
  5. In the OAuth Scopes field, enter a comma-delimited list of the scopes required for your application (for a list of possible scopes, see Authorize requests).

    For example, if you need domain-wide access to activity reports, enter:

  6. Click Authorize.

Your service account now has domain-wide access to the Google Admin SDK Reports API for all of the users of your domain. You are ready to instantiate an authorized Admin SDK Reports service object on behalf of your Google Workspace domain's users.

Instantiate an Admin SDK Reports service object

This section shows how to instantiate an Admin SDK Reports service object and then authorize it to make API requests using OAuth 2.0 and your service account's credentials to perform Google Workspace Domain-wide delegation. You should now have the service account's private key file in a PKCS #12 format and the email of the service account.



/** Email of the Service Account */
private static final String SERVICE_ACCOUNT_EMAIL = "<some-id>";

/** Path to the Service Account's Private Key file */
private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH = "/path/to/<public_key_fingerprint>-privatekey.p12";

 * Build and returns a Reports service object authorized with the service accounts
 * that act on behalf of the given user.
 * @param userEmail The email of the user. Needs permissions to access the Admin APIs.
 * @return Reports service object that is ready to make requests.
public static Reports getReportsService(String userEmail) throws GeneralSecurityException,
    IOException, URISyntaxException {
  HttpTransport httpTransport = new NetHttpTransport();
  JacksonFactory jsonFactory = new JacksonFactory();
  GoogleCredential credential = new GoogleCredential.Builder()
  Reports service = new Reports.Builder(httpTransport, jsonFactory, null)
  return service;


from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

"""Email of the Service Account"""

"""Path to the Service Account's Private Key file"""
SERVICE_ACCOUNT_PKCS12_FILE_PATH = '/path/to/<public_key_fingerprint>-privatekey.p12'

def create_reports_service(user_email):
    """Build and returns an Admin SDK Reports service object authorized with the service accounts
    that act on behalf of the given user.

      user_email: The email of the user. Needs permissions to access the Admin APIs.
      Admin SDK reports service object.

    credentials = ServiceAccountCredentials.from_p12_keyfile(

    credentials = credentials.create_delegated(user_email)

    return build('admin', 'reports_v1', http=http)

If you encounter errors authorizing requests using service accounts, see JWT error codes.

Now that you are authorized and have instantiated an Admin SDK Reports service, have a look at the next steps.

Next steps

Once you are comfortable authorizing Admin SDK Reports API requests, you're ready to manage users usage reports and perform any of the other operations described in our developer's guides. You can learn more about available API methods in the API Reference.