2013-07-22 66 views
3

我不确定我在做什么。我应该为此使用一个库吗?或者手动做?在Python中使用URL查询字符串构造请求

因此,我试图在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 

据我所知,这是相当多使用OAuth的典型格式,除了在用户标识结束。

那么,我有可能使用请求库来提出这样的请求吗?或者其他一些图书馆?如何通过评论和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

你见过https://requests-oauthlib.readthedocs.org/en/latest/了吗? –

+0

是的,但我无法从文档中找出如何在需要将URL放在URL末尾时构建请求,在本例中为userid。 – elykl33t

+0

我看到[支持额外信息](https://requests-oauthlib.readthedocs.org/en/latest/oauth2_workflow.html#all-define-the-token-token-saver-and-needed-credentials)文档。 –

回答

6

要执行GET请求与URL查询字符串:

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的工作流程,并实现它们

  1. 在收到您的OAuth关键和OAuth Secret,使用以下端点执行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

  2. 然后,你需要授权您提供了以下要求,这将给你的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. 然后,你需要通过打一些更查询字符串此端点请求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 &的userid = 831

  4. 现在你已经一切都需要在你的问题进行上述要求,和其他人,例如直接从文档:

    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 &的userid = 831

再次,一切都没有明确的oauth库来完成,你可以从dict饲料内置到方法的参数paramsrequests.get查询字符串完成的工作流程。

我真的希望这可以帮助你实现你的目标。

+0

我曾想到过这个问题,我将不得不手动生成OAuth签名,这有点痛苦,并且我不确定我是否可以做到。 – elykl33t

+0

@ elykl33t然后使用'requests-oauthlib'来执行签名过程并获得上述参数,然后用我的答案中提到的__query string__构造一个GET请求。 – woozyking

+0

这是我想过的,但我不知道该怎么做。我怎样才能从请求中获得签名?我遇到这个问题的一个重要原因是因为我的Python经验不足。 – elykl33t

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() 
相关问题