使用 Python 请求对 magiccardmarket 进行 OAuth 身份验证 [英] OAuth authentication for magiccardmarket with Python requests

查看:28
本文介绍了使用 Python 请求对 magiccardmarket 进行 OAuth 身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式获取 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'}

这似乎包括所有相关数据(虽然可能太多了?诸如 Accept-Encoding、User-Agent 和 Connection 之类的东西会自动添加,但可能不是预期的,但我不确定.)

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

推荐答案

问题:获取特定用户的库存

Cardmarket RESTful API 文档(2.0 版)

  • OAuth 标头和生成签名
  • 文章
  • 用户文章
  • 使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆