Demo entry 5624635



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

def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

        Credentials, the obtained credential.
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, 'Desktop\DataMinig\Python_Samples')
    if not os.path.exists(credential_dir):
    credential_path = os.path.join(credential_dir,'credentials_file.json')
    print('credential file '+credential_path)
    # The client secret file has to be downloaded manully from the google console site in JSON format
    CLIENT_SECRET_FILE = os.path.join(credential_dir,'client_secret_file.json')
    print('client file '+CLIENT_SECRET_FILE)

    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatibility with Python 2.6
            credentials =, store)
        print('Storing credentials to ' + credential_path)
    return credentials

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).