Demo entry 5624874

mypythoncode

   

Submitted by anonymous on Jul 05, 2016 at 17:35
Language: Python. Code size: 1.8 kB.

def get_credentials():

    # 1. build the path where the credentials file will be located
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, 'Desktop\DataMinig\Python_Samples')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,'credentials_file.json')
    print('credential file '+credential_path)

    # 2. next, build the path where the client secrets file was stored (previously downloaded from Google Console)
    CLIENT_SECRET_FILE = os.path.join(credential_dir,'client_secret_file.json')
    print('client file '+CLIENT_SECRET_FILE)

    # 3. first, we try to read in the credentials file 
    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()

    # 4. now, we need to check if we have a valid credentials file in place
    #     if the credentials file is fine, then we jump to the return statement and end the function
    #     however, if this is not the case i.e. there is no credentials file or the existing one is corrupt, out of date etc., we need to create a new one.
    #     the authorization process calls the default web browser to open the Google Authorization page, which as to be confirmed by the user.
    if not credentials or credentials.invalid:
        # the variables "scopes" and "application_name" are global variables 
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: 
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).