cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

client_credentials access token request won't work...help!

Hi all, I've been on this for ages, and it is verily doing my head in.

I've read the documentation many times, and tried to get working code,

but there is much inconsistency, so I have constructed my code, in Python,

from the ebay documentation. I've tried it all ways round, but it just won't work.

I keep getting 401_invalid client.

 

from encodings.utf_16 import encode, decode
import urllib.parse
import requests
import json
import base64
#import os
#import time
from requests.auth import HTTPBasicAuth

client_id = "St............98c"
#id = "St..............98c"

client_secret = "PR...........65"
#secret = "PR.............65"

id_string = base64.b64encode(b'client_id:client_secret')
#id_string = base64.b64encode(b'id:secret')

print(id_string) #prints,as expected, the bytes form of the encoding

#quit().....generally the hashouts are to change things round and experiment.

id_string=id_string.decode() #need to decode to allow the concat.

print(id_string) # now prints decoded string form
#quit()
authorisation = "Basic " + id_string
#authorisation = HTTPBasicAuth(id, secret)
#authorisation = HTTPBasicAuth(client_id, client_secret)

print(authorisation)

url = 'https://api.ebay.com/identity/v1/oauth2/token'
headers_list = {
"Content-Type":"application/x-www-form-urlencoded",
"Authorization":"authorisation"
}

scope = 'https://api.ebay.com/oauth/api_scope'

payload_data = {
"grant_type" : "client_credentials",
"scope":urllib.parse.quote(scope,safe='') #covers all public data, & only scope needed.
}
print(payload_data)
response = requests.post(url, headers=headers_list, data=payload_data)
print(response)
if response.status_code == 200:
access_token = response.json()["access_token"]
print("Access Token:", access_token)
else:
print("Error fetching access token:", response.text)

#the printouts give a 33 digit b64 encoded string, with authorisation being: Basic Y2xp...Q=
{'grant_type': 'client_credentials', 'scope': 'https%3A%2F%2Fapi.ebay.com%2Foauth%2Fapi_scope'} afaik the
% encoding is ok. But.....
<Response [401]>
Error fetching access token: {"error":"invalid_client","error_description":"client authentication failed"}

Just a thought. Documentation says that Json is the default format and does not need to be declared,
but the <Response [401]> looks like xml.
Could this be a clue?

The other totally wierd thing I found out with the alternative names of id & secret for the variables is
that the id_string becomes a completely different thing; a 12 digit string: aW........V0, and of course
concat with the Basic aW... etc.

Any help would be hugely appreciated. Ta



Message 1 of 6
latest reply
5 REPLIES 5

client_credentials access token request won't work...help!

Sorry if I misunderstand your problem. If you try to get access token with oauth2, this is my boilerplate code
headers
= {
      "Content-Type": "application/x-www-form-urlencoded",
      "Authorization": "Basic " + base64.b64encode(f"{EBAY_CLIENT_ID}:{EBAY_CLIENT_SECRET}".encode()).decode(),
            }
data = {
      "grant_type": "authorization_code",
      "code": code,
      "redirect_uri"EBAY_REDIRECT_URI,
   }
url = f"{EBAY_API_URL}/identity/v1/oauth2/token"
response = requests.post(url, headers=headers, data=data)
Message 2 of 6
latest reply

client_credentials access token request won't work...help!

Thank you for this.

I am analysing,  but copy and paste of the "Authoristion" line seems to show that I have failed to understand the steps in base64 encoding. It gives a long string that I have not seem before.

Message 3 of 6
latest reply

client_credentials access token request won't work...help!

I've tried everything, but I still can't get this to work.

 

Message 4 of 6
latest reply

client_credentials access token request won't work...help!

url = 'https://api.ebay.com/identity/v1/oauth2/token'

change

url = 'https://auth.ebay.com/identity/v1/oauth2/token'

 

Message 5 of 6
latest reply

client_credentials access token request won't work...help!

Thanks for the suggestion.

It is against the ebay documentation, but I did try it.

Unfortunately the response was:

Error fetching access token: {"timestamp":"2025-03-01T12:20:48.327+00:00","status":404,"error":"Not Found","path":"/identity/v1/oauth2/token"}

Message 6 of 6
latest reply