[null,null,[],[[["\u003cp\u003eService accounts are used to authenticate API calls for creating and managing AdSense sub-accounts.\u003c/p\u003e\n"],["\u003cp\u003eA Google Cloud project and a service account with appropriate permissions are required for API access.\u003c/p\u003e\n"],["\u003cp\u003eYou must enable the AdSense Platform API for your Google Cloud project and generate a service key for authentication.\u003c/p\u003e\n"],["\u003cp\u003eGoogle's OAuth libraries are used to generate access tokens, which are included in HTTP request headers for API calls.\u003c/p\u003e\n"],["\u003cp\u003eDirect HTTP requests are necessary to interact with the AdSense Platform API, as client libraries are not yet supported.\u003c/p\u003e\n"]]],["To authenticate AdSense Platform API calls, you must first create or use an existing Google Cloud project, then create a service account. Share this service account with Google to enable AdSense access. Enable the AdSense Platform API for the project, and generate a service key (JSON file) for access token creation. Use Google's OAuth libraries, with the specified scope, to generate the access token. Include this token in the \"Authorization\" header of direct HTTP requests to the API.\n"],null,["Authenticating API calls\n------------------------\n\nIn this example, we're showing how service accounts can be used to call the AdSense Platforms API to create and manage sub-accounts.\n\n### Step 1: Create a new Google Cloud project (or use an existing one)\n\nIf you have an existing Google Cloud project, feel free to use that. Otherwise, follow the guide below on setting up a new project:\n\n\u003chttps://cloud.google.com/resource-manager/docs/creating-managing-projects\u003e\n\n### Step 2: Create a service account\n\nUsing service accounts is the best way to create sub-accounts. Follow these steps to create your service account:\n\n- Visit the [service accounts](https://console.cloud.google.com/iam-admin/serviceaccounts) page in Google Cloud\n- You can either use an existing service account, or create a new one:\n - Click on \"+ Create service account\"\n - Fill in the \"Service account details\" form\n - Steps 2 and 3 on the page (granting access to projects and users) are optional\n\nLearn more about [creating and managing service accounts](https://cloud.google.com/iam/docs/creating-managing-service-accounts).\n\nOnce 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.\n\n### Step 3: Enable the AdSense Platform API for your Google Cloud project\n\nThe AdSense Platform API isn't discoverable, meaning you have to visit the following link to enable the it for your project:\n\n\u003chttps://console.developers.google.com/apis/api/adsenseplatform.googleapis.com/overview\u003e\n\n### Step 4: Create a service key\n\nIn order to generate access tokensfor use in the API calls, you need to create a service key. Follow these steps:\n\n- Visit the [service accounts](https://console.cloud.google.com/iam-admin/serviceaccounts) page in Google Cloud\n- In the actions column, for the service account you want to use to create sub-accounts, click then click \"Manage keys\"\n- Click on \"Add key\", then select \"Create new key\"\n- Keep JSON selected as the key type, and click on \"Create\"\n- A json file will be created and downloaded onto your computer. Keep this safe as it will be needed to authenticate the API calls\n\nLearn more about [creating and managing service account keys](https://cloud.google.com/iam/docs/creating-managing-service-account-keys).\n\n### Step 5: Use Google's OAuth libraries to generate an access token\n\nGoogle 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:\n\n\u003chttps://developers.google.com/identity/protocols/oauth2/service-account#authorizingrequests\u003e\n\nThe scope for the AdSense Platforms API is as follows:\n`https://www.googleapis.com/auth/adsense`\n\n#### Python example\n\n**Note:** the `service.json` file is the key generated as part of step 4 above. \n\n from google.auth.transport import requests\n from google.oauth2 import service_account\n\n CREDENTIAL_SCOPES = [\"https://www.googleapis.com/auth/adsense\"]\n CREDENTIALS_KEY_PATH = 'service.json'\n\n def get_service_account_token():\n credentials = service_account.Credentials.from_service_account_file(\n CREDENTIALS_KEY_PATH, scopes=CREDENTIAL_SCOPES)\n credentials.refresh(requests.Request())\n return credentials.token\n\nAt 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: \n\n Authorization: OAuth \u003ccredentials\u003e\n\nExamples are included in the API pages."]]