This guide is for those who don’t want to spend hours trouble shooting or going through the myriad Google AdWords API Documentation to get the API up and running. This guide is best suited for developers who are creating tools for an AdWords Manager Account or single AdWords account as this does not go through the Auth2 Workflow as that requires having users logging in and keeping their sessions continuously tracked and refreshed.
$ = Terminal Command;
“some_word” = Code Variable or File Name;
Step 1 – Download Google AdWords API
There are two ways to do this. Download it or use Composer.
$ composer require googleads/googleads-php-lib
At this point, you should have the googleads library in your project. If you used composer, it’ll be within your vendor folder and you should see “composer.json” and “composer.lock” files. [See Figure 1]
Step 2 – Configuring the Auth.ini File
Now that you have the library, it’s time to configure the “auth.ini” file so it has all the correct credentials to use the API.
auth.ini file path: ./googleads/googleads-php-lib/src/Google/Api/Ads/AdWords/auth.ini
Now, open up the auth.ini file and it should look like this:
You’ve made it to the most complicated part of this whole setup. It was this complicated part that made me decide to create this guide in the first place. We need to get five points of data to fill in this auth.ini to work properly. Those include:
Once we have all these points complete, we can begin to use the API.
So first things first; let’s get the “developerToken” and the “userAgent”.
To get these, you will need to log into your AdWords account. Click on the little gear in the top right, and go to account settings.
Then in the left side navigation panel, you want to click AdWords API Center. This will bring you up to a screen that looks like this.
!! If you don’t see the AdWords API Center, you either don’t have the right permission level to view it, in which you will have to ask whoever does to increase your permission, or you haven’t signed up for the AdWords API in which case you will need to do so. !!
If you see the AdWords API Center, you should then be able to see the Developer token at the very top in the AdWords API Center. Grab that value and put it into the “developerToken” variable in “auth.ini”.
Next, grab the Company Name and put that into the “userAgent” variable. It’s important that these values line up with what’s in AdWords, otherwise it will not work.
We now have 2/5 of the data points complete, time to get the “client_id” and “client_secret”.
To get these data points, we will need to register an OAuth2 Application in the Google Console. To do that, go here:
Once you log in, you should see something that looks similar to this.
Name the project whatever you want, and hit create project.
Now that the project is created, it needs credentials and it needs to have a consent screen that lets the user know this application is authorizing the application.
Firstly, you should have automatically been directed to the credentials tab after creating your project, and you should see a box in the middle that says create credentials. Click it and select OAuth Client ID. It is critical that you select the correct one.
You should now be in a screen that looks like the figure below, and it will say you first need to configure a consent screen. Click the “Configure consent screen” button.
You should now be on the Oauth consent screen page. The only thing that’s required here is to put a Product Name as that’s what will show in the popup when your application requests access. It will say “Your Product Name” is requesting access to the following… If you want to put a logo, or any of the other optional fields, feel free. I’m going to name mine Simple Guide Example ZZ. You will see what that looks like later. Once you’re done, click “Save” and it will take you back to the Credentials Page.
On the credentials page, you want to select “Other”. This works for the type of application we’re making which is considered an installed application. Name the credential whatever you’d like, it doesn’t matter.
Once you’re done, hit create, and you will get a popup with your client ID and client secret.
Copy the Client ID and Client Secret and paste them into your auth.ini file in the “client_id” and “client_secret”.
You now have 4/5 data points needed for your “auth.ini”. It should look like this:
Now we’re ready to get the last piece of the puzzle which is your refresh token. This allows your application to continuously access the API after giving it access once. To do this, we’re going to need to go into the terminal and run php on the GetRefreshToken.php file.
GetRefreshToken.php Path = ./googleads/googleads-php-lib/examples/AdWords/Auth/GetRefreshToken.php
Navigate to the file, and in your terminal we’re going to run:
$ php GetRefreshToken.php
It should output a link to a website and ask for the Authorization Code.
Copy the Link and paste it into your Browser. It will say [whatever you named your application] would like to Manage your AdWords Campaign.
Hit allow, and it will return the code you need to paste into the terminal.
Now paste it into the terminal, and it will generate a refresh token.
Copy the refresh token, paste it into your auth.ini file and we’re done. Now we can run our first API call using one of the examples given.
Step 3 – Test
So to test, the easiest way to check without modifying any files is to run the GetReportFields.php file.
GetReportFields.php path = ./googleads/googleads-php-lib/examples/AdWords/[v201605 or whatever version you’re using]/Reporting/GetReportFields.php
Once you’ve navigated to the folder, run:
$ php GetReportFields.php
You should see an output that looks similar to this:
You have now completed your first successful Google AdWords API call. Play around with the other examples; some of them may require certain data points to be updated so keep that in mind. If you have any questions, comments, or concerns feel free to email me – firstname.lastname@example.org.