Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 32 additions & 31 deletions src/withings_api_example/www.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("/")
Expand All @@ -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)

Expand All @@ -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