Exemplo n.º 1
0
def fitbit_auth():
    token = None
    error = None

    client_id = '227XNF'
    client_secret = '1a53508ac0bd0aa5ffa3a9f6de07cb9d'
    redirect_uri = ('https://%s/project/default/fitbit_auth' %
                     request.env.http_host)
    code =  request.vars.code

    oauth = FitbitOauth2Client(client_id, client_secret)

    print code
    try:
        token = oauth.fetch_access_token(code, redirect_uri)
    except MissingTokenError:
        error = 'Missing access token parameter.</br>Please check that you are using the correct client_secret'
    except MismatchingStateError:
        error ='CSRF Warning! Mismatching state'
    print token

    client = Fitbit(client_id, client_secret, access_token=token["access_token"], refresh_token=token["refresh_token"])

    f_id = db.fitbit_user_t.insert(
        user_email=auth.user.email,
        fitbit_user_id=token["user_id"],
        access_token=token["access_token"],
        refresh_token=token["refresh_token"],
        expires_at=token["expires_at"],
    )
    user = client.user_profile_get()
    goals = client.activities_daily_goal()
    wt_goal = client.body_weight_goal()
    print wt_goal
    u_id = db.user_t.insert(
        user_email=auth.user.email,
        dob=user["user"]["dateOfBirth"],
        sex=user["user"]["gender"],
        height=user["user"]["height"],
        image=user["user"]["avatar150"],
        steps_target=goals["goals"]["steps"],
        weight_target=wt_goal["goal"]["weight"],
        weight=user["user"]["weight"],
    )


    redirect(URL('index'))