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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17788445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:12:43  来源:igfitidea点击:

Constructing requests with URL Query String in Python

pythonapioauthrequest

提问by elykl33t

I'm not really sure what I'm doing. Should I be using a library for this? Or do it manually?

我不确定我在做什么。我应该为此使用图书馆吗?还是手动完成?

So I'm trying to do some work with the WiThings (http://www.withings.com/api) API in Python.

所以我正在尝试使用Python 中的 WiThings ( http://www.withings.com/api) API做一些工作。

In order to perform some of the requests, OAuth authentication is required. I have gone through using the requests library and obtained an oauth token and secret token, alongside my consumer and consumer secret tokens.

为了执行某些请求,需要 OAuth 身份验证。我已经使用了请求库并获得了一个 oauth 令牌和秘密令牌,以及我的消费者和消费者秘密令牌。

Now I am at the point of having to make requests, and I am running into some problems. The format for the request I need to make is as follows (an example from their API):

现在我必须提出请求,而且我遇到了一些问题。我需要提出的请求格式如下(来自他们的 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

As far as I can tell, this is pretty much a typical format with OAuth, except for the userid at the end.

据我所知,这几乎是 OAuth 的典型格式,除了最后的用户 ID。

So, is it possible for me to make a request like this using the requests library? Or some other library? How do I get the URL right, with the comment and userid and callbackurl fields? Or do I need to generate this URL manually? If that's the case, whats the best way for going about doing this?

那么,我是否可以使用 requests 库发出这样的请求?还是其他图书馆?如何使用注释、用户 ID 和 callbackurl 字段获取正确的 URL?还是我需要手动生成这个 URL?如果是这种情况,执行此操作的最佳方法是什么?

Any assistance is greatly appreciated, as I've been stuck on this for a while.

非常感谢任何帮助,因为我已经坚持了一段时间。

EDIT

编辑

So, for some clarification, I understand about 98% of the code I am being referred to. I am only having a little problem at the end.

所以,为了澄清一下,我理解我被引用的大约 98% 的代码。最后我只有一个小问题。

So here I am, with the following code:

所以我在这里,使用以下代码:

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)

This is all I need. I have everything I need but the signature. Is there a way I can get the signature from the oauth libraries? This is all that has been holding me up.

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

回答by woozyking

To perform GETrequests with URL query string:

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)

With that cleared, now you just need to follow the workflow documented on http://www.withings.com/en/api/oauthguideand implement them

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

  1. Upon receiving your OAuth Key and OAuth Secret, perform a GETrequest with the following endpoint and query stringwhich will give you back 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. Then you need to authorizethe token you received with the following request which will give you the 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. Then you need to request the access_tokenby hitting this endpoint with some more query string:

    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. Now you have everything needed to perform the aforementioned request in your question, and others, example directly from the documentation:

    http://wbsapi.withings.com/measure? action=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

  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

Again, everything can be done without explicit oauthlibrary as you can finish the workflow with requests.getand query stringbuilt from a dictfeed into the paramsargument of the method.

再次,一切都没有明确的做oauth库,你可以完成与工作流程requests.get查询字符串从内置dict送入params该方法的参数。

I truly hope this helps you achieve your goal.

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

回答by maxcountryman

Here's a working example using the rauth client library. Full disclosure, I'm the original rauth author. Hope this helps:

这是一个使用rauth 客户端库的工作示例。完全披露,我是原作者。希望这可以帮助:

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()