我不确定我在做什么。我应该为此使用一个库吗?或者手动做?在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库中获取签名?这一直是我一直坚持的。
你见过https://requests-oauthlib.readthedocs.org/en/latest/了吗? –
是的,但我无法从文档中找出如何在需要将URL放在URL末尾时构建请求,在本例中为userid。 – elykl33t
我看到[支持额外信息](https://requests-oauthlib.readthedocs.org/en/latest/oauth2_workflow.html#all-define-the-token-token-saver-and-needed-credentials)文档。 –