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

AccessToken error in authorization code grant flow

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/token
Response 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.

Message 1 of 2
latest reply
1 REPLY 1

Re: AccessToken error in authorization code grant flow

Hi @tixd70,

 

The authorization code returned from the authorization code grant is valid for 299 seconds. Use the authorization code before it expires to obtain a User access token.

Tip: The authorization code returned by eBay is URL-encoded. This value must be URL-encoded when you pass the value in the code parameter of the authorization code grant request. However, if the method you use to make the request URL-encodes the values you pass, then you must URL-decode the authorization code before using it the authorization code grant request.

 

Best Regards,
eBay Developer Support
Message 2 of 2
latest reply