Data Export API + OAuth 2.0

123 views
Skip to first unread message

Ray West

unread,
Nov 11, 2011, 8:46:59 AM11/11/11
to google-analytics...@googlegroups.com
Hi there, I'm trying to figure out if it's possible to do API requests to the Data Export API using Oauth 2.0 authentication. Any help would be greatly appreciated.

Jeetendra S.

unread,
Nov 12, 2011, 1:45:09 PM11/12/11
to google-analytics...@googlegroups.com
Yeah, in fact OAuth2.0 is the recommended auth mechanism. You can refer the documentation at http://code.google.com/apis/accounts/docs/OAuth2.html.

Ray West

unread,
Nov 24, 2011, 1:32:33 AM11/24/11
to google-analytics...@googlegroups.com
HI, thanks for the reply. I have already gone through the OAuth2 specification and link you posted above. I was hoping that someone could point me to a working example of interfacing with both Analytics API and OAuth2 protocol. Thank you for any help.

Leonardo Naressi

unread,
Nov 24, 2011, 7:23:40 AM11/24/11
to google-analytics...@googlegroups.com
Hi Ray, 

You can see a example of OAuth2 and Analytics API here:
https://code.google.com/apis/analytics/docs/mgmt/v3/mgmtPython.html

I've implemented this in one of my project and it works well. Of course you need a API Key and authorizations from API Console (https://code.google.com/apis/console/b/0/?pli=1)

But, this example uses the Management API, that only exposes the Account information and not the report data.
The Data Export API is currently in V2.4 and does not have a native way to support Oauth2 through GData (at least, in my tests, it didn't run)
Google is working on update all the APIs to the new model it's a matter of time to get a good and simple solution.

But, meanwhile, to make Oauth2 works with legacy GData and Analytics v1, v2.4, I managed to create a custom class that applies the OAuth2 Token to the http header used in older GData versions.

Take a look and how to do that. Youll need to get the access_token from the credentials object shown in the earlier example, and simply uses an modify_request method who add some headers to http request and it's called as a "callback" by the old gdata library.

This solutions is currently working on my own code. Check if it fits for your needs:

class OAuth2Token(object):
def __init__(self, access_token):
self.access_token = access_token

def modify_request(self, http_request): 
"""Modify the request headers to add the appropriate
Authorization header."""
if http_request.headers == None:
http_request.headers = {}
http_request.headers['authorization'] = 'OAuth ' + self.access_token
http_request.headers['GData-Version'] =  '2'

return http_request

analytics_client = gdata.analytics.client.AnalyticsClient(
source=SOURCE_APP_NAME,
auth_token=OAuth2Token(credentials.access_token))
data_query = gdata.analytics.client.DataFeedQuery({
       'ids': self.tableId,
       'start-date': oRange['startDate'].isoformat(),
       'end-date': oRange['endDate'].isoformat(),
       'dimensions': '',
       'metrics': ','.join(['ga:visitors',
'ga:percentNewVisits',
'ga:visits',
'ga:avgTimeOnSite',
'ga:pageviews',
'ga:entranceBounceRate',
'ga:pageviewsPerVisit',
'ga:goalCompletionsAll',
'ga:goalConversionRateAll']),
       'sort': '',
       'filters': '',
       'max-results': '50'})
data_feed = analytics_client.GetDataFeed(data_query)

I hope it helps you...

Leonardo Naressi
CIO

L...@digitalinc.com.br
skype: leonardonaressi
twitter: @leonaressi

_________________________________________________________


Av. Nações Unidas, 12.495 - 3º Andar - Brooklin - São Paulo
www.digitalinc.com.br
@_digitalinc




On Thu, Nov 24, 2011 at 4:32 AM, Ray West <The....@gmail.com> wrote:
HI, thanks for the reply. I have already gone through the OAuth2 specification and link you posted above. I was hoping that someone could point me to a working example of interfacing with both Analytics API and OAuth2 protocol. Thank you for any help.

--
You received this message because you are subscribed to the Google Groups "GA Data Export API" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-analytics-data-export-api/-/8yX_dmL0wu0J.

To post to this group, send email to google-analytics...@googlegroups.com.
To unsubscribe from this group, send email to google-analytics-data-...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-analytics-data-export-api?hl=en.


Reply all
Reply to author
Forward
0 new messages