Authorize Requests

Every request your application sends to the Google Workspace Email Audit API must include an authorization token. The token also identifies your application to Google.

About authorization protocols

Your application must use OAuth 2.0 to authorize requests. No other authorization protocols are supported. If your application uses Sign In With Google, some aspects of authorization are handled for you.

Authorizing requests with OAuth 2.0

All requests to the Google Workspace Email Audit API must be authorized by an authenticated user.

The details of the authorization process, or "flow," for OAuth 2.0 vary somewhat depending on what kind of application you're writing. The following general process applies to all application types:

  1. When you create your application, you register it using the Google API Console. Google then provides information you'll need later, such as a client ID and a client secret.
  2. Activate the Google Workspace Email Audit API in the Google API Console. (If the API isn't listed in the API Console, then skip this step.)
  3. When your application needs access to user data, it asks Google for a particular scope of access.
  4. Google displays a consent screen to the user, asking them to authorize your application to request some of their data.
  5. If the user approves, then Google gives your application a short-lived access token.
  6. Your application requests user data, attaching the access token to the request.
  7. If Google determines that your request and the token are valid, it returns the requested data.

Some flows include additional steps, such as using refresh tokens to acquire new access tokens. For detailed information about flows for various types of applications, see Google's OAuth 2.0 documentation.

Here's the OAuth 2.0 scope information for the Google Workspace Email Audit API:

Scopes Meaning Global scope for access to audit user accounts

To request access using OAuth 2.0, your application needs the scope information, as well as information that Google supplies when you register your application (such as the client ID and the client secret).

Generate a public key

The Email Audit API provides the ability to upload an OpenPGP Public Encryption Key for the domain. This step is done once when setting up the mailbox download. The domain administrators can generate the public/private key pair using any OpenPGP-compatible software, such as GNU Privacy Guard (GPG).

To generate a key with GNU Privacy Guard, follow these steps:

  1. Install GnuPG 1.4.

  2. Run gpg --gen-key --expert to generate a new key, selecting option 8 "RSA (set your own capabilities)" and toggling the sign capability.

  3. Accept all default options to complete the key generation process.

  4. Run gpg --armor --export to export the key. Make sure you are only exporting the single key you just generated.

  5. Encode the key with base64 encoding the key with Motobit or another tool. Make sure to copy all lines, including the header, and not add any extra lines.

This public encryption key should be a PGP format ASCII-encoded RSA key. If using the GPG implementation, you need to specify the --gen-key --expert flags in order to generate an RSA encryption key (Option 8 on the GPG menu). This key should have the Encrypt action enabled. You can turn off the Sign action, as it will not be used.

Export the public part of the key using gpg --armor --export. Note you should only export the key for the user id that corresponds to your Google Workspace domain. If you have already used GPG to generate other keys, you will have multiple keys in your keyring. To verify this, use gpg --list-keys. If multiple keys are listed, you will need to specify the uid of the key that you want to export, using gpg --armor --export key_uid. This key will be used to encrypt the mailbox export files before being made available for downloads. Once the administrator downloads the requested files, decrypt the files using the private encryption key. If using the GPG implementation, this is done by running gpg -d. The final mailbox export files are in mbox format.

Before uploading the public key, convert it to a base64 encoded string. The public key file should be read with the charset US-ASCII, (IANA preferred charset name for ASCII).


Below is an example of how a public key should be converted into a base64 encoded string.

Public key before conversion

Version: GnuPG v1.4.10 (GNU/Linux)


Public key after conversion



Upload the public key

To upload the public key, start by creating an XML entry with the base64 encoded public key as shown in the example below:

<atom:entry xmlns:atom='' xmlns:apps=''>
<apps:property name="publicKey" value="<em>the base64 Encoded Key</em>"/>

Send an HTTP POST request to the 'publickey' feed URI in your Google Workspace domain:

POST{domain name}

If successful, the server returns a 201 CREATED status code found in the Google Data API HTTP status codes documentation. Along with the status code, the response is similar to this example:

<entry xmlns:atom='' xmlns:apps=''>
<id><em>your domain</em>/id>/id>
<link rel='self' type='application/atom+xml' href='<em>your domain</em>/id'/>
<link rel='edit' type='application/atom+xml' href='<em>your domain</em>/id'/>
<apps:property name='publicKey' value='<em>base64 encoded public key</em>'/>

An attempt to upload an invalid key will return with error code 1411, EncryptionPublicKeyInvalidFormat.

If, at a later time, the Email Audit API is called with a different public key, the old key is replaced by the latest key. All the future encryptions will use the latest key.