Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WiThings API OAuth with Python

I've really been struggling with getting OAuth to work with the WiThings API lately, using Python 3.3. For reference, here is the documentation for WiThings: http://www.withings.com/api

Now... As I've said, I have been working with the WiThings API in Python, using the requests library (http://docs.python-requests.org/en/latest/). Supposedly, this has built in support for OAuth 1.0.

Using this, when I put in my consumer key and consumer secret, then perform the token request, I get this response...

b'<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n<html><head>\n<title>413 Request Entity Too Large</title>\n</head><body>\n<h1>Request Entity Too Large</h1>\nThe requested resource<br />/index.php<br />\ndoes not allow request data with POST requests, or the amount of data provided in\nthe request exceeds the capacity limit.\n<hr>\n<address>Apache Server at oauth.withings.com Port 80</address>\n</body></html>\n'

Any idea what could be causing this? I have a feeling its WiThings specific... but their support is horrible.

Next, I did some more research, and found this: https://github.com/maximebf/python-withings

While also rather poorly documented, I installed it, and have this code:

from __future__ import unicode_literals
from urllib.parse import parse_qs
import requests
from requests_oauthlib import OAuth1
import withings

CONSUMER_KEY = "omitted"

CONSUMER_SECRET = "omitted"

auth = WithingsAuth(CONSUMER_KEY, CONSUMER_SECRET)
authorize_url = auth.get_authorize_url()
print("Go to %s allow the app and copy your oauth_verifier" %authorize_url)
oauth_verifier = raw_input('Please enter your oauth_verifier: ')
creds = auth.get_credentials(oauth_verifier)

client = WithingsApi(creds)
measures = client.get_measures(limit=1)
print("Your last measured weight: %skg" % measures[0].weight) 

And get the following error...

File "withings.py", line 5, in <module>
   import withjings
File C:\User_Directory\withings.py", line 11, in <module>
   auth = WithingsAuth(CONSUMER_KEY, CONSUMER_SECRET)
NameError: name 'WithingsAuth' is not defined

Any help on either of these issues? Has anyone had success working with Withings in python?

Thanks for the help guys

like image 875
elykl33t Avatar asked May 15 '26 07:05

elykl33t


1 Answers

You either need to import WithingsAuth from withings, or specify that you want to use the withings.WithingsAuth. Changing your code it becomes:

from __future__ import unicode_literals
try:
    from urllib.parse import parse_qs
except:
    import urlparse as parse_qs

try:
    input_method = raw_input
except:
    input_method = input

import requests
from requests_oauthlib import OAuth1
import withings

CONSUMER_KEY = "omitted"

CONSUMER_SECRET = "omitted"

auth = withings.WithingsAuth(CONSUMER_KEY, CONSUMER_SECRET)
authorize_url = auth.get_authorize_url()
print("Go to %s allow the app and copy your oauth_verifier" %authorize_url)
oauth_verifier = input_method('Please enter your oauth_verifier: ')
creds = auth.get_credentials(oauth_verifier)

client = withings.WithingsApi(creds)
measures = client.get_measures(limit=1)
print("Your last measured weight: %skg" % measures[0].weight) 
like image 111
Troels Avatar answered May 16 '26 20:05

Troels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!