首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Python的WiThings API OAuth

使用Python的WiThings API OAuth
EN

Stack Overflow用户
提问于 2013-07-13 00:20:37
回答 2查看 1.6K关注 0票数 2

最近,我一直在努力让OAuth使用Python3.3来使用Python。作为参考,以下是WiThings的文档:http://www.withings.com/api

现在..。正如我所说的,我一直在使用WiThings中的Python,使用的是请求库(http://docs.python-requests.org/en/latest/)。据推测,这已经内置了对OAuth 1.0的支持。

使用它,当我输入我的消费者密钥和消费者机密,然后执行令牌请求时,我会得到这个响应……

代码语言:javascript
复制
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'

知道是什么原因造成的吗?我有一种特定于WiThings的感觉...但他们的支持是可怕的。

接下来,我做了更多的研究,发现了这个:https://github.com/maximebf/python-withings

虽然文档也很差,但我安装了它,并编写了以下代码:

代码语言:javascript
复制
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) 

并得到以下错误...

代码语言:javascript
复制
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

在这两个问题上有什么帮助吗?有没有人在python中成功地使用过Withing?

感谢你们的帮助

EN

回答 2

Stack Overflow用户

发布于 2014-05-15 16:05:42

它应该是

代码语言:javascript
复制
from withings import WithingsAuth, WithingsApi 

对我来说,它工作得很好,我能够提取我最后测量的体重。

票数 0
EN

Stack Overflow用户

发布于 2016-05-29 22:41:28

您需要从withings导入WithingsAuth,或者指定要使用withings.WithingsAuth。改变你的代码,它会变成:

代码语言:javascript
复制
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) 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17619474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档