问题
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.
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.
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.)
回答1:
Question: fetch the stock of specific users
Cardmarket RESTful API Documentation (Version 2.0)
- OAuth Header and Generating a Signature
- Articles
- User Articles
Python wrapper for the cardmarket.com API (version 2.0, using OAuth1)
- pymkm
Requests-OAuthlib: OAuth for Humans
- Docs » Requests-OAuthlib
Using 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)
来源:https://stackoverflow.com/questions/55659823/oauth-authentication-for-magiccardmarket-with-python-requests