How to use v3 (Oauth2.0) for cron jobs

2,686 views
Skip to first unread message

Gerron Mulder

unread,
Aug 31, 2011, 5:19:20 PM8/31/11
to google-analytic...@googlegroups.com
We make use of a couple of cron jobs that synchronize new Google Analytics data with our data warehouse. Currently our cron jobs use our Google Analytics account details (email / password) to fetch all profiles, look-up any missing data points between a defined window using a defined interval (start-date / end-date combinations), and fetches the data for each of the profiles.

In order to main the same workflow, I would essentially have to replace (email, password) with (Client Id, Client Secret, and a refresh token)? And use those 3 values to fetch a access token for each time a cron job runs (each cron job runs only once a day)?

Do I also have to grant the "Google Analytics Project" access to all of our Google Analytics accounts that are already being "owned" by our Google Account?

Thanks!
Gerron

Nick

unread,
Sep 2, 2011, 8:33:11 PM9/2/11
to google-analytic...@googlegroups.com
Hi,

So the OAuth 2.0 flow is a bit different. All the details are here: http://code.google.com/apis/accounts/docs/OAuth2.html#IA

But the steps are:

1. register your project to use OAuth 2.0 for native apps.
2. use the client id and secret in your app
3. run the app and you'll get a URL
4. go to the url, login as the user you want to grant access to
5. you will get an authorization code
6. go back to your app and use the authorization code
7. the api gives you an access token and a refresh token
8. You save both tokens.

9. You use the access token to get authorized access to the API. This access token lasts for a period of time. Once the token expores, the API will return a 401 status code.
10. When you see a 401, you have to use the refresh token to get a new access token.
11. You save the new access token

Steps 9, 10 and 11 are what your app needs to do on it's own so you can cron your app.

If you use python, our getting started sample has all this built in: http://code.google.com/apis/analytics/docs/mgmt/v3/mgmtPython.html

-Nick




Thomas Winans

unread,
Dec 4, 2013, 6:32:02 PM12/4/13
to google-analytic...@googlegroups.com
Nick -

What does this look like today? What is the analog to native (console) app?

Thanks,
Tom

Thomas Winans

unread,
Dec 4, 2013, 6:38:03 PM12/4/13
to google-analytic...@googlegroups.com
Gerron -

Did you get this working? I am particularly curious given the changes lately for GA API v3 ... 

Thank you,
Tom

Nick

unread,
Dec 4, 2013, 7:45:07 PM12/4/13
to google-analytic...@googlegroups.com
The analytics samples directory in the Python client library should have the latest syntax for making this work: https://code.google.com/p/google-api-python-client/source/browse/#hg%2Fsamples%2Fanalytics

-Nick
Reply all
Reply to author
Forward
0 new messages