Packages up the data from Analytics.
[ckanext-ga-report.git] / ckanext / ga_report / ga_auth.py
blob:a/ckanext/ga_report/ga_auth.py -> blob:b/ckanext/ga_report/ga_auth.py
import os import os
import httplib2 import httplib2
from apiclient.discovery import build from apiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage from oauth2client.file import Storage
from oauth2client.tools import run from oauth2client.tools import run
   
from pylons import config from pylons import config
   
   
def _prepare_credentials(token_filename, credentials_filename): def _prepare_credentials(token_filename, credentials_filename):
""" """
Either returns the user's oauth credentials or uses the credentials Either returns the user's oauth credentials or uses the credentials
file to generate a token (by forcing the user to login in the browser) file to generate a token (by forcing the user to login in the browser)
""" """
storage = Storage(token_filename) storage = Storage(token_filename)
credentials = storage.get() credentials = storage.get()
   
if credentials is None or credentials.invalid: if credentials is None or credentials.invalid:
flow = flow_from_clientsecrets(credentials_filename, flow = flow_from_clientsecrets(credentials_filename,
scope='https://www.googleapis.com/auth/analytics.readonly', scope='https://www.googleapis.com/auth/analytics.readonly',
message="Can't find the credentials file") message="Can't find the credentials file")
credentials = run(flow, storage) credentials = run(flow, storage)
   
return credentials return credentials
   
   
def init_service(token_file, credentials_file): def init_service(token_file, credentials_file):
""" """
Given a file containing the user's oauth token (and another with Given a file containing the user's oauth token (and another with
credentials in case we need to generate the token) will return a credentials in case we need to generate the token) will return a
service object representing the analytics API. service object representing the analytics API.
""" """
http = httplib2.Http() http = httplib2.Http()
   
credentials = _prepare_credentials(token_file, credentials_file) credentials = _prepare_credentials(token_file, credentials_file)
http = credentials.authorize(http) # authorize the http object http = credentials.authorize(http) # authorize the http object
   
return build('analytics', 'v3', http=http) return credentials.access_token, build('analytics', 'v3', http=http)
   
   
def get_profile_id(service): def get_profile_id(service):
""" """
Get the profile ID for this user and the service specified by the Get the profile ID for this user and the service specified by the
'googleanalytics.id' configuration option. This function iterates 'googleanalytics.id' configuration option. This function iterates
over all of the accounts available to the user who invoked the over all of the accounts available to the user who invoked the
service to find one where the account name matches (in case the service to find one where the account name matches (in case the
user has several). user has several).
""" """
accounts = service.management().accounts().list().execute() accounts = service.management().accounts().list().execute()
   
if not accounts.get('items'): if not accounts.get('items'):
return None return None
   
accountName = config.get('googleanalytics.account') accountName = config.get('googleanalytics.account')
  if not accountName:
  raise Exception('googleanalytics.account needs to be configured')
webPropertyId = config.get('googleanalytics.id') webPropertyId = config.get('googleanalytics.id')
  if not webPropertyId:
  raise Exception('googleanalytics.id needs to be configured')
for acc in accounts.get('items'): for acc in accounts.get('items'):
if acc.get('name') == accountName: if acc.get('name') == accountName:
accountId = acc.get('id') accountId = acc.get('id')
   
webproperties = service.management().webproperties().list(accountId=accountId).execute() webproperties = service.management().webproperties().list(accountId=accountId).execute()
   
profiles = service.management().profiles().list( profiles = service.management().profiles().list(
accountId=accountId, webPropertyId=webPropertyId).execute() accountId=accountId, webPropertyId=webPropertyId).execute()
   
if profiles.get('items'): if profiles.get('items'):
return profiles.get('items')[0].get('id') return profiles.get('items')[0].get('id')
   
return None return None