import requests
import base64
import urllib.parse
# Replace with your actual credentials
clientID = ''
clientSecret = ''
authorization_code = ''
redirect_uri = ''
# eBay sandbox OAuth token endpoint
# Request headers
headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Authorization': 'Basic ' + base64.b64encode((clientID + ':' + clientSecret).encode()).decode()
}
# Request body parameters
data = {
    'grant_type': 'authorization_code',
    'code': urllib.parse.quote(authorization_code),
    'redirect_uri': redirect_uri
}
# Send POST request to eBay OAuth token endpoint
try:
    response = requests.post(token_url, headers=headers, data=data)
    response.raise_for_status()  # Raise an exception for bad responses (4xx or 5xx)
    
    # Parse the JSON response
    response_data = response.json()
    
    # Extract the access token
    access_token = response_data.get('access_token')
    expires_in = response_data.get('expires_in')
    token_type = response_data.get('token_type')
    
    print(f"Access Token: {access_token}")
    print(f"Expires In: {expires_in} seconds")
    print(f"Token Type: {token_type}")
    
except requests.exceptions.HTTPError as http_err:
    print(f"HTTP error occurred: {http_err}")
    print(f"Response status code: {response.status_code}")
    print(f"Response text: {response.text}")
except Exception as err:
    print(f"Other error occurred: {err}")
 
-----------------------------------------------------
HTTP error occurred: 400 Client Error: Bad Request for url: 
https://api.ebay.com/identity/v1/oauth2/tokenResponse status code: 400
Response text: {"error":"invalid_grant","error_description":"the provided authorization grant code is invalid or was issued to another client"}
I tried to get access_token in authorization grant flow after get authoriztion_code.
But I can't get token.