Get Oauth Token (Python)

258 views
Skip to first unread message

V E

unread,
Jan 13, 2022, 1:59:50 PM1/13/22
to Etsy API
Hello, i'm trying to obtain oauth token using official etsy documentation.
Step1. OK
Step2. OK
Step 3 (Get Ouath Token) - FAIL.
For post data/json i'm using this data:
body = {
'grant_type': 'authorization_code',
'client_id': clientID,
'redirect_uri': redirectUri,
'code': authCode,
'code_verifier': clientVerifier,
}

Headers: 
headers = {
'Content-Type': 'application/json',
'x-api-key': clientID  // Had to add it because no information about in manual, but response error message say that x-api-key is required.

As result i'm always getting:
400
{"error":"invalid_request","error_description":"grant_type is required"}

when grant_type is first value in the dict.
'grant_type': 'authorization_code',

Any help please?

Gareth Doherty

unread,
Jan 24, 2022, 9:38:04 AM1/24/22
to Etsy API
Content type needs to be  application/x-www-form-urlencoded

try change that and see how you get on.

Ilya Solozobov

unread,
Jan 25, 2022, 1:40:15 PM1/25/22
to Etsy API
That is ready python script for getting oath credentials

````
#!/bin/python3

import requests
import string
from requests_oauthlib import OAuth1
import urllib.parse

def parceRsp(req) -> {}:
    req_ulr = urllib.parse.unquote(str(req))

    req_ulr = req_ulr[2:len(req_ulr) - 1]

    if ("?" in req_ulr):
        req_ulr = req_ulr.split("?")[1]

    keys = req_ulr.split("&")

    keys_dict = {}

    for i in keys:
        var = i.split("=")[0]
        val = i.split("=")[1]
        keys_dict[var] = val

    return keys_dict

consumer_key = ""
consumer_secret = ""

# oauth client registration
request_token_url = 'https://openapi.etsy.com/v2/oauth/request_token?scope=transactions_r%20billing_r&treasury_r'
oauth = OAuth1(consumer_key, client_secret=consumer_secret)
r = requests.post(url=request_token_url, auth=oauth)

rsp = parceRsp(r.content)

oauth_token = rsp.get("oauth_token")
oauth_token_secret = rsp.get("oauth_token_secret")

url = urllib.parse.unquote(str(r.content))
url = url[2:len(url) - 1]
url = url.replace("login_url=", "")

print ("Go here and copy verification code:\n",url)
verifier = input('Enter a verification code:')
print ("code", verifier)

oauth = OAuth1(consumer_key,
                client_secret=consumer_secret,
                resource_owner_key=oauth_token,
                resource_owner_secret=oauth_token_secret,
                verifier=verifier)

r = requests.post(url="https://openapi.etsy.com/v2/oauth/access_token", auth=oauth)

rsp = parceRsp(r.content)

oauth_token = rsp.get("oauth_token")
oauth_token_secret = rsp.get("oauth_token_secret")

print ("oauth_token: ", oauth_token)
print ("oauth_token_secret: ", oauth_token_secret) 
```

For making requests you have to use consumer_key, consumer_secret, oauth_token, oauth_token_secret


понедельник, 24 января 2022 г. в 17:38:04 UTC+3, gareth....@gmail.com:
Reply all
Reply to author
Forward
0 new messages