我不确定我在做什么。我应该使用图书馆吗?或者手动完成?
所以我正在尝试使用Python中的WiThings(http://www.withings.com/api)API。
要执行某些请求,需要进行OAuth身份验证。我已经使用了请求库并获得了一个oauth令牌和秘密令牌,以及我的消费者和消费者秘密令牌。
现在我不得不提出请求,我遇到了一些问题。我需要做的请求的格式如下(来自其API的示例):
http://wbsapi.withings.net/notify?action=subscribe
&callbackurl=http%3a%2f%2fwww.yourdomain.net%2fyourCustomApplication.php
&comment=Your%20Own%20Application%20Description
&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
据我所知,除了最后的用户ID之外,这几乎是OAuth的典型格式。
那么,我可以使用请求库发出这样的请求吗?还是其他一些图书馆?如何使用comment和userid以及callbackurl字段获取正确的URL?或者我是否需要手动生成此URL?如果是这样的话,那么最好的办法是什么呢?
非常感谢任何帮助,因为我已经坚持了一段时间。
修改
因此,为了澄清一点,我理解了98%的代码。我最后只有一点问题。
所以我在这里,使用以下代码:
from __future__ import unicode_literals
from urlparse import parse_qs
import requests
from requests_oauthlib import OAuth1Session
consumer_key = '**Valid consumer key**'
consumer_secret = '**Valid consumer secret**'
oauth_key = '**Valid oauth key obtained through requests library and OAuth workflow**'
oauth_secret ='**Valid oauth secret obtained through requests library and OAuth workflow**'
verifier = '**Valid consumer key obtained through requests library and OAuth workflow**'
base_url = 'http://wbsapi.withings.net/notify'
params = {
'action': 'subscribe',
'callbackurl': '**callback URL**',
'comment': '**comment**',
'oauth_consumer_key': '**consumer_key**',
'oauth_nonce': 'etc etc',
'oauth_signature' : '' # <-------------- Where do I get this
# etc etc... I have everything else
}
r = requests.get("http://wbsapi.withings.net/notify", params=params)
这就是我所需要的。我有我需要的一切,但签名。有没有办法从oauth库中获取签名?这就是阻碍我的一切。
答案 0 :(得分:14)
使用URL查询字符串执行GET
次请求:
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)
清除后,现在您只需要遵循http://www.withings.com/en/api/oauthguide中记录的工作流程并实施它们
收到您的OAuth密钥和OAuth密钥后,请执行GET
请求,其中包含以下端点和查询字符串,这将返回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
然后,您需要授权您收到的令牌,并提供以下请求,该请求会为您提供 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
然后,您需要通过使用更多查询字符串命中此端点来请求access_token
:
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 &安培;用户ID = 831
现在您已经拥有了在您的问题和其他人中执行上述请求所需的一切,例如直接来自文档:
http://wbsapi.withings.com/measure? 行动= 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 &安培;用户ID = 831
同样,所有内容都可以在没有明确oauth
库的情况下完成,因为您可以使用requests.get
和查询字符串从dict
Feed中完成工作流程。该方法的params
参数。
我真的希望这可以帮助你实现目标。
答案 1 :(得分:0)
以下是使用rauth client library的工作示例。完全披露,我是原作者。希望这会有所帮助:
from rauth import OAuth1Service
withings = OAuth1Service(
name='withings',
consumer_key='fd5fe4002db502983fbd056fdf416941d83e15ecb68ee9eeb4978cb2370c',
consumer_secret='29dbc46056c530814c2debcf24c76ff42f6cc66d0e3e5cfdef1e166725c6f',
base_url='http://wbsapi.withings.net/notify',
request_token_url='https://oauth.withings.com/account/request_token',
authorize_url='http://oauth.withings.com/account/authorize',
access_token_url='https://oauth.withings.com/account/access_token')
request_token, request_token_secret = withings.get_request_token()
callback = 'https://github.com/litl/rauth'
authorize_url = withings.get_authorize_url(request_token,
oauth_callback=callback)
print('Visit this URL in your browser: {url}'.format(url=authorize_url))
userid = raw_input('Enter userid from browser URL: ')
sess = withings.get_auth_session(request_token,
request_token_secret,
params={'userid': userid})
print sess.get('measure', params={'action': 'getmeas',
'userid': userid}).json()