带有Python请求的magiccardmarket的OAuth身份验证 [英] OAuth authentication for magiccardmarket with Python requests
问题描述
我想以编程方式在 http://www.cardmarket.com/,但似乎无法在以下Python代码段中使用OAuth身份验证.
I want to programmatically fetch the stock of specific users on http://www.cardmarket.com/, but can't seem to get OAuth authentication to work in the following Python snippet.
仅使用requests_oauthlib库中的可用方法并没有得到任何积极的结果,而且我还尝试自己构造OAuth标头并将其传递到请求调用中,但均无济于事.我有点不知所措,因为我已经尝试了好几个小时都没有结果,以至于我失去了本来应该是一个简单的爱好项目的大部分乐趣.尽管如此,我相信这是一个简单的问题,有望很快得到解决.
Simply using available methods from the requests_oauthlib library have not given any positive results and I've also tried constructing the OAuth header myself and passing that along in the requests-call, all to no avail. I'm a bit at the end of my wits, because I've tried for hours without results, to the point where I've lost most enjoyment for what should have been a simple hobby project. Nonetheless I'm confident that it's a simple problem that can hopefully be resolved quickly.
以下是应该但不起作用的简单代码:
Here's the simple code that should, but doesn't, work:
import requests
from requests_oauthlib import OAuth1
user = ..
app_token = ..
app_secret = ..
access_token = ..
access_token_secret = ..
request_url = "https://api.cardmarket.com/ws/v2.0/users/" + user + "/articles?start=0&maxResults=100"
auth = OAuth1(app_token, app_secret, resource_owner_key=access_token, resource_owner_secret=access_token_secret)
response = requests.get(request_url, auth=auth)
print(response.request.headers)
print(response)
print(response.content)
我还尝试了各种变体,并且如上所述,还尝试自己构造标头,但没有结果.
I've also tried a mixture of variations to it and as stated, have also tried to construct the header myself, but with no results.
按原样提供的代码没有发现任何问题,但是在执行给定查询时仍然会因未授权而出错.
I don't see anything wrong with the provided code as-is, but still get an error for being unauthorized when performing the given query.
response.request.headers打印语句返回以下内容:
The response.request.headers print statement returns the following:
{'Authorization': b'OAuth oauth_nonce="..", oauth_timestamp="..", oauth_version="1.0", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="..", oauth_token="..", oauth_signature=".."', 'Accept-Encoding': b'gzip, deflate', 'User-Agent': b'python-requests/2.18.4', 'Accept': b'*/*', 'Connection': b'keep-alive'}
哪个似乎包含了所有相关数据(尽管可能太多了?诸如接受编码",用户代理"和连接"之类的东西会自动添加,但可能不会出现,但我不确定.)
Which seems the include all relevant data (though maybe too much? Things like Accept-Encoding, User-Agent and Connection are automatically added, but may not be expected, but I'm not sure.)
推荐答案
问题:获取特定用户的库存
Question: fetch the stock of specific users
Cardmarket RESTful API文档(2.0版)
- OAuth标头和生成签名
- 文章
- 用户文章
- OAuth Header and Generating a Signature
- Articles
- User Articles
Cardmarket RESTful API Documentation (Version 2.0)
使用OAuth1Session
:
from requests_oauthlib import OAuth1Session
# base_url = 'https://api.cardmarket.com/ws/v2.0/output.json'
base_url = 'https://api.cardmarket.com/ws/v2.0'
# product_id = 266361 # Mandatory
# url = '{}/articles/{}'.format(base_url, product_id)
user_id = 266361 # Mandatory Type: integer (ID) or string (name)
url = '{}/users/:{}/articles'.format(base_url, user_id)
oauth = OAuth1Session('app_token',
client_secret='app_secret',
resource_owner_key='access_token',
resource_owner_secret='access_token_secret',
realm=url
)
params = {'start':0, 'maxResults':100}
r = oauth.get(url, params=params)
这篇关于带有Python请求的magiccardmarket的OAuth身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!