client_credentials python access token keeps giving invalid scope
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2025 09:04 AM
Hi all,
This is driving me mad. Can anyone please see why this code won't work? If I comment out the scope it gives the access token, so I believe that proves the id concat is good. Of course the token is no good as there is no scope attached to it. Then, with the only scope needed in client credentials access, url encoded, it keeps saying invalid scope or malformed etc. The code is as follows (production & sandbox give the same result.)
import urllib.parse
import requests
import jason
import base64
client_id = 'St.......98c'
client_secret = 'PRD........565'
credentials = f"{client_id}:{client_secret}"
encoded_credentials = base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
authorisation = f'Basic {encoded_credentials}'
headers_list = {
"Content-Type":"application/x-www-form-urlencoded",
"Authorization":authorisation
}
scope = "https://api.ebay.com/oauth/api_scope"
url_scope = urllib.parse.quote(scope,safe="")
print(url_scope) #SEE output
url = f"https://api.ebay.com/identity/v1/oauth2/token"
payload_data = {
'grant_type' : 'client_credentials',
"scope":url_scope
}
response = requests.post(url, headers=headers_list, data=payload_data)
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 output is always:
https%3A%2F%2Fapi.ebay.com%2Foauth%2Fapi_scope
Error fetching access token: {"error":"invalid_scope","error_description":"The requested scope is invalid, unknown, malformed, or exceeds the scope granted to the client"}
Process finished with exit code 0
