def __init__(self): client = FitbitOauth2Client( config.fitbit_client_id, config.fitbit_client_secret, redirect_uri=config.fitbit_redirect_uri, timeout=10, ) self.client: FitbitOauth2Client = client
def __init__(self, client_id, client_secret, redirect_uri='http://localhost:8080/'): """ Initialize the FitbitOauth2Client """ self.redirect_uri = redirect_uri self.success_html = """ <h1>You are now authorized to access the Fitbit API!</h1> <br/><h3>You can close this window</h3>""" self.failure_html = """ <h1>ERROR: %s</h1><br/><h3>You can close this window</h3>%s""" self.oauth = FitbitOauth2Client(client_id, client_secret)
def forwards(apps, schema_editor): UserFitbit = apps.get_model('fitapp', 'UserFitbit') for fbuser in UserFitbit.objects.filter(refresh_token=''): try: token = FitbitOauth2Client( get_setting('FITAPP_CONSUMER_KEY'), get_setting('FITAPP_CONSUMER_SECRET'), refresh_token='{0}:{1}'.format( fbuser.access_token, fbuser.auth_secret)).refresh_token() fbuser.access_token = token['access_token'] fbuser.refresh_token = token['refresh_token'] fbuser.save() except MissingTokenError: # Delete fitbit user if existing access_token is invalid fbuser.delete()
def __init__(self, client_id, client_secret, redirect_uri='http://127.0.0.1:8080/'): """ Initialize the FitbitOauth2Client """ self.redirect_uri = redirect_uri self.success_html = """ <style> h1 {text-align:center;} h3 {text-align:center;} </style> <h1>You are now authorised to access the Fitbit API!</h1> <br/><h3>You can close this window</h3>""" self.failure_html = """ <style> h1 {text-align:center;} </style> <h1>ERROR: %s</h1><br/><h3>You can close this window</h3>%s""" self.oauth = FitbitOauth2Client(client_id, client_secret)
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'))
def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the Fitbit sensor.""" config_path = hass.config.path(FITBIT_CONFIG_FILE) if os.path.isfile(config_path): config_file = load_json(config_path) if config_file == DEFAULT_CONFIG: request_app_setup( hass, config, add_entities, config_path, discovery_info=None ) return False else: save_json(config_path, DEFAULT_CONFIG) request_app_setup(hass, config, add_entities, config_path, discovery_info=None) return False if "fitbit" in _CONFIGURING: hass.components.configurator.request_done(_CONFIGURING.pop("fitbit")) access_token = config_file.get(ATTR_ACCESS_TOKEN) refresh_token = config_file.get(ATTR_REFRESH_TOKEN) expires_at = config_file.get(ATTR_LAST_SAVED_AT) if None not in (access_token, refresh_token): authd_client = Fitbit( config_file.get(ATTR_CLIENT_ID), config_file.get(ATTR_CLIENT_SECRET), access_token=access_token, refresh_token=refresh_token, expires_at=expires_at, refresh_cb=lambda x: None, ) if int(time.time()) - expires_at > 3600: authd_client.client.refresh_token() unit_system = config.get(CONF_UNIT_SYSTEM) if unit_system == "default": authd_client.system = authd_client.user_profile_get()["user"]["locale"] if authd_client.system != "en_GB": if hass.config.units.is_metric: authd_client.system = "metric" else: authd_client.system = "en_US" else: authd_client.system = unit_system dev = [] registered_devs = authd_client.get_devices() clock_format = config.get(CONF_CLOCK_FORMAT) for resource in config.get(CONF_MONITORED_RESOURCES): # monitor battery for all linked FitBit devices if resource == "devices/battery": for dev_extra in registered_devs: dev.append( FitbitSensor( authd_client, config_path, resource, hass.config.units.is_metric, clock_format, dev_extra, ) ) else: dev.append( FitbitSensor( authd_client, config_path, resource, hass.config.units.is_metric, clock_format, ) ) add_entities(dev, True) else: oauth = FitbitOauth2Client( config_file.get(ATTR_CLIENT_ID), config_file.get(ATTR_CLIENT_SECRET) ) redirect_uri = f"{hass.config.api.base_url}{FITBIT_AUTH_CALLBACK_PATH}" fitbit_auth_start_url, _ = oauth.authorize_token_url( redirect_uri=redirect_uri, scope=[ "activity", "heartrate", "nutrition", "profile", "settings", "sleep", "weight", ], ) hass.http.register_redirect(FITBIT_AUTH_START, fitbit_auth_start_url) hass.http.register_view(FitbitAuthCallbackView(config, add_entities, oauth)) request_oauth_completion(hass)
] if "devices/battery" in monitored_resources: entities.extend([ FitbitSensor( authd_client, config_path, FITBIT_RESOURCE_BATTERY, hass.config.units.is_metric, clock_format, dev_extra, ) for dev_extra in registered_devs ]) add_entities(entities, True) else: oauth = FitbitOauth2Client(config_file.get(CONF_CLIENT_ID), config_file.get(CONF_CLIENT_SECRET)) redirect_uri = f"{get_url(hass, require_ssl=True)}{FITBIT_AUTH_CALLBACK_PATH}" fitbit_auth_start_url, _ = oauth.authorize_token_url( redirect_uri=redirect_uri, scope=[ "activity", "heartrate", "nutrition", "profile", "settings", "sleep", "weight", ], )
def oauth2client(): return FitbitOauth2Client(config.get('fitbit', 'CLIENT_ID'), config.get('fitbit', 'CLIENT_SECRET'))
@app.route('/') def hello_world(): return '<A HREF="{}">Register</A>'.format( url_for('redirecter', device_id=17)) @app.route('/redirect/<int:device_id>') def redirecter(device_id): state_str = "device_{:04}".format(int(device_id)) redirect_uri = get_redirect_url() url, _ = oauth.authorize_token_url(redirect_uri=redirect_uri, state=state_str) return redirect(url) @app.route('/token/') def token(): # show the post with the given id, the id is an integer args = dict(request.args) save_credentials(args['state'], args['code']) return "done" return '{}'.format() creds = get_credentials() oauth = FitbitOauth2Client(creds['client_id'], creds['client_secret']) if __name__ == "__main__": print get_credentials() app.run()