02-11-2025 09:45 AM
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
02-11-2025 03:01 PM
02-12-2025 09:27 AM
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.
02-17-2025 04:24 AM
I've tried everything, but I still can't get this to work.
02-28-2025 12:50 PM
url = 'https://api.ebay.com/identity/v1/oauth2/token'
change
url = 'https://auth.ebay.com/identity/v1/oauth2/token'
03-01-2025 07:42 AM
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"}