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 Google Sign-In, 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
https://apps-apis.google.com/a/feeds/compliance/audit/ 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).

Example

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

Public key before conversion

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.10 (GNU/Linux)

mQENBErWaD4BCACt2ngfs6/+QOGYbxNbc3gLnXHtqp7NTTXNW4SJo+/A1oUZoGxA
Qx6zFXhQ/8MXW66+8STS1YqNJOARFtjbIKPwjrdcukdPzYVKGZre0RaxCnMyCV+6
F4YNQD1UegHTu2wCGR1uiYOfLxUa7/do6s31WRTH8vbtiPY9/6obEIxDjDzKIqYO
rvRDWqALBYklOkJ3Hbgfyl42EsnLiAhS+dMs2PCDi2X0ZJCPZ8eTjLsdAtqVZJ+R
WC1J3UDuFfmcpsDYRtUL9w6YMtlapC+9mmJ3ABEBAAG0V0Rhc2hlciBUZXN0IChU
dGVyMkBkYXNoZXItaHlkLXRlc3QuY29tPokBOAQTAQIAIgUCStZoPgIbDQYLCQgH
k19QckTpwBdskEYumFvmWve5UX2SVV7fzOC0nZtgFxtZGlJhGmjsA3rxFTlb+Ira
WZayXCWYiCzd7m9z5/KyGD2GFTK/94mdm25N6GXh/b35pIFZXBI/rZjrYrhYRBFu
GtzGFIw9AAnFyUzEUUVfPWUtBe5yHMW54C60nHk5xYIa6qFhiLp4PYqZCrYX1iIs
fRROFA==
=STHr
-----END PGP PUBLIC KEY BLOCK-----

Public key after conversion

LS0tLS1CRUdJTiBQR1AgUFVCTElDIEtFWSBCTE9DSy0tLS0tDQpWZXJzaW9uOiBHbn
VQRyB2MS40LjEwIChHTlUvTGludXgpDQoNCm1RRU5CRXJXYUQ0QkNBQ3QybmdmczYv

K1FPR1lieE5iYzNnTG5YSHRxcDdOVFRYTlc0U0pvKy9BMW9VWm9HeEENClF4NnpGWG
hRLzhNWFc2Nis4U1RTMVlxTkpPQVJGdGpiSUtQd2pyZGN1a2RQellWS0dacmUwUmF4
Q25NeUNWKzYNCkY0WU5RRDFVZWdIVHUyd0NHUjF1aVlPZkx4VWE3L2RvNnMzMVdSVE
g4dmJ0aVBZOS82b2JFSXhEakR6S0lxWU8NCnJ2UkRXcUFMQllrbE9rSjNIYmdmeWw0
MkVzbkxpQWhTK2RNczJQQ0RpMlgwWkpDUFo4ZVRqTHNkQXRxVlpKK1INCldDMUozVU
R1RmZtY3BzRFlSdFVMOXc2WU10bGFwQys5bW1KM0FCRUJBQUcwVjBSaGMyaGxjaUJV
WlhOMElDaFUNCmRHVnlNa0JrWVhOb1pYSXRhSGxrTFhSbGMzUXVZMjl0UG9rQk9BUV
RBUUlBSWdVQ1N0Wm9QZ0liRFFZTENRZ0gNCmsxOVFja1Rwd0Jkc2tFWXVtRnZtV3Zl
NVVYMlNWVjdmek9DMG5adGdGeHRaR2xKaEdtanNBM3J4RlRsYitJcmENCldaYXlYQ1
dZaUN6ZDdtOXo1L0t5R0QyR0ZUSy85NG1kbTI1TjZHWGgvYjM1cElGWlhCSS9yWmpy
WXJoWVJCRnUNCkd0ekdGSXc5QUFuRnlVekVVVVZmUFdVdEJlNXlITVc1NEM2MG5Iaz
V4WUlhNnFGaGlMcDRQWXFaQ3JZWDFpSXMNCmZSUk9GQT09DQo9U1RIcg0KLS0tLS1F
TkQgUEdQIFBVQkxJQyBLRVkgQkxPQ0stLS0tLQ==

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='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>
<apps:property name="publicKey" value="<em>the base64 Encoded Key</em>"/>
</atom:entry>

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

POST https://apps-apis.google.com/a/feeds/compliance/audit/publickey/{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='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'>
<id>https://apps-apis.google.com/a/feeds/compliance/audit/publickey/<em>your domain</em>/id>/id>
<updated>2009-04-17T15:02:45.646Z</updated>
<link rel='self' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/compliance/audit/publickey/<em>your domain</em>/id'/>
<link rel='edit' type='application/atom+xml' href='https://apps-apis.google.com/a/feeds/compliance/audit/publickey/<em>your domain</em>/id'/>
<apps:property name='publicKey' value='<em>base64 encoded public key</em>'/>
</entry>

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.