Search code examples
c#asp.netebay-api

C# eBay Taxonomy API Get_Category_Suggestions


I'm unable to get the new eBay Taxonomy API to work properly with the call get_category_suggestions with C# to replace the old GetSuggestedCategories call that eBay is deprecating. I'm using the same exact methods I used for making calls to other parts of the newer eBay API and it worked to get the default category tree id within the Taxonomy API. The following code worked to get me the tree node ID for EBAY-US which is 124.

        HttpWebRequest request = WebRequest.Create("https://api.ebay.com/commerce/taxonomy/v1/get_default_category_tree_id?marketplace_id=EBAY-US") as HttpWebRequest;

        request.Method = "GET";
        request.Headers.Add(HttpRequestHeader.Authorization, System.Web.HttpUtility.HtmlEncode("Bearer " + accessToken));
        request.Headers.Add("X-EBAY-C-MARKETPLACE-ID", "EBAY-US");


        log.Debug("starting request.GetRequestStream get_category_suggestions");
        string result = null;
        var response = (HttpWebResponse)request.GetResponse();
        using (var streamReader = new StreamReader(response.GetResponseStream()))
        {
            result = streamReader.ReadToEnd();
        }

        log.Debug("about to load results to json reader");
        var reader = new JsonTextReader(new StringReader(result));
        log.Debug("successfully loaded results");   

Then I use that 124 default category tree id to try to use the get_category_suggestions with the same exact code and doing exactly as eBay has as an example (https://developer.ebay.com/api-docs/commerce/taxonomy/resources/category_tree/methods/getCategorySuggestions#h2-samples) but I always receive a 404 Not Found message from eBay. What am I missing here? The problem code is below:

HttpWebRequest request = WebRequest.Create("https://api.ebay.com/commerce/taxonomy/v1/get_default_category_tree_id?marketplace_id=EBAY-US") as HttpWebRequest;
        request.Method = "GET";
        request.Headers.Add(HttpRequestHeader.Authorization, System.Web.HttpUtility.HtmlEncode("Bearer " + accessToken)); 
        
request.Headers.Add("X-EBAY-C-MARKETPLACE-ID", "EBAY-US");request.Headers.Add("Accept-Encoding", "application/gzip");

        log.Debug("starting request.GetRequestStream get_category_suggestions");
        string result = null;
        var response = (HttpWebResponse)request.GetResponse();
        using (var streamReader = new StreamReader(response.GetResponseStream()))
        {
            result = streamReader.ReadToEnd();
        }

        log.Debug("about to load results to json reader");
        var reader = new JsonTextReader(new StringReader(result));
        log.Debug("successfully loaded results");   

Solution

  • So I saw that in the documentation for the call getCategorySubtree for the category_tree_id they used the number 0.  To be clear the number 0 is not what the documentation for the getCategorySuggestions states.   As of April 2021, the documentation states that you first need to make a call to getDefaultCategoryTree and use that return as your category_tree_id.  But knowing how eBay operates (poorly), I gave the getCategorySuggestions call a try with the number 0 as the category_tree_id and it worked.  Here's my final working code for anyone that finds this later.

    HttpWebRequest request = WebRequest.Create("https://api.ebay.com/commerce/taxonomy/v1/category_tree/0/get_category_suggestions?q=" + title) as HttpWebRequest;
    request.Method = "GET";
    request.Headers.Add(HttpRequestHeader.Authorization, System.Web.HttpUtility.HtmlEncode("Bearer " + accessToken));
    request.Headers.Add("X-EBAY-C-MARKETPLACE-ID", "EBAY-US");
    request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
    log.Debug("starting request.GetRequestStream get_category_suggestions");
    string result = null;
    var response = (HttpWebResponse)request.GetResponse();
    using (var streamReader = new StreamReader(response.GetResponseStream()))
    {
        result = streamReader.ReadToEnd();
    }
    log.Debug("about to load results to json reader");
    var reader = new JsonTextReader(new StringReader(result));
    log.Debug("successfully loaded results");