Skip to content

Instantly share code, notes, and snippets.

@brettclare
Created May 2, 2016 23:51
Show Gist options
  • Save brettclare/b6618dd37980abf3e0fedc8b8b7f7437 to your computer and use it in GitHub Desktop.
Save brettclare/b6618dd37980abf3e0fedc8b8b7f7437 to your computer and use it in GitHub Desktop.
Enter file contents here#https://github.com/jay0lee/GAM/wiki/CreatingClientSecretsFile
#https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/installed-py#run
"""A simple example of how to access the Google Analytics API."""
#need a working WORK account with admin privliges, that is why the page doesn't come up
import httplib2
from googleapiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client import tools
import argparse
CLIENT_SECRETS = 'client_secrets.json'
# The Flow object to be used if we need to authenticate.
FLOW = flow_from_clientsecrets(
CLIENT_SECRETS,
scope='https://www.googleapis.com/auth/analytics.readonly',
message='%s is missing' % CLIENT_SECRETS
)
# A file to store the access token
TOKEN_FILE_NAME = 'credentials.dat'
def prepare_credentials():
parser = argparse.ArgumentParser(parents=[tools.argparser])
flags = parser.parse_args()
# Retrieve existing credendials
storage = Storage(TOKEN_FILE_NAME)
credentials = storage.get()
# If no credentials exist, we create new ones
if credentials is None or credentials.invalid:
credentials = tools.run_flow(FLOW, storage, flags)
return credentials
def initialize_service():
# Creates an http object and authorize it using
# the function prepare_creadentials()
http = httplib2.Http()
credentials = prepare_credentials()
http = credentials.authorize(http)
# Build the Analytics Service Object with the authorized http object
return build('analytics', 'v3', http=http)
if __name__ == '__main__':
service = initialize_service()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment