If you are not a Google Engineer, OAuth2 for Google APIs can be a nightmare without good examples. If you think Facebook was bad at providing instructions, wait to get lost in a Microsoft-style ocean of useless information provided by Google on its new V3 gdata….
I assume you have curl installed, and here is a step-by-step guide on how to retrieve Google Analytics data using curl, which for me remains the best way to test any API.
- Visit Core Reporting API, and get confused by the amount of (not very useful) information ;
- On your browser, log-in at Google with the account you want to use to access Analytics info;
- Go to the APIs console. Click on create a “project”. Put “status on” for Analytics. Possibly rename the project going to the left menu “API Project” –the name given by default to the project;
- Click on “API Access”. Click on “Create an OAuth 2.0 client ID”. Give any name (this name is the one which would be seen by users, but here the only user will be you, downloading your data). Chose “Installed Application” and then “Create ID”. These infos will appear:Client ID for installed applications
Client ID: 1234567890.apps.googleusercontent.comClient secret: xywzxywzxywzxywzxywzRedirect URIs: urn:ietf:wg:oauth:2.0:oob http://localhost
- Go to Using OAuth 2.0 for Installed Apps (hardly useful), form the following URL and visit it with the browser where you logged in with your Analytics account (I had to guess the scope, could not find a page where all scope values are listed!):
https://accounts.google.com/o/oauth2/auth? scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics& redirect_uri=urn:ietf:wg:oauth:2.0:oob& response_type=code& client_id=1234567890.apps.googleusercontent.com
- Allow the access, of course, and copy the code which should look like

- Go to the terminal and use the command below using your own “code” from above, “client_id” etc:
curl -H "Content-Type: application/x-www-form-urlencoded" -d 'code=4/v6xr77ewYqjkslsdUOKwAzu &client_id=1234567890.apps.googleusercontent.com &client_secret=xywzxywzxywzxywzxywz &redirect_uri=urn:ietf:wg:oauth:2.0:oob &grant_type=authorization_code' https://accounts.google.com/o/oauth2/token
- You’ll get a JSON like this one:
{ "access_token" : "ya29.AHES6Zkjhkjhahskjhskkskjh", "token_type" : "Bearer", "expires_in" : 3600, "refresh_token" : "1/HH9E7k5D0jakjhsd7askdjh7899a8sd989" } - If you curl:
curl 'https://www.googleapis.com/oauth2/v1/tokeninfo? access_token=ya29.AHES6Zkjhkjhahskjhskkskjh'
you’ll get something like:
{ "issued_to": "562211803675.apps.googleusercontent.com", "audience": "562211803675.apps.googleusercontent.com", "scope": "https://www.googleapis.com/auth/analytics", "expires_in": 3556 }(see below how to renew the token without having to go to ask for another “code” as in point 5)
- Done
curl 'https://www.googleapis.com/analytics/v3/management/ accounts?access_token=ya29.AHES6Zkjhkjhahskjhskkskjh'
will give you all info about your accounts, more info on the Management API REST page.
- How to get the data? A nice hint I could not find anywhere is that the URL of the new Analytics Dashboard provides account, web property and profile:
https://google.com/analytics/web/#dashboard/default/ aACCOUNTwWEBPROPERTYpPROFILE/
- Which means you can get the data with the following address:
curl 'https://www.googleapis.com/analytics/v3/data/ga? ids=ga:PROFILE&metrics=ga:visits&start-date=2011-12-01& end-date=2011-12-08& access_token=ya29.AHES6Zkjhkjhahskjhskkskjh'
Renew the token
You have to use the “refresh_token” received in point 8:
curl -d "client_id=562211803675.apps.googleusercontent.com &client_secret=ZQxoOBGbvMGnZOYUrVIDXrgl &refresh_token=1/HH9E7k5D0jakjhsd7askdjh7899a8sd989 &grant_type=refresh_token" https://accounts.google.com/o/oauth2/token
and you’ll get a new access_token.




Finally, an enlightened and perverse mind has found the way to become shamefully rich in the water industry. After three months in the US, interviewing executives and analysts from the water industry, I begun wondering if there was anybody out there happy with what so many people call “the blue gold”.



