sscheetz / etsy-python2

Python access to the Etsy API
http://developer.etsy.com
GNU General Public License v3.0
32 stars 18 forks source link

403 oauth_problem=token_rejected #4

Closed TheKnottyCelt closed 4 years ago

TheKnottyCelt commented 4 years ago

I was happy to find this porject, as I have been struggling to no end trying to make API calls to my Etsy stroe and listings.

I first tried using my pre-existing credentials which I managed to acquire using requests.

from etsy2 import Etsy
from etsy2.oauth import EtsyOAuthClient

etsy_oauth = EtsyOAuthClient(
    client_key=<keystring>,
    client_secret=<shared secret>,
    resource_owner_key=<oauth_token>,
    resource_owner_secret=<oauth_token_secret>)

etsy = Etsy(etsy_oauth_client=etsy_oauth)

etsy.findAllListingActive()

yields the following exception:

ValueError: Could not decode response from Etsy as JSON: status_code: 403, text: 'oauth_problem=token_rejected', url 'https://openapi.etsy.com/v2/listings/active'

This in turn caused an json.decoder.JSONDecodeError exception. Since the main cause was a rejected token, I decided to try to use `EtsyOAuthHelper to generate some new credentials.

from etsy2.oauth import EtsyOAuthHelper

permission_scopes=['listings_r', 'listings_w']
login_url, temp_oauth_token_secret = EtsyOAuthHelper.get_request_url_and_token_secret(<keystring>, <shared secret>, permission_scopes)

#after following the login url to acquire <verifier>
oauth_token, oauth_token_secret = EtsyOAuthHelper.get_oauth_token(<keystring>, <shared secret>, temp_oauth_token_secret, verifier)

etsy_oauth = EtsyOAuthClient(
    client_key=<keystring>,
    client_secret=<shared secret>,
    resource_owner_key=oauth_token,
    resource_owner_secret=oauth_token_secret)

etsy = Etsy(etsy_oauth_client=etsy_oauth)

etsy.findAllListingActive()

This yields the same exception noted above.

sscheetz commented 4 years ago

You are probably right in that your first token was expired, revoked, etc. Your attempt to get the new credentials is incorrect though.

the call to get_request_url_and_token_secret is fine (although without a callback_url it was previously impossible to finish authentication).

the call to get_oauth_token has the wrong parameters though.

the old method signature was

get_oauth_token_via_auth_url(api_key, shared_secret, oauth_token_secret, auth_url, etsy_env=EtsyEnvProduction())

you can see the 4th parameter was the auth_url, not the verifier. The auth_url would look something like "https://seancallback.com/?oauth_token=bfa146c8d8ac1aaaa5bd07598a01c&oauth_verifier=c399b7e9#_=_" and would have been the url etsy redirected you to if you had passed a callback_url.


With that said, you are the second person to ask to be able to get oauth credentials by manually passing the verification code. I added a method to do this in 0.6.0 so you should update your package and follow the instructions in "Obtaining Etsy OAuthCredentials".

TheKnottyCelt commented 4 years ago

Thank you for your rapid response. 0.6.0 did the trick.