使用Python中的URL查询字符串构造请求

ely*_*33t 7 python api oauth request

我不太确定我在做什么.我应该使用图书馆吗?或者手动完成?

所以我正在尝试使用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
Run Code Online (Sandbox Code Playgroud)

据我所知,除了最后的用户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)
Run Code Online (Sandbox Code Playgroud)

这就是我所需要的.我有我需要的一切,但签名.有没有办法从oauth库中获取签名?这就是阻碍我的一切.

woo*_*ing 18

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)
Run Code Online (Sandbox Code Playgroud)

如果已经清除,现在您只需要遵循http://www.withings.com/en/api/oauthguide上记录的工作流程 并实施它们

  1. 收到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

  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&用户ID = 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&用户ID = 831

同样,一切都可以在没有显式oauth库的情况下完成,因为您可以完成工作流,requests.get并将查询字符串dictfeed 构建到params方法的参数中.

我真的希望这能帮助你实现目标.