Constructing requests with URL Query String in Python

To perform GET requests with URL query string:

import requests

params = {
    'action': 'subscribe',
    'callbackurl': '',
    'comment': '',
    'oauth_consumer_key': '',
    'oauth_nonce': '',
    # more key=value pairs as appeared in your query string
}
r = requests.get("http://wbsapi.withings.net/notify", params=params)

With that cleared, now you just need to follow the workflow documented on http://www.withings.com/en/api/oauthguide and implement them

  1. Upon receiving your OAuth Key and OAuth Secret, perform a GET request with the following endpoint and query string which will give you back token:

    https://oauth.withings.com/account/request_token?
    oauth_callback=http%3A%2F%2Fexample.com%2Fget_access_token
    &oauth_consumer_key=c331c571585e7c518c78656f41582e96fc1c2b926cf77648223dd76424b52b
    &oauth_nonce=f71972b1fa93b8935ccaf34ee02d7657
    &oauth_signature=J8xzgFtHTsSRw8Ejc8UDV2jls34%3D
    &oauth_signature_method=HMAC-SHA1
    &oauth_timestamp=1311778988
    &oauth_version=1.0

  2. Then you need to authorize the token you received with the following request which will give you the user_id:

    https://oauth.withings.com/account/authorize?
    oauth_callback=http%3A%2F%2Fexample.com%2Fget_access_token
    &oauth_consumer_key=c331c571585e7c518c78656f41582e96fc1c2b926cf77648223dd76424b52b
    &oauth_nonce=369f9ceb2f285ac637c9a7e9e98019bd
    &oauth_signature=OR9J9iEl%2F2yGOXP2wk5c2%2BWtYvU%3D
    &oauth_signature_method=HMAC-SHA1
    &oauth_timestamp=1311778988
    &oauth_token=5bb105d2292ff43ec9c0f633fee9033045ed4643e9871b80ce586dc1bf945
    &oauth_version=1.0

  3. Then you need to request the access_token by hitting this endpoint with some more query string:

    https://oauth.withings.com/account/access_token?
    oauth_consumer_key=c331c571585e7c518c78656f41582e96fc1c2b926cf77648223dd76424b52b
    &oauth_nonce=7acd22371fc56fd8a0aaf8416f79f84f
    &oauth_signature=jmj1g%2FB3rYR2DCpWp86jB5YVHIM%3D
    &oauth_signature_method=HMAC-SHA1
    &oauth_timestamp=1311778988
    &oauth_token=5bb105d2292ff43ec9c0f633fee9033045ed4643e9871b80ce586dc1bf945
    &oauth_version=1.0
    &userid=831

  4. Now you have everything needed to perform the aforementioned request in your question, and others, example directly from the documentation:

    http://wbsapi.withings.com/measure?
    action=getmeas
    &oauth_consumer_key=c331c571585e7c518c78656f41582e96fc1c2b926cf77648223dd76424b52b
    &oauth_nonce=accbac1b7ee2b86b828e6dc4a5a539b2
    &oauth_signature=XfobZMboIg2cRyNKAvyzONHHnKM%3D
    &oauth_signature_method=HMAC-SHA1
    &oauth_timestamp=1311842514
    &oauth_token=887557411788d5120537c6550fbf2df68921f8dd6f8c7e7f9b441941eb10
    &oauth_version=1.0
    &userid=831

Again, everything can be done without explicit oauth library as you can finish the workflow with requests.get and query string built from a dict feed into the params argument of the method.

I truly hope this helps you achieve your goal.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)