Authenticating API calls

In this example, we're showing how service accounts can be used to call the AdSense Platforms API to create and manage sub-accounts.

Step 1: Create a new Google Cloud project (or use an existing one)

If you have an existing Google Cloud project, feel free to use that. Otherwise, follow the guide below on setting up a new project:

https://cloud.google.com/resource-manager/docs/creating-managing-projects

Step 2: Create a service account

Using service accounts is the best way to create sub-accounts. Follow these steps to create your service account:

  • Visit the service accounts page in Google Cloud
  • You can either use an existing service account, or create a new one:
    • Click on "+ Create service account"
    • Fill in the "Service account details" form
    • Steps 2 and 3 on the page (granting access to projects and users) are optional

Learn more about creating and managing service accounts.

Once the service account has been created, you need to send it to Google to get it added to your AdSense account. This is essential, as the service account needs to be allowed to access your AdSense account. Please communicate it via your account manager.

Step 3: Enable the AdSense Platform API for your Google Cloud project

The AdSense Platform API isn't discoverable, meaning you have to visit the following link to enable the it for your project:

https://console.developers.google.com/apis/api/adsenseplatform.googleapis.com/overview

Step 4: Create a service key

In order to generate access tokensfor use in the API calls, you need to create a service key. Follow these steps:

  • Visit the service accounts page in Google Cloud
  • In the actions column, for the service account you want to use to create sub-accounts, click then click "Manage keys"
  • Click on "Add key", then select "Create new key"
  • Keep JSON selected as the key type, and click on "Create"
  • A json file will be created and downloaded onto your computer. Keep this safe as it will be needed to authenticate the API calls

Learn more about creating and managing service account keys.

Step 5: Use Google's OAuth libraries to generate an access token

Google provides libraries to help generate access tokens, which can be used to make the API calls. Learn about how to generate credentials for service accounts here:

https://developers.google.com/identity/protocols/oauth2/service-account#authorizingrequests

The scope for the AdSense Platforms API is as follows: https://www.googleapis.com/auth/adsense

Python example

from google.auth.transport import requests
from google.oauth2 import service_account

CREDENTIAL_SCOPES = ["https://www.googleapis.com/auth/adsense"]
CREDENTIALS_KEY_PATH = 'service.json'

def get_service_account_token():
  credentials = service_account.Credentials.from_service_account_file(
          CREDENTIALS_KEY_PATH, scopes=CREDENTIAL_SCOPES)
  credentials.refresh(requests.Request())
  return credentials.token

At this stage, you're ready to start calling the APIs. As client libraries are not supported for the AdSense Platform API yet, direct HTTP requests have to be made instead. The access token should be included as a header in the HTTP request. The header should look like this:

Authorization: OAuth <credentials>

Examples are included in the API pages.