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 NaressiCIO
_________________________________________________________
Av. Nações Unidas, 12.495 - 3º Andar - Brooklin - São Paulo
www.digitalinc.com.br@_digitalinc