Example #1
0
def main():
    """
    main function
    """

    with open(settings.SERVICE_ACCOUNT_JSON) as json_file:

        json_data = json.load(json_file)
        #print json_data
        credentials = SignedJwtAssertionCredentials(
            json_data['client_email'],
            json_data['private_key'],
            scope='https://apps-apis.google.com/a/feeds/emailsettings/2.0/',
            access_type="offline",
            approval_prompt="force",
            sub=email)

    storage = Storage(settings.STORAGE_FILE)

    storage.put(credentials)
    #credentials = storage.get()
    credentials.get_access_token()

    if test:
        print credentials.__dict__
        #print credentials.access_token

    http = httplib2.Http()
    auth = credentials.authorize(http)
    # refresh does not seem to work
    credentials.refresh(http)
    print credentials.__dict__
Example #2
0
def main():
    '''
    main function
    '''

    with open(settings.SERVICE_ACCOUNT_JSON) as json_file:

        json_data = json.load(json_file)
        #print json_data
        credentials = SignedJwtAssertionCredentials(
            json_data['client_email'],
            json_data['private_key'],
            scope=
            'https://www.googleapis.com/auth/admin.reports.audit.readonly',
            access_type='offline',
            approval_prompt='force',
            sub=email)

    storage = Storage(settings.STORAGE_FILE)

    storage.put(credentials)
    #credentials = storage.get()
    credentials.get_access_token()

    if test:
        print credentials.__dict__
        #print credentials.access_token

    http = httplib2.Http()
    auth = credentials.authorize(http)
    # refresh does not seem to work
    credentials.refresh(http)
    print credentials.__dict__
Example #3
0
def get_cred(email, scope, service_account=None):
    """
    Establishes the proper credentials to access the
    Google API resource
    """

    if scope[0:4] != "http":
        scope='https://www.googleapis.com/auth/{}'.format(scope),
    if not service_account:
        service_account = settings.SERVICE_ACCOUNT_JSON
    with open(service_account) as json_file:
        json_data = json.load(json_file)
        credentials = SignedJwtAssertionCredentials(
            json_data['client_email'],
            json_data['private_key'],
            scope=scope,
            #access_type="offline",
            #approval_prompt = "force",
            token_uri='https://accounts.google.com/o/oauth2/token',
            sub=email
        )

    credentials.get_access_token()

    return credentials
def main():
    """
    main function
    """

    with open(settings.SERVICE_ACCOUNT_JSON) as json_file:

        json_data = json.load(json_file)
        #print json_data
        credentials = SignedJwtAssertionCredentials(
            json_data['client_email'],
            json_data['private_key'],
            scope='https://apps-apis.google.com/a/feeds/emailsettings/2.0/',
            access_type="offline",
            approval_prompt = "force",
            sub=email
        )

    storage = Storage(settings.STORAGE_FILE)

    storage.put(credentials)
    #credentials = storage.get()
    credentials.get_access_token()

    if test:
        print credentials.__dict__
        #print credentials.access_token

    http = httplib2.Http()
    auth = credentials.authorize(http)
    # refresh does not seem to work
    credentials.refresh(http)
    print credentials.__dict__
Example #5
0
def analytics(context, view_id=None, next = None):

    # The scope for the OAuth2 request.
    SCOPE = 'https://www.googleapis.com/auth/analytics.readonly'
    token = ""



    ggsettings = GoogleAPISettings.objects.first()

    if ggsettings and ggsettings.account_key_file:

        if not view_id:
            view_id = "%s" % int(ggsettings.analytics_default_view_id)

        _key_data = json.load(ggsettings.account_key_file)

        # Construct a credentials objects from the key data and OAuth2 scope.
        try:
            _credentials = SignedJwtAssertionCredentials(
                _key_data['client_email'],
                _key_data['private_key'],
                'https://www.googleapis.com/auth/analytics.readonly',
                # token_uri='https://accounts.google.com/o/oauth2/token'
            )
            token = _credentials.get_access_token().access_token
        except Exception, e:
            print e.message
            token = ""
Example #6
0
def analytics(context, next=None):

    if not getattr(settings, 'OA_ANALYTICS_CREDENTIALS_JSON', None):
        raise ImproperlyConfigured(
            'Analytics service account json path missing')

    if not getattr(settings, 'OA_ANALYTICS_VIEW_ID', None):
        raise ImproperlyConfigured('Analytics view id missing')

    # The scope for the OAuth2 request.
    SCOPE = 'https://www.googleapis.com/auth/analytics.readonly'

    # The location of the key file with the key data.
    KEY_FILEPATH = settings.OA_ANALYTICS_CREDENTIALS_JSON

    # Load the key file's private data.
    with open(KEY_FILEPATH) as key_file:
        _key_data = json.load(key_file)

    # Construct a credentials objects from the key data and OAuth2 scope.
    _credentials = SignedJwtAssertionCredentials(_key_data['client_email'],
                                                 _key_data['private_key'],
                                                 SCOPE)

    return {
        'token': _credentials.get_access_token().access_token,
        'view_id': settings.OA_ANALYTICS_VIEW_ID
    }
Example #7
0
def get_cred(email, scope, service_account=None):
    """Establishes the proper credentials to access the Google API resource."""
    if scope[0:4] != "http":
        scope = 'https://www.googleapis.com/auth/{}'.format(scope),
    if not service_account:
        service_account = settings.SERVICE_ACCOUNT_JSON
    with open(service_account) as json_file:
        json_data = json.load(json_file)
        credentials = SignedJwtAssertionCredentials(
            json_data['client_email'],
            json_data['private_key'],
            scope=scope,
            #access_type="offline",
            #approval_prompt = "force",
            token_uri='https://accounts.google.com/o/oauth2/token',
            sub=email)

    credentials.get_access_token()

    return credentials
Example #8
0
def get_access_token(ga_key_filepath):
    # from https://ga-dev-tools.appspot.com/embed-api/server-side-authorization/
    # Defines a method to get an access token from the credentials object.
    # The access token is automatically refreshed if it has expired.

    # Load the key file's private data.
    with open(ga_key_filepath) as key_file:
        _key_data = json.load(key_file)

    # The scope for the OAuth2 request.
    SCOPE = 'https://www.googleapis.com/auth/analytics.readonly'

    # Construct a credentials objects from the key data and OAuth2 scope.
    _credentials = SignedJwtAssertionCredentials(
        _key_data['client_email'], _key_data['private_key'], SCOPE)

    return _credentials.get_access_token().access_token
Example #9
0
def get_access_token(ga_key_filepath):
    # from https://ga-dev-tools.appspot.com/embed-api/server-side-authorization/
    # Defines a method to get an access token from the credentials object.
    # The access token is automatically refreshed if it has expired.

    # Load the key file's private data.
    with open(ga_key_filepath) as key_file:
        _key_data = json.load(key_file)

    # The scope for the OAuth2 request.
    SCOPE = 'https://www.googleapis.com/auth/analytics.readonly'

    # Construct a credentials objects from the key data and OAuth2 scope.
    _credentials = SignedJwtAssertionCredentials(_key_data['client_email'],
                                                 _key_data['private_key'],
                                                 SCOPE)

    return _credentials.get_access_token().access_token
Example #10
0
 def get_google_account_token(account_key_file):
     # The scope for the OAuth2 request.
     SCOPE = 'https://www.googleapis.com/auth/analytics.readonly'
     token = None
     try:
         _key_data = json.load(account_key_file)
         _credentials = SignedJwtAssertionCredentials(
             _key_data['client_email'],
             _key_data['private_key'],
             'https://www.googleapis.com/auth/analytics.readonly',
             # token_uri='https://accounts.google.com/o/oauth2/token'
         )
         token = _credentials.get_access_token().access_token
     except Exception, e:
         if raven:
             raven.captureException()
         print "GOOGLE API TOKEN RETRIEVE ERROR:", e.message
         token = None
Example #11
0
import json
from oauth2client.client import SignedJwtAssertionCredentials

# scope for the OAuth2 endpoint
SCOPE = 'https://www.googleapis.com/auth/analytics.readonly'

# the location of the keyfile that has the keydata
KEY_FILEPATH = 'controllers/secret.json'

# load the key file's private data
with open(KEY_FILEPATH) as key_file:
    _key_data = json.load(key_file)

# make a credentials objects from the keydata and OAuth2 scope
_credentials = SignedJwtAssertionCredentials(_key_data['client_email'],
                                             _key_data['private_key'], SCOPE)

# defines a method to get an access token from the credentials object
# the access token is automatically refreshed if its expired
#def get_access_token();
#return _credentials.get_access_token().access_token

print _credentials.get_access_token().access_token
Example #12
0
# service-account.py

import json
from oauth2client.client import SignedJwtAssertionCredentials

# The scope for the OAuth2 request.
SCOPE = 'https://www.googleapis.com/auth/analytics.readonly'

# The location of the key file with the key data.
KEY_FILEPATH = '../key.json'

# Load the key file's private data.
with open(KEY_FILEPATH) as key_file:
  _key_data = json.load(key_file)

# Construct a credentials objects from the key data and OAuth2 scope.
_credentials = SignedJwtAssertionCredentials(
    _key_data['client_email'], _key_data['private_key'], SCOPE)

print('horse')
# Defines a method to get an access token from the credentials object.
# The access token is automatically refreshed if it has expired.
print(_credentials.get_access_token().access_token)