{"id":8737,"date":"2025-04-08T10:20:31","date_gmt":"2025-04-08T10:20:31","guid":{"rendered":"https:\/\/pariswells.com\/blog\/?p=8737"},"modified":"2025-04-08T10:20:32","modified_gmt":"2025-04-08T10:20:32","slug":"python-to-use-taxonomy-to-get-categories-for-listing","status":"publish","type":"post","link":"https:\/\/pariswells.com\/blog\/research\/python-to-use-taxonomy-to-get-categories-for-listing","title":{"rendered":"Python to Use Taxonomy to get Categories for Listing"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">import requests\nimport base64\n\n# eBay API credentials\nAPP_ID = 'xxxxxx'\nDEV_ID = 'xxxxxx'\nCERT_ID = 'xxxxxxxxxxx'\nEBAY_API_URL = 'https:\/\/api.ebay.com\/commerce\/taxonomy\/v1'\n\n# Example product title (will be dynamic in full script)\nproduct_title = \"Ryobi 18V ONE+ Blower 4.0Ah Kit\"\nrefined_query = \"Ryobi 18V ONE+ Blower 4.0Ah Kit\"  # More specific query\n\n# Step 1: Get OAuth application token\ndef get_oauth_token():\n    auth_string = f\"{APP_ID}:{CERT_ID}\"\n    auth_encoded = base64.b64encode(auth_string.encode('utf-8')).decode('utf-8')\n    headers = {\n        'Content-Type': 'application\/x-www-form-urlencoded',\n        'Authorization': f'Basic {auth_encoded}'\n    }\n    data = {\n        'grant_type': 'client_credentials',\n        'scope': 'https:\/\/api.ebay.com\/oauth\/api_scope'\n    }\n    response = requests.post('https:\/\/api.ebay.com\/identity\/v1\/oauth2\/token', headers=headers, data=data)\n    response.raise_for_status()\n    return response.json()['access_token']\n\ntry:\n    # Get OAuth token\n    oauth_token = get_oauth_token()\n    print(\"Successfully obtained OAuth token\")\n\n    # Step 2: Query Taxonomy API for category suggestions\n    headers = {\n        'Authorization': f'Bearer {oauth_token}',\n        'Content-Type': 'application\/json',\n        'Accept': 'application\/json'\n    }\n    params = {\n        'q': refined_query  # Use refined query\n    }\n    response = requests.get(f'{EBAY_API_URL}\/category_tree\/15\/get_category_suggestions', headers=headers, params=params)\n    response.raise_for_status()\n    category_data = response.json()\n\n    # Step 3: Process category suggestions\n    if 'categorySuggestions' in category_data and category_data['categorySuggestions']:\n        print(\"Category Suggestions:\")\n        for suggestion in category_data['categorySuggestions']:\n            category_id = suggestion['category']['categoryId']\n            category_name = suggestion['category']['categoryName']\n            print(f\"ID: {category_id} | Name: {category_name}\")\n        \n        # Pick the first tools-related category (e.g., containing \"Battery\" or \"Charger\")\n        for suggestion in category_data['categorySuggestions']:\n            category_name = suggestion['category']['categoryName'].lower()\n            if any(keyword in category_name for keyword in [\"battery\", \"charger\", \"tool\", \"accessory\"]):\n                category_id = suggestion['category']['categoryId']\n                print(f\"Selected eBay Category ID: {category_id}\")\n                print(f\"Selected Category Name: {suggestion['category']['categoryName']}\")\n                break\n        else:\n            raise Exception(\"No tools-related category ID found in eBay Taxonomy API response\")\n    else:\n        raise Exception(\"Could not find category ID in eBay Taxonomy API response\")\n\nexcept requests.RequestException as e:\n    print(f\"API Error: {e}\")\n    if e.response:\n        print(f\"API Response: {e.response.text}\")\nexcept Exception as e:\n    print(f\"Error: {e}\")<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8737","post","type-post","status-publish","format-standard","hentry","category-research"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/8737","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/comments?post=8737"}],"version-history":[{"count":1,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/8737\/revisions"}],"predecessor-version":[{"id":8738,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/8737\/revisions\/8738"}],"wp:attachment":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/media?parent=8737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/categories?post=8737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/tags?post=8737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}