eBay API SDK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-16-2023 02:49 AM - edited ‎11-16-2023 02:50 AM
Hello all,
I am trying to find a way to automate the API calls for the fulfillment API. I am able to recall the order details using the API from Postman, but would like to automate the token generation process so that i can do order calls every 30 mins or 60 mins.
https://developer.ebay.com/api-docs/static/oauth-consent-request.html
https://developer.ebay.com/api-docs/static/oauth-auth-code-grant-request.html
https://developer.ebay.com/api-docs/static/oauth-refresh-token-request.html
I am using the following code based on the three articles shared above and also the eBay API python SDK. The commented details are user specified and we have the correct values.
The first part of the oauth consent request is working perfectly. But the second part of the user token generation is returning the Error 404.
import json
from urllib.parse import unquote
import requests
import base64
import sys
production_login_end_point = "https://auth.ebay.com/oauth2/authorize"
# client_id = <"client_id">
# client_secret = <"client_secret">
# redirect_uri = <"runame">
# dev_id = <"devid">
scope = "https://api.ebay.com/oauth/api_scope https://api.ebay.com/oauth/api_scope/sell.fulfillment"
URL = f"{production_login_end_point}?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code&scope={scope}"
print(URL)
code = input("Enter the code received")
print(unquote(code))
code2 = unquote(code)
exchange_end_point = "https://api.ebay.com/identitv/v1/oauth2/token"
ci_cs = f"{client_id}:{client_secret}"
encoded_ci_cs = ci_cs.encode()
b64_e_ci_cs = base64.b64encode(encoded_ci_cs).decode()
print(b64_e_ci_cs)
headers = {
"Content-Type" : "application/x-www-form-urlencoded",
"Authorization" : "Basic " + b64_e_ci_cs
}
print(headers)
data = {
"grant_type" : "authorization code",
"code" : code,
"redirect_uri" : redirect_uri
}
print(data)
response = requests.post(exchange_end_point , headers=headers, data=data)
print(response.json)
Can anyone help / guide to a better way to automate the generation of the User Tokens.
With Warm Regards,
A M.
Re: eBay API SDK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-16-2023 08:58 AM
I don't understand the reason for 'encode' in this sequence:
encoded_ci_cs = ci_cs.encode()
b64_e_ci_cs = base64.b64encode(encoded_ci_cs).decode()
Admittedly, I don't know the language, but if your program is reading the ci and cs values from a file, it would seem that the only requirement would be:
b64_e_ci_cs = base64.b64encode(ci_cs)
Re: eBay API SDK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2023 01:13 AM
Hello,
The first encode is for converting the string to bytes in UTF-8 which allows it to be used as input for the encoding with B64. Similarly after the encoding by B64, it must be decoded back from bytes to string, which is why the decode function is used.
Without the encoding, decoding the output for the B64 encoding would be wrong.
User Access token is being generated but manually, the wish is to automate the entire process with python.
We tried to use the client credential grant type flow but unfortunately the production OAuth scopes only have the basic public data that can be accessed using client credential flow.
Which is why we are using the authorization flow grant type.
Is there any way to either:
1. Expand the OAuth scopes in production
or
2. Get a user access token that does not expire in 2 hours but lasts longer like atleast a day if not months ?
With Warm Regards,
A M.
Re: eBay API SDK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2023 07:58 AM
"grant_type" : "authorization code",
I have only seen "authorization_code" with an underscore. Don't know if this makes a difference.
Re: eBay API SDK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2023 12:06 AM - edited ‎11-30-2023 12:07 AM
Hello,
Yes that was rectified.
This is the latest iteration of the code. This works completely fine now.
I need to find a way to parse the 5-minute authorization code token automatically and not manually copying it from the browser.
I have highlighted the input point of the URL with "Need Help" comment section.
I tried using selenium to automatically capture the resulting URL but as chrome was under control by automated test software, ebay threw a captcha.
import json
import base64
import requests
import webbrowser
from selenium import webdriver
from urllib.parse import urlparse, parse_qs
# Replace these values with your actual eBay API credentials
client_id = 'xxxxxxxxx'
client_secret = 'xxxxxxxxx'
redirect_uri = 'xxxxxxx'
# Define the scopes needed for your application
scopes = [
'https://api.ebay.com/oauth/api_scope',
'https://api.ebay.com/oauth/api_scope/sell.marketing',
'https://api.ebay.com/oauth/api_scope/sell.inventory',
'https://api.ebay.com/oauth/api_scope/sell.account',
'https://api.ebay.com/oauth/api_scope/sell.fulfillment',
'https://api.ebay.com/oauth/api_scope/sell.finances'
]
# eBay authorization URL
authorization_url = f'https://auth.ebay.com/oauth2/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code&scope={"%20".join(scopes)}'
print (authorization_url)
# Open the default web browser to eBay authorization URL
webbrowser.open(authorization_url)
## ======================= NEED HELP TO AUTOMATICALLY PARSE THIS ====================== ##
# Wait for the user to enter the authorization code
authorization_url_response = input("Enter the full redirect URL after granting permission: ")
## ======================= NEED HELP TO AUTOMATICALLY PARSE THIS ====================== ##
# Parse the authorization code from the URL
parsed_url = urlparse(authorization_url_response)
authorization_code = parse_qs(parsed_url.query)['code'][0]
# Print the authorization code
print(f'Authorization Code: {authorization_code}')
# Assign the authorization code to variable
code = authorization_code
# Set the eBay token endpoint
token_endpoint = 'https://api.ebay.com/identity/v1/oauth2/token'
# Define the payload for the token request
payload = {
'grant_type': 'authorization_code',
'code' : code,
'redirect_uri': redirect_uri
}
print (payload)
# Define the headers for the token request
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': f'Basic {base64.b64encode(f"{client_id}:{client_secret}".encode()).decode()}'
}
print(headers)
# Make the token request
response = requests.post(token_endpoint, data=payload, headers=headers)
# Assuming 'response' is the variable containing your JSON response
response_json = response.json()
# Print the pretty-formatted JSON response
print(json.dumps(response_json, indent=2))
access_token = response.json().get('access_token')
print(f"\nAccess Token\n\n{access_token}")
refresh_token = response.json().get('refresh_token')
print(f"\nRefresh Token\n\n{refresh_token}\n")
# Assign the access token to variable
access_token = access_token
# Use a sample endpoint to test the call
api_endpoint = 'https://api.ebay.com/sell/fulfillment/v1/order?filter=creationdate:%5B2023-10-01T00:00:01.026Z..%5D&limit=200&offset=0'
# Define the headers for the API request
headers2 = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {access_token}',
}
# Sample request to get inventory items
url = f'{api_endpoint}'
response = requests.get(url, headers=headers2)
# Assuming 'response' is the variable containing your JSON response
response_json = response.json()
# Print the pretty-formatted JSON response
print(json.dumps(response_json, indent=2))
In another post you mentioned that the refresh token can be used repeatedly for 18 months to generate a user access token. Is that correct ?
I was under the impression that the refresh token can be used only once to generate an access token and then one needed to restart the whole authorization grant flow.
Again many thanks for the help.
With Warm Regards,
A M.
Re: eBay API SDK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2023 12:20 AM
I just tried it and yes the refresh token works repeatedly for minting fresh user access tokens.
Here is the code incase anyone is interested.
import json
import base64
import requests
# Replace these values with your actual eBay API credentials
client_id = 'xxxxxx'
client_secret = 'xxxxxx'
redirect_uri = 'xxxxxxx'
refresh_token = 'xxxxxxxx'
# Set the eBay token endpoint
token_endpoint = 'https://api.ebay.com/identity/v1/oauth2/token'
# Define the scopes needed for your application
scopes = [
'https://api.ebay.com/oauth/api_scope',
'https://api.ebay.com/oauth/api_scope/sell.marketing',
'https://api.ebay.com/oauth/api_scope/sell.inventory',
'https://api.ebay.com/oauth/api_scope/sell.account',
'https://api.ebay.com/oauth/api_scope/sell.fulfillment',
'https://api.ebay.com/oauth/api_scope/sell.finances'
]
# Define the payload for the token request
payload = {
'grant_type': 'refresh_token',
'refresh_token' : refresh_token,
'scope': ' '.join(scopes)
}
print (payload)
# Define the headers for the token request
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': f'Basic {base64.b64encode(f"{client_id}:{client_secret}".encode()).decode()}'
}
print(headers)
# Make the token request
response = requests.post(token_endpoint, data=payload, headers=headers)
# Assuming 'response' is the variable containing your JSON response
response_json = response.json()
# Print the pretty-formatted JSON response
print(json.dumps(response_json, indent=2))
access_token = response.json().get('access_token')
print(f"\nAccess Token\n\n{access_token}")
With Warm Regards,
A M.
Re: eBay API SDK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2023 01:51 AM
# eBay authorization URL
authorization_url = f'https://auth.ebay.com/oauth2/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code&scope={"%20".join(scopes)}'
print (authorization_url)
# Open the default web browser to eBay authorization URL
webbrowser.open(authorization_url)
## ======================= NEED HELP TO AUTOMATICALLY PARSE THIS ====================== ##
# Wait for the user to enter the authorization code
authorization_url_response = input("Enter the full redirect URL after granting permission: ")
## ======================= NEED HELP TO AUTOMATICALLY PARSE THIS ====================== ##
eBay's authorization URL can be programmatically invoked and will launch a webpage. When that webpage opens, the user must grant authorization by clicking the "agree" button. Upon user acceptance, your "auth accepted URL" that you entered on your token page will be launched.
https://developer.ebay.com/my/auth?env=production&index=0
Because a page on your server is triggered to display your acceptance message, your server would read the query string that eBay appends to your acceptance URL.
There are two values to read:
code=v%5E1.1%23i%5E1...VeMjYw
expires_in=299
or, if the user declines, then:
error=access_denied
