def gather_keys():
    # setup
    pp = pprint.PrettyPrinter(indent=4)
    print('** OAuth Python Library Example **\n')
    client = FitbitOauthClient(CLIENT_KEY, CLIENT_SECRET)

    # get request token
    print('* Obtain a request token ...\n')
    token = client.fetch_request_token()
    print('RESPONSE')
    pp.pprint(token)
    print('')

    print('* Authorize the request token in your browser\n')
    stderr = os.dup(2)
    os.close(2)
    os.open(os.devnull, os.O_RDWR)
    webbrowser.open(client.authorize_token_url())
    os.dup2(stderr, 2)
    try:
        verifier = raw_input('Verifier: ')
    except NameError:
        # Python 3.x
        verifier = input('Verifier: ')

    # get access token
    print('\n* Obtain an access token ...\n')
    token = client.fetch_access_token(verifier)
    print('RESPONSE')
    pp.pprint(token)
    print('')
def gather_keys():
    # setup
    pp = pprint.PrettyPrinter(indent=4)
    print('** OAuth Python Library Example **\n')
    client = FitbitOauthClient(CLIENT_KEY, CLIENT_SECRET)

    # get request token
    print('* Obtain a request token ...\n')
    token = client.fetch_request_token()
    print('RESPONSE')
    pp.pprint(token)
    print('')

    print('* Authorize the request token in your browser\n')
    stderr = os.dup(2)
    os.close(2)
    os.open(os.devnull, os.O_RDWR)
    webbrowser.open(client.authorize_token_url())
    os.dup2(stderr, 2)
    try:
        verifier = raw_input('Verifier: ')
    except NameError:
        # Python 3.x
        verifier = input('Verifier: ')

    # get access token
    print('\n* Obtain an access token ...\n')
    token = client.fetch_access_token(verifier)
    print('RESPONSE')
    pp.pprint(token)
    print('')
Beispiel #3
0
def fitbitAuth(config, OAUTH_SECRETS):
    CLIENT_KEY = config.get("fitbit", "consumer_key")
    CLIENT_SECRET = config.get("fitbit", "secret_key")

    request_token_url = 'https://api.fitbit.com/oauth/request_token'
    access_token_url = 'https://api.fitbit.com/oauth/access_token'
    authorize_url = 'https://www.fitbit.com/oauth/authorize'

    try:
        f = open(OAUTH_SECRETS, "rb")
        token = pickle.load(f)
        f.close()
    except:
        logger.error("Couldn't open %s, reloading..." % OAUTH_SECRETS)
        token = False

    if(not token):
        pp = pprint.PrettyPrinter(indent=4)
        client = FitbitOauthClient(CLIENT_KEY, CLIENT_SECRET)

        print('* Obtain a request token ...\n')
        token = client.fetch_request_token()
        print('RESPONSE')
        pp.pprint(token)
        print('')

        print('* Authorize the request token in your browser\n')

        print client.authorize_token_url()

        try:
            verifier = raw_input('Verifier: ')
        except NameError:
            # Python 3.x
            verifier = input('Verifier: ')

        # get access token
        print('\n* Obtain an access token ...\n')
        token = client.fetch_access_token(verifier)
        print('RESPONSE')
        pp.pprint(token)
        print('')

        print token
        print "Access key:", token['oauth_token']
        print "Access Secret:", token['oauth_token_secret']

        f = open(OAUTH_SECRETS, "w")
        pickle.dump(token, f)
        f.close()

    return fitbit.Fitbit(
        CLIENT_KEY,
        CLIENT_SECRET,
        resource_owner_key=token['oauth_token'],
        resource_owner_secret=token['oauth_token_secret'])
Beispiel #4
0
def hello_world():
    app.logger.debug("Init FitbitOauthClient")
    client = FitbitOauthClient(CLIENT_KEY, CLIENT_SECRET)

    app.logger.debug("Get request token")
    token = client.fetch_request_token()
    app.logger.debug('RESPONSE')
    app.logger.debug(token)

    # Save the oauth token for use in callback()
    try:
        session['oauth_token'] = token
    except:
        print "Unexpected error:", sys.exc_info()
        raise

    app.logger.debug('* Authorize the request token in your browser\n')
    return render_template('auth.html', link=client.authorize_token_url())
Beispiel #5
0
def callback():
    client = FitbitOauthClient(CLIENT_KEY, CLIENT_SECRET)
    oauth_verifier = request.args.get('oauth_verifier')
    oauth_token = session['oauth_token']
    token = client.fetch_access_token(oauth_verifier, token=oauth_token)
    conn = sqlite3.connect(DATABASE)
    c = conn.cursor()
    # Check if the user exists
    c.execute("SELECT * FROM users  WHERE encoded_user_id = '%s'" %
              token['encoded_user_id'])
    if c.fetchone() != None:
        # Delete the old record
        c.execute("DELETE FROM users WHERE  encoded_user_id = '%s'" %
                  token['encoded_user_id'])
    c.execute("INSERT INTO users VALUES ('%s', '%s', '%s')" %
              (token['encoded_user_id'], token['oauth_token'],
               token['oauth_token_secret']))
    conn.commit()
    conn.close()
    return render_template('callback.html')
Beispiel #6
0
def gather_keys():
    # setup
    client = FitbitOauthClient(CLIENT_KEY, CLIENT_SECRET)

    # get request token
    token = client.fetch_request_token()

    stderr = os.dup(2)
    os.close(2)
    os.open(os.devnull, os.O_RDWR)
    webbrowser.open(client.authorize_token_url())
    os.dup2(stderr, 2)
    try:
        verifier = raw_input('Verifier: ')
    except NameError:
        # Python 3.x
        verifier = input('Verifier: ')

    # get access token
    token = client.fetch_access_token(verifier)
    return token
Beispiel #7
0
def gather_keys():
    
    print('* Obtain a request token ...\n')
    client = FitbitOauthClient(client_key, client_secret)
    token = client.fetch_request_token()
    
    stderr = os.dup(2)
    os.close(2)
    os.open(os.devnull, os.O_RDWR)
    try:
        webbrowser.get().open(client.authorize_token_url())
    finally:
        os.dup2(stderr, 2)
        
    print('* Authorize the request token in your browser\n')
    time.sleep(3)
    
    verifier = input('\nPlease input PIN: ')
        
    # get access token
    print('\n* Obtain an access token ...\n')
    token = client.fetch_access_token(verifier)

    global user_id, user_key, user_secret

    user_key = token['oauth_token']
    user_secret = token['oauth_token_secret']

    print('* Your user key is %s and your user secret is %s. These will be saved in config.cfg.' % (user_key, user_secret))
    
    # lets create that config file for next time...
    cfgfile = open("./config.cfg",'w')

    # add the settings to the structure of the file, and lets write it out...
    config.add_section('Passkey')
    config.set('Passkey','user_key', user_key)
    config.set('Passkey','user_secret', user_secret)
    config.write(cfgfile)
    cfgfile.close()