def withings_connect(client, oauth_verifier, oauth_token, oauth_secret): try: healthdevice = client.health_device except: healthdevice = HealthDevice(client=client) auth = WithingsAuth(WITHINGS_SETTINGS['CONSUMER_KEY'], WITHINGS_SETTINGS['CONSUMER_SECRET'], withings_get_callback_uri()) auth.oauth_token = oauth_token auth.oauth_secret = oauth_secret print(oauth_verifier, oauth_token, oauth_secret) creds = auth.get_credentials(oauth_verifier) try: healthdevice = client.health_device except: healthdevice = HealthDevice(client=client) healthdevice.provider = HealthDevice.ProviderType.WITHINGS.value healthdevice.access_token = creds.access_token healthdevice.refresh_token = '' healthdevice.expires_at = datetime.now() + timedelta(days=365) healthdevice.meta = { 'access_token_secret': creds.access_token_secret, 'user_id': creds.user_id } healthdevice.save() return True
def get_authorizer(token=None): back_url = '%s://%s/withings/comeback' % ( request.get_header('url_scheme', 'http'), request.get_header('HTTP_HOST', request.get_header('SERVER_NAME', 'wykresik.genoomy.com')), ) sys.stderr.write('back_url: %s\n' % (back_url,)) auth = WithingsAuth(settings.WITHINGS['key'], settings.WITHINGS['secret'], back_url) if token: with db_connection() as db_conn: with db_conn.cursor() as c: c.execute('SELECT * FROM withings_credentials WHERE token=%s ORDER BY created_at DESC', (token,)) db_result = c.fetchone() if db_result is None: raise InvalidToken secret = db_result['secret'] auth.oauth_token = token auth.oauth_secret = secret return auth