Google Affiliate Network API PHP Script

I have been, or can be if you click on a link and make a purchase, compensated via a cash payment, gift, or something else of value for writing this post. Regardless, I only recommend products or services I use personally and believe will be good for my readers.

Since this was for myself, and not a web-based service that would be used by others, the Simple API method of authorization was good enough. After getting it to work with Picasa, but not GAN, I asked for some help and was introduced to the OAuth 2.0 Playground. This showed me the headers I’d need to send, and how OAuth 2.0 works.

For the record: the Google Affiliate Network API does NOT support Simple API Access. You cannot access the service with an API Key and IP locking.

I now have a working script, and have written up step-by-step instructions on how you can pull orders from GAN automatically each day.

This script is not complete, as it’s up to you to do something with the data once you have it. You can also modify the final call to pull advertisers, instead of orders.

Copy this to a new file (double-click to select-all. I named mine gan.php)

Make sure $cRedirectURI is correct. Then save and upload gan.php (yes, there will be some empty variables… that’s OK!), and run it via command-line or a browser. You’ll see:

[esnagel@ec2 temp]$ php gan.php
Go to
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=xxxxxxxxxxxx.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgan.readonly
and enter the given value into $cAuthCode

Go to this URL while logged into the Google account associated with your Google Affiliate Network (usually you@gmail.com, but if you’re like me and use Google Apps, you’ll have to open an Incognito window in Chrome to login)

At this point, it’s up to you to loop through the items array and do what you want with the data.

I’ll answer some technical questions in the comments, but for the complicated questions, I’ll direct you to my PHP Programmer page where you can email or call me to describe your project and receive an estimate.

Brandon Carlson

Google doesn’t specify the date format for eventDateMax, I’ve tried several different variations (mm/dd/YYYY, dd/mm/YYYY, mm/dd/yy, dd/mm/yy, and even unix timestamp). Do you know what format we’re supposed to use?

That’s what this script is doing. While I store the variables in the script (as I’m the only account I need to pull data from) I have used this as the basis of a script for a client of mine, and we store the GAN ids in a database.

Namrata Mathur

– When we first time run this script, it generates the URL for AuthCode.
– Then we open that URL in our browser.
– If we are not logged in to our GAN account then it we sow the login page,
– and at last we saw the message to allow access (the dialog box which you have mentioned as “GAN API Allow Access”).
– After clicking on allow access button we get the AuthCode.

My requirement is to generate/get AuthCode at runtime without using above process. So I want to know if it is possible. If yes then how?

No, this is not possible, as the AuthCode is specific to the Google account that you’re logged in with.

I guess I shouldn’t say it’s not possible… in theory you could automate a Google login, hit the URL, authenticate the app then copy the AuthCode, but I’m not sure how to go about doing that. The first step would be to see if you can simulate a login w/ your GAN username / password.

Namrata Mathur

Thanks again, actually I am using .NET client library for GAN API. I was lurking here coz I wanted to findout a working code so that I can use the login used in that code, and your code was very helpfull for me. I will try to implement your suggetion in my code.

Make sure you’re using the same account. For example, my GAN account is esnagel@gmail.com, but everything else I use is eric@ericnagel.com. So if you put in your publisherID for one account, then authorize the other, it won’t work.

Brad Moore

So I’m assuming from this discussion thread that there is no way to automate the login and authentication to GAN? I have a script that connects to each of the other networks (i.e. CJ, Linkshare, and Pepperjam) to retrieve transaction details and can automate everything with curl. This script runs with a kron job and is not user attended. I would like to add GAN access to this script, but it doesn’t sound possible.

Am I understanding correctly? And if so, where could I go to learn how to simulate a browser session with curl? I currently use curl to send post requests to a remote site and then process the return data as json, xml, or straight text. What I don’t know how to do is how to login to google and how I would respond to an alert box on the page such as the GAN Reporting is requesting permission… popup.

Chris

I struggled with this for a week (was receiving a blank refresh token). After trying a few different scripts, I came back and found the issue with this script. In the refresh token call, you have the redirect_url spelled incorrectly with a I rather than a L.