From 3bd8bcca54289fd0ac32d55f5525621830be020f Mon Sep 17 00:00:00 2001 From: MathildaSu Date: Wed, 19 Jul 2023 12:43:31 +0100 Subject: [PATCH] fix: discrepency between this repo and the curreent withings oauth2 api --- src/withings_api_example/www.py | 63 +++++++++++++++++---------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/src/withings_api_example/www.py b/src/withings_api_example/www.py index 383bb80..7838abc 100644 --- a/src/withings_api_example/www.py +++ b/src/withings_api_example/www.py @@ -4,12 +4,12 @@ app = Flask(__name__) -CLIENT_ID = config.get('withings_api_example', 'client_id') -CUSTOMER_SECRET = config.get('withings_api_example', 'customer_secret') -STATE = config.get('withings_api_example', 'state') -ACCOUNT_URL = config.get('withings_api_example', 'account_withings_url') -WBSAPI_URL = config.get('withings_api_example', 'wbsapi_withings_url') -CALLBACK_URI = config.get('withings_api_example', 'callback_uri') +CLIENT_ID = config.get("withings_api_example", "client_id") +CUSTOMER_SECRET = config.get("withings_api_example", "customer_secret") +STATE = config.get("withings_api_example", "state") +ACCOUNT_URL = config.get("withings_api_example", "account_withings_url") +WBSAPI_URL = config.get("withings_api_example", "wbsapi_withings_url") +CALLBACK_URI = config.get("withings_api_example", "callback_uri") @app.route("/") @@ -19,16 +19,16 @@ def get_code(): This endpoint redirects to a Withings' login page on which the user has to identify and accept to share his data """ - payload = {'response_type': 'code', # imposed string by the api - 'client_id': CLIENT_ID, - 'state': STATE, - 'scope': 'user.info', # see docs for enhanced scope - 'redirect_uri': CALLBACK_URI, # URL of this app - 'mode': 'demo' # Use demo mode, DELETE THIS FOR REAL APP - } + payload = { + "response_type": "code", # imposed string by the api + "client_id": CLIENT_ID, + "state": STATE, + "scope": "user.info", # see docs for enhanced scope + "redirect_uri": CALLBACK_URI, # URL of this app + "mode": "demo", # Use demo mode, DELETE THIS FOR REAL APP + } - r_auth = requests.get(f'{ACCOUNT_URL}/oauth2_user/authorize2', - params=payload) + r_auth = requests.get(f"{ACCOUNT_URL}/oauth2_user/authorize2", params=payload) return redirect(r_auth.url) @@ -41,28 +41,29 @@ def get_token(): an authentication code and the state code provided in the initial call """ - code = request.args.get('code') - state = request.args.get('state') + code = request.args.get("code") + state = request.args.get("state") - payload = {'grant_type': 'authorization_code', - 'client_id': CLIENT_ID, - 'client_secret': CUSTOMER_SECRET, - 'code': code, - 'redirect_uri': CALLBACK_URI - } + payload = { + "action": "requesttoken", + "grant_type": "authorization_code", + "client_id": CLIENT_ID, + "client_secret": CUSTOMER_SECRET, + "code": code, + "redirect_uri": CALLBACK_URI, + } - r_token = requests.post(f'{ACCOUNT_URL}/oauth2/token', - data=payload).json() + r_token = requests.post(f"{WBSAPI_URL}/v2/oauth2", data=payload).json() - access_token = r_token.get('access_token', '') + access_token = r_token["body"]["access_token"] # GET Some info with this token - headers = {'Authorization': 'Bearer ' + access_token} - payload = {'action': 'getdevice'} + headers = {"Authorization": "Bearer " + access_token} + payload = {"action": "getdevice"} # List devices of returned user - r_getdevice = requests.get(f'{WBSAPI_URL}/v2/user', - headers=headers, - params=payload).json() + r_getdevice = requests.get( + f"{WBSAPI_URL}/v2/user", headers=headers, params=payload + ).json() return r_getdevice