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.
<span>from </span>encodings.utf_16 <span>import </span>encode, decode<br/><span>import </span>urllib.parse<br/><span>import </span>requests<br/><span>import </span>json<br/><span>import </span>base64<br/><span>#import os<br/></span><span>#import time<br/></span><span>from </span>requests.auth <span>import </span>HTTPBasicAuth<br/><br/>client_id = <span>"St............98c"<br/></span><span>#id = "St..............98c"<br/></span><span><br/></span>client_secret = <span>"PR...........65"<br/></span><span>#secret = "PR.............65"<br/></span><span><br/></span>id_string = base64.b64encode(<span>b'client_id:client_secret'</span>)<br/><span>#id_string = base64.b64encode(b'id:secret')<br/></span><span><br/></span>print(id_string) #prints,as expected, the bytes form of the encoding<br/><br/><span>#quit().....generally the hashouts are to change things round and experiment.<br/></span><span><br/></span>id_string=id_string.decode() <span>#need to decode to allow the concat.<br/></span><span><br/></span>print(id_string) # now prints decoded string form<br/><span>#quit()<br/></span>authorisation = <span>"Basic " </span>+ id_string<br/><span>#authorisation = HTTPBasicAuth(id, secret)<br/></span><span>#authorisation = HTTPBasicAuth(client_id, client_secret)<br/></span><span><br/></span>print(authorisation)<br/><br/>url = <span>'https://api.ebay.com/identity/v1/oauth2/token'<br/></span>headers_list = {<br/> <span>"Content-Type"</span>:<span>"application/x-www-form-urlencoded"</span>,<br/> <span>"Authorization"</span>:<span>"authorisation"<br/></span>}<br/><br/>scope = <span>'https://api.ebay.com/oauth/api_scope'<br/></span><span><br/></span>payload_data = {<br/> <span>"grant_type" </span>: <span>"client_credentials"</span>,<br/> <span>"scope"</span>:urllib.parse.quote(scope,safe=<span>''</span>) <span>#covers all public data, & only scope needed.<br/></span>}<br/>print(payload_data)<br/>response = requests.post(url, headers=headers_list, data=payload_data)<br/>print(response)<br/><span>if </span>response.status_code == <span>200</span>:<br/> access_token = response.json()[<span>"access_token"</span>]<br/> print(<span>"Access Token:"</span>, access_token)<br/><span>else</span>:<br/> print(<span>"Error fetching access token:"</span>, response.text)<br/><br/>#the printouts give a 33 digit b64 encoded string, with authorisation being: Basic Y2xp...Q=<br/>{'grant_type': 'client_credentials', 'scope': 'https%3A%2F%2Fapi.ebay.com%2Foauth%2Fapi_scope'} afaik the<br/>% encoding is ok. But.....<br/><Response [401]><br/>Error fetching access token: {"error":"invalid_client","error_description":"client authentication failed"}<br/><br/>Just a thought. Documentation says that Json is the default format and does not need to be declared, <br/>but the <Response [401]> looks like xml.<br/>Could this be a clue?<br/><br/>The other totally wierd thing I found out with the alternative names of id & secret for the variables is <br/>that the id_string becomes a completely different thing; a 12 digit string: aW........V0, and of course<br/>concat with the Basic aW... etc.<br/><br/>Any help would be hugely appreciated. Ta<br/><br/><br/><br/>5 comments
sinanal
·1 year agochange
steviestuff_57
OP1 year agoThanks 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"}
sinanal
·1 year agoheaders = {
steviestuff_57
OP1 year agoThank 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.
steviestuff_57
OP1 year agoI've tried everything, but I still can't get this to work.