question

n1shop avatar image
0 Likes"
n1shop asked ·

eBay Oauth token - unable to exchange authorization code for a User access token

I am getting {"error":"invalid_client","error_description":"client authentication failed"} 401 response. I did manage to get a user consent (docs: https://developer.ebay.com/api-docs/static/oauth-authorization-code-grant.html) by accessing the url manually and logging in to my account using Python 3:

import requests, urllib, base64 
my_AppID = "someAppID"
my_Ru_Name = "someRuName"
scope = "https://api.ebay.com/oauth/api_scope/sell.fulfillment"
scope = urllib.parse.quote_plus(scope) 

url = f"""https://auth.ebay.com/oauth2/authorize?
client_id={my_AppID}&
redirect_uri={my_Ru_Name}&
response_type=code& scope={scope}&"""

I printed the url string and accessed it in the browser, logged in and "consented". This page said "Authorization successfully completed." so I took the code value from the new redirected page url. After this I was unable to exchange the authorization code for a User access token:

my_CertID = "someCertID"
client_id = base64.b64encode(my_AppID.encode())
client_secret = base64.b64encode(my_CertID.encode())
auth_string = "Basic " + client_id.decode() + ":" + client_secret.decode() 

consent_code = "v%521.1%25i..................jYw" # from the page's link after logging in
consent_code = urllib.parse.quote_plus(code) 

headers = {"Content-Type": "application/x-www-form-urlencoded", "Authorization": auth_string}
data = {"grant_type": "authorization_code", "code": consent_code , "redirect_uri": Ru_Name}
url_token = "https://api.ebay.com/identity/v1/oauth2/token"
resp = requests.post(url_token, headers=headers, data=data)
print(resp.text) 

# the response I get:
{"error":"invalid_client","error_description":"client authentication failed"}

What am I doing wrong? Is it the request part? The encoding?

I am kinda new to all this so thanks in advance!

apierroroauth2pythontokensoauthtoken erroruser tokenauthentication failedinvalid client
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

· Write an Answer
gblohm_de avatar image
0 Likes"
gblohm_de answered ·

You need to assemble the string first (client id + ":" + client secret), then encode that to get the auth param.


· 2 · Share
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hey, thanks for the answer.

Now I am getting :

{"error":"unsupported_grant_type","error_description":"grant type in request is not supported by the authorization server"}

client_id_and_secret = my_AppID + ":" + my_CertID
client_id_and_secret = base64.b64encode(client_id_and_secret.encode())
auth_string = "Basic " + client_id_and_secret.decode()


What am I doing wrong here?





0 Likes 0 ·

Looks good to me. I only ever used the sandbox as my application isn't done yet, but this is from my working log output (auth code truncated somewhat):

https://api.sandbox.ebay.com/identity/v1/oauth2/token


grant_type=authorization_code&code=v^1.1#i^1#I^3#p^3#f^0#r^1#t^Ul41XzY6MUU4N...4NA==&redirect_uri=Guntram_Blohm-GuntramB-Franks-lhomjyb


Did you refresh your auth code before calling that api? They last only 5 minutes, and while the error hints at a problem with the grant type, the ebay api is a mess, so who knows what they're returning when the code is outdated.


0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.