3

I am trying to convert a block of cURL to python requests. I get the following error when I do:

{'error': 'invalid_request', 'error_description': 'request is missing a required parameter or malformed.'}

What am I translating incorrectly?

curl

POST /identity/v1/oauth2/token HTTP/1.1
Host: api.sandbox.ebay.com
Authorization: Basic <B64-encoded-oauth-credentials>
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code=<URL-decoded-auth-code>&redirect_uri=<your_redirect_uri>

my_call.py

headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
a    'Authorization': f'Basic {<Base64 encoded value>}'
}
data = {
    'grant_type': 'authorization_code',
    'code': client_id  # str,
    'redirect_uri': url  # str,
    'scope': 'https://api.ebay.com/oauth/api_scope/sell.inventory.readonly',
}

def get_oath_token():
    url = 'https://api.ebay.com/identity/v1/oauth2/token'
    r = requests.post(url, headers=headers, data=data)
    print(r.json())
juju
  • 884
  • 1
  • 9
  • 31
  • This may be too simple a fix, but is OAuth enabled? See this answer: https://stackoverflow.com/a/44689884/8787680 – Sébastien Lavoie Mar 16 '19 at 04:16
  • How is the URL encoded? Does it look like (1) `http://foo.bar/` or like (2) `http%3A%2F%2Ffoo.bar%2F`? – Sébastien Lavoie Mar 16 '19 at 04:21
  • @SébastienLavoie I do have Oauth enabled – juju Mar 16 '19 at 18:10
  • The url is like (1) – juju Mar 16 '19 at 18:10
  • Can you try sending your POST request with the second version? You can do this by importing `from requests.utils import quote` and after defining `url` in the function `get_oath_token`, reassign it the following value: `url = quote(url, safe='')`. – Sébastien Lavoie Mar 16 '19 at 18:27
  • @SébastienLavoie I did that and received the following error: `requests.exceptions.MissingSchema: Invalid URL 'https%3A%2F%2Fapi.ebay.com%2Fidentity%2Fv1%2Foauth2%2Ftoken': No schema supplied.` I then tried to cut `https\\` from the url and do `"https\\" + quote(url, safe=True)` – juju Mar 16 '19 at 20:34
  • I believe this has to be done with the URL specified as the value for `data['scope']` instead, not the `url` I pointed out earlier. You can find an example request from an official tutorial here near the bottom of the page: https://developer.ebay.com/api-docs/static/oauth-client-credentials-grant.html – Sébastien Lavoie Mar 16 '19 at 20:53

0 Answers0