问题
Premise
I am working at linking my product configuration database with my POS and various eCommerce sites. The last link in the puzzle is connecting to Etsy. Their guide (https://www.etsy.com/developers/documentation/reference/listing) is specific to PHP, but I am working with python 3.7.
Status
I have been able to successfully acquire the credentials required thanks to this question: How to add a new item using Python Etsy HTTP API methods? I now have the following credentials:
oauth_token
oauth_token_secret
oauth_consumer_key
verifier
key_string
shared_secret
What I've Tried
I tried using both python_etsy
and etsy_python
, but both seem to have been abandoned and are incomplete, producing exceptions at every turn. I tried to my best ability to try to fix each issue as it arises, but am just so lost.
As suggested in the answer to the the question linked above, I tried using an OAuth1Session
object.
>>> etsy = OAuth1Session(
... key_string,
... client_secret=shared_secret,
... resource_owner_key=oauth_token,
... resource_owner_secret=oauth_token_secret)
>>> esty.get('https://openapi.etsy.com/v2/users/__SELF__')
>>> response.status_code
403
>>> response.text
'oauth_problem=token_rejected'
I just don't know how to properly use the given credentials in order to make successful API calls to the Etsy API.
Even adding signature_type=SIGNATURE_TYPE_QUERY
when defining the OAuth1Session
object etsy
produces the same result.
I am posting this as a new question, since the question linked above is specific to adding a new item using the API, while I am looking for general instruction on interpreting and converting the PHP examples for use in Python. This is what I have done with other APIs with good success, but this particular one has thrown be for a loop.
Where does the verifier
come in to play?
--Update--
In place of an oauth
session, I used requests.get
as follows:
>>> import requests
>>> import uuid
>>> from datetime import datetime
>>> url = 'https://openapi.etsy.com/v2/oauth/access_token'
>>> params = {
... 'oauth_consumer_key': {oauth_consumer_key},
... 'oauth_signature': {verifier},
... 'oauth_signature_method': {method},
... 'oauth_nonce': str(uuid(uuid1()),
... 'oauth_timestamp': datetime.timestamp(datetime.utcnow())
... 'oauth_token': {oauth_token}
...}
>>> token_response = requests.get(url,params=params)
>>> print(rrr.status_code,rrr.text)
400 oauth_problem=verifier_invalid
I tried the above using each of the following value for oauth_signature_method
:
- 'PLAINTEXT'
- 'RSA-SHA1'
- 'HMAC-SHA1'
Each value produced the same result. I also tried placing the verifier
in the oauth_nonce
paramater
回答1:
Use etsy-python2 module to acquire and process the credentials.
pip install etsy2
from etsy2 import Etsy
from etsy2.oauth import EtsyOAuthHelper, EtsyOAuthClient
import urllib.parse as urlparse
from urllib.parse import parse_qs
api_key = <etsy keystring>
shared_secret = <etsy shared secret>
#define permission scopes
permission_scopes = ['listings_r', 'listings_w']
login_url, temp_oauth_token_secret = \
EtsyOAuthHelper.get_request_url_and_token_secret(api_key, shared_secret, permission_scopes)
query = urlparse.urlparse(login_url).query
temp_oauth_token = parse_qs(query)['oauth_token'][0]
print(login_url)
#follow the url to acquire the verifier.
oauth_token, oauth_token_secret = \
EtsyOAuthHelper.get_oauth_token_via_verifier(api_key, shared_secret, temp_oauth_token, temp_oauth_token_secret, input('Verifier: '))
etsy_oauth = EtsyOAuthClient(client_key = api_key,
client_secret = shared_secret,
resource_owner_key = oauth_token,
resource_owner_secret = oauth_token_secret)
etsy = Etsy(etsy_oauth_client = etsy_oauth)
The etsy2
module is current and being maintained, so right now, it is a good option. The methods are dynamicly defined at runtime from Etsy's API server. After defining your Etsy
object, you can list the available methods using:
dir(etsy)
来源:https://stackoverflow.com/questions/61416515/how-to-use-etsy-api-credentials-to-get-and-post-data-in-python