Updated version of etsy-python with python3 compatibility and support for the modern etsy api. Please file any bugs, suggestions, or usage questions as github issues and I will get to them as soon as possible.
See changelog at bottom of the readme for differences between etsy-python and etsy-python2. The last release for etsy-python was 0.3.1.
Python access to the Etsy API
Originally by Dan McKinley - dan@etsy.com - http://mcfunley.com
Forked by Sean Scheetz
Other Contributors
The simplest way to install the module is pip
$ pip install etsy2
To install from source, extract the tarball and use the following commands.
$ python setup.py build $ sudo python setup.py install
This library works by retrieving the api metadata from https://api.etsy.com/v2/?api_key=Etsy
object at runtime. This happens during
construction of the Etsy
object (the api client). Refer to the Etsy API docs to see what method names you can call from the Etsy
object. Every parameter can be passed in as a named parameter to the method call. e.g.
etsy.findAllShopReceipts(shop_id=<shop_id>)
There are two types of etsy endpoints: Those that require OAuth and those that don't. For endpoints that don't require OAuth you can use the regular etsy client provided by this library as shown below.
from etsy2 import Etsy
etsy = Etsy(api_key=<your-api-key>)
etsy.findAllFeaturedListings()
For endpoints that do require OAuth you must pass an EtsyOAuthClient
to the Etsy
constructor.
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 EtsyOAuthClient
requires a client_key, client_secret, resource_owner_key, and resource_owner_secret to be constructed. The client_key and the client_secret are the keystring and shared secret given to you by etsy upon registering your app. The resource_owner_key and resource_owner_secret are the oauth_token and oauth_token_secret that must be retrieved by working through etsy's oauth workflow. See the "Obtaining Etsy OAuthCredentials" section to learn how to get the oauth_token and oauth_token_secret used by the EtsyOAuthClient.
The EtsyOAuthHelper
exists to simplify the retrieval of the oauth_token and oauth_token_secret. The first step of the process will always be generating the login_url to which you will redirect the resource owner (user of your application). Usage is shown below.
# define permissions scopes as defined in the 'OAuth Authentication' section of the docs
# https://www.etsy.com/developers/documentation/getting_started/oauth#section_permission_scopes
permission_scopes = ['transactions_r', 'listings_r']
# In this case, user will be redirected to page on etsy that shows the verification code.
login_url, temp_oauth_token_secret = \
EtsyOAuthHelper.get_request_url_and_token_secret(api_key, shared_secret, permission_scopes)
# In this case, user will be redirected to the callback_url after telling etsy to allow access to their data.
login_url, temp_oauth_token_secret = \
EtsyOAuthHelper.get_request_url_and_token_secret(api_key, shared_secret, permission_scopes, callback_url)
# Note,
# login_url is the url to redirect the user to have them authenticate with etsy.
# temp_oauth_token_secret is the secret used in the get_ouath_token methods to retrieve permanent oauth credentials.
After your user has told Etsy they want to give you access to their data, the next step is to trade your temp_oauth_token and temp_oauth_token_secret for the permanent oauth_token and oauth_token_secret. There are two different paths for achieving this and which one you take depends on if you specified a callback_url in your request to EtsyOAuthHelper.get_request_url_and_token_secret
.
If you did not specify a callback url, the user will be redirected to a page owned by etsy that displays the verification code. You need to pass this verification code along with the temp_oauth_token and temp_oauth_token_secret to the EtsyOAuthHelper.get_oauth_token_via_verifier
method to retrieve the final tokens expected by the EtsyOAuthClient
. This is done as shown below.
oauth_token, oauth_token_secret = \
EtsyOAuthHelper.get_oauth_token_via_verifier(api_key, shared_secret, temp_oauth_token, temp_oauth_token_secret, verifier)
# Note,
# temp_oauth_token is part of the login_url generated by get_request_url_and_token_secret
# temp_oauth_token_secret is returned by get_request_url_and_token_secret
# verifier is the verification code on the screen etsy redirects the user to
The oauth_token and oauth_token_secret obtained from this step are the tokens expected by the EtsyOAuthClient
.
It is important to note that temp_oauth_token is part of the login_url generated by get_request_url_and_token_secret. If you are going to use EtsyOAuthHelper.get_oauth_token_via_verifier
you need to parse the temp_oauth_token from the login url before it can be passed as a parameter. This can be done as shown below.
import urllib.parse as urlparse
from urllib.parse import parse_qs
login_url, temp_oauth_token_secret = \
EtsyOAuthHelper.get_request_url_and_token_secret(api_key, shared_secret, permission_scopes, auth_callback_url)
query = urlparse.urlparse(login_url).query
temp_oauth_token = parse_qs(query)['oauth_token'][0]
If you specified a callback_url, etsy will redirect the user to that url after the user grants access to your application. Etsy will append the temp_oauth_token and the verifier as query strings to callback_url. You should pass the full url the the user was redirected to to EtsyOAuthHelper.get_oauth_token_via_auth_url
as shown below.
oauth_token, oauth_token_secret = \
EtsyOAuthHelper.get_oauth_token_via_auth_url(api_key, shared_secret, temp_oauth_token_secret, auth_url)
# Note,
# temp_oauth_token_secret is returned from get_request_url_and_token_secret
# auth_url is the url the user was redirected to by etsy
EtsyOAuthHelper.get_oauth_token_via_auth_url
will obtain the temp_oauth_token and the verifier from the auth_url. The oauth_token and oauth_token_secret obtained from this step are the tokens expected by the EtsyOAuthClient
.
For convenience (and to avoid storing API keys in revision control systems), the package supports local configuration. You can manage your API keys in a file called $HOME/etsy/keys (or the equivalent on Windows) with the following format:
v2 = 'Etsy API version 2 key goes here'
Alternatively, you can specify a different key file when creating an API object.
from etsy import Etsy api = Etsy(key_file='/usr/share/etsy/keys')
(Implementation note: the keys file can be any valid python script that defines a module-level variable for the API version you are trying to use.)
This package comes with a reasonably complete unit test suite. In order to run the tests, use:
$ python setup.py test
Some tests (those that actually call the Etsy API) require your API key to be locally configured. See the Configuration section, above.
As mentioned above, this module is implemented by metaprogramming against the method table published by the Etsy API. In other words, API methods are not explicitly declared by the code in this module. Instead, the list of allowable methods is downloaded and the patched into the API objects at runtime.
This has advantages and disadvantages. It allows the module to automatically receive new features, but on the other hand, this process is not as fast as explicitly declared methods.
In order to speed things up, the method table json is cached locally by default. If a $HOME/etsy directory exists, the cache file is created there. Otherwise, it is placed in the machine's temp directory. By default, this cache lasts 24 hours.
The cache file can be specified when creating an API object:
from etsy import Etsy
api = Etsy(method_cache='myfile.json')
Method table caching can also be disabled by passing None as the cache parameter:
from etsy import Etsy
# do not cache methods
api = Etsy(method_cache=None)
Initial release