This article describes a simple way to grab some OAuth 2.0 tokens for use in an AdWords API application. OAuth is great for building tools that access third-party accounts, but is a hurdle when you're just getting started with the AdWords API or when you are just using your own account. Fortunately, Google provides a tool, called the OAuth Playground, that you can use to generate some tokens quickly.
Google's OAuth Playground
The OAuth Playground is a tool for learning how OAuth works. It presents you with a three-step process for selecting the services you want to authorize, generating an access token, and making API requests. However, the access token only lasts for an hour and you can't select the AdWords API at the moment, so you need to perform a few additional steps:
- Create your own client id and secret so you can generate new access tokens in your own code.
- Add the client id and secret to the playground.
- Configure the tool to use the AdWords API.
Generating a Client ID and Secret
You can sign up for a Client ID and Secret from the Google API Console. If you've used it before, the process is similar to generating tokens for the web flow, but you need to set the redirect URL to point back to the OAuth Playground at developers.google.com/oauthplayground instead of your own website.
If you haven't used the console before, you'll see the following screen after logging into your account:
Just press the Create Project button to generate a generic project, called “API Project”, then you will be taken to the dashboard. You create tokens using the API Access tab:
Pressing the Create and OAuth 2.0 Client ID… button will bring up a wizard to guide you through the token creation process. If you haven't created any tokens yet, then you'll be asked to enter some branding information first. Just enter a name for your application, and click on the Next button to specify the settings for your application:
Leave the Web application radio button checked, enter developers.google.com/oauthplayground into the textbox, then press Create client ID. Once this is complete, you'll be shown a screen containing your Client ID and Client Secret:
Adding the Client ID and Secret to the Playground
Now you need to enter your tokens into the settings dialog of the OAuth Playground:
Tick the Use your own OAuth credentials checkbox, then enter your Client ID and Client Secret into the OAuth Client ID and OAuth Client secret textboxes:
Selecting the AdWords API Scope
You can now generate some tokens using your own credentials, but the AdWords API isn't available in the list of APIs options, so you need to enter its scope into the box at the bottom of the list:
Generating Some Tokens for Your Account
Now that you've configured the tool, you can get an access and refresh token. Press the Authorize APIs button, and you will be taken to a dialog to authorize your account:
Pressing the Accept button will take you back to the OAuth playground with an initial code that you can use to generate full OAuth tokens:
Generate the full OAuth tokens by pressing the Exchange authorization code for tokens button:
Using the Tokens in Your Scripts
Now that you've got some tokens, you can now plug them into your scripts. Here's an example of pulling a keyword performance report using the PHP client library:
Once your Access Token has expired, you can refresh it using the following code:
If you're running this less than once per hour, you can just call the refresh code each time the script runs.