Well 1st of all my problem: more or less one year ago i wrote a nice python script, a shell script that every night downloads datas from my analytics account and save them to a database. It’s useful for my job because i can use this data to better analyze how my business web sites are going.
Now, more or less a week ago my script stopped to work. I had an error 401, limit rate exceeded you need to authenticate…
But my script was authenticated! Yes i used an old gdata api and a normal old ClientLogin API authorization protocol but it worked till last week.
Anyway i decided to switch to the Oauth 2.0 Authentication. There is a ton of documentation about how Oauth 2.0 works and there is a ton of documentation about how a Web Application and a Desktop Application can use Oauth 2.0 to be authenticated with Google API but there is really poor documentation about how a server side script can authenticate itself with google without an human presence.
So this is my short tutorial.
- You need an account on Google API Console
- You need a new project
- Click to “API Access” and “Create an OAuth 2.0 Client ID”
- Select “Service Account” and Create Client ID
- “Download Private Key” and save it in a secure place
- Log in in your analytics account and add the email address related to your new project to your analytics property
- Take note of your Client ID
- Install the google-api-python-client library to your server
We are ready to write code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
You are done. I won’t speak about the theory of how oauth works, this is a short tutorial. If you want you can learn more at https://developers.google.com/accounts/docs/OAuth2