Perform G Suite 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 G Suite 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 G Suite 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

The service account that you created needs to be granted access to the G Suite domain’s user data that you want to access. The following tasks have to be performed by an administrator of the G Suite domain:

  1. Go to your G Suite domain’s Admin console.
  2. Select Security from the list of controls. If you don't see Security listed, select More controls from the gray bar at the bottom of the page, then select Security from the list of controls.
  3. Select Advanced settings from the list of options.
  4. Select Manage third party OAuth Client access in the Authentication section.
  5. In the Client name field enter the service account's Client ID.
  6. In the One or More API Scopes field enter the list of scopes that your application should be granted access to (see image below). For example if you need domain-wide access to activity reports enter:
  7. Click the Authorize button.

Your service account now has domain-wide access to the Google Admin SDK Reports API for all the users of your domain. You are ready to instantiate an authorized Admin SDK Reports service object on behalf of your G Suite 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 G Suite 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)

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.