import requests
import base64
# eBay API credentials
APP_ID = 'xxxxxx'
DEV_ID = 'xxxxxx'
CERT_ID = 'xxxxxxxxxxx'
EBAY_API_URL = 'https://api.ebay.com/commerce/taxonomy/v1'
# Example product title (will be dynamic in full script)
product_title = "Ryobi 18V ONE+ Blower 4.0Ah Kit"
refined_query = "Ryobi 18V ONE+ Blower 4.0Ah Kit" # More specific query
# Step 1: Get OAuth application token
def get_oauth_token():
auth_string = f"{APP_ID}:{CERT_ID}"
auth_encoded = base64.b64encode(auth_string.encode('utf-8')).decode('utf-8')
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': f'Basic {auth_encoded}'
}
data = {
'grant_type': 'client_credentials',
'scope': 'https://api.ebay.com/oauth/api_scope'
}
response = requests.post('https://api.ebay.com/identity/v1/oauth2/token', headers=headers, data=data)
response.raise_for_status()
return response.json()['access_token']
try:
# Get OAuth token
oauth_token = get_oauth_token()
print("Successfully obtained OAuth token")
# Step 2: Query Taxonomy API for category suggestions
headers = {
'Authorization': f'Bearer {oauth_token}',
'Content-Type': 'application/json',
'Accept': 'application/json'
}
params = {
'q': refined_query # Use refined query
}
response = requests.get(f'{EBAY_API_URL}/category_tree/15/get_category_suggestions', headers=headers, params=params)
response.raise_for_status()
category_data = response.json()
# Step 3: Process category suggestions
if 'categorySuggestions' in category_data and category_data['categorySuggestions']:
print("Category Suggestions:")
for suggestion in category_data['categorySuggestions']:
category_id = suggestion['category']['categoryId']
category_name = suggestion['category']['categoryName']
print(f"ID: {category_id} | Name: {category_name}")
# Pick the first tools-related category (e.g., containing "Battery" or "Charger")
for suggestion in category_data['categorySuggestions']:
category_name = suggestion['category']['categoryName'].lower()
if any(keyword in category_name for keyword in ["battery", "charger", "tool", "accessory"]):
category_id = suggestion['category']['categoryId']
print(f"Selected eBay Category ID: {category_id}")
print(f"Selected Category Name: {suggestion['category']['categoryName']}")
break
else:
raise Exception("No tools-related category ID found in eBay Taxonomy API response")
else:
raise Exception("Could not find category ID in eBay Taxonomy API response")
except requests.RequestException as e:
print(f"API Error: {e}")
if e.response:
print(f"API Response: {e.response.text}")
except Exception as e:
print(f"Error: {e}")