Search code examples
javajsonrestebay-api

Unable to List Item via EBay's Inventory API


I'm attempting to list an item on EBay using EBay's Inventory API via the following code (I'm using Apache HTTP Client):

public void put() throws ClientProtocolException, IOException
    {
        String url = "https://api.ebay.com/sell/inventory/v1/inventory_item/83368339";
        String charset = "utf-8";

        HttpClient client = HttpClientBuilder.create().build();
        HttpPut put = new HttpPut(url);

        // add request header
        put.addHeader("Authorization", "Bearer <TOKEN>");
        put.addHeader("Content-Language", "en-US");



        String json = "{ \"availability\": { \"pickupAtLocationAvailability\": [ { \"availabilityType\": \"IN_STOCK\", \"fulfillmentTime\": { \"unit\": \"TimeDurationUnitEnum\": \"BUSINESS_DAY\", \"value\": 1 }, \"merchantLocationKey\": \"NJ\", \"quantity\": 1 } ], \"shipToLocationAvailability\": { \"quantity\": 1 } }, \"condition\": \"ConditionEnum : [NEW]\", \"conditionDescription\": \"New condition\","
            + "\"product\": { \"aspects\": \"object\", \"brand\": \"Outlite\", \"description\": \"ADJUSTABLE FOCUS: Intense Spotlight for long range observation can up to 600 feet, Circle Floodlight for large area illumination\", \"imageUrls\": [ \"https://images-na.ssl-images-amazon.com/images/I/71c57aJiDAL._SL1500_.jpg\" ], \"title\": \"Outlite A100 Portable Ultra Bright Handheld LED Flashlight\", \"sku\": \"sku546372817\" }";


        HttpResponse response = client.execute(put);

        System.out.println("Response Code : "
                + response.getStatusLine().getStatusCode());

        BufferedReader rd = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent()));

        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }

        System.out.println(result);


    }

However I'm encountering the following error:

Response Code : 400
{"errors":[{"errorId":2004,"domain":"ACCESS","category":"REQUEST","message":"Invalid request","longMessage":"The request has errors. For help, see the documentation for this API.","parameters":[{"name":"reason","value":"Could not serialize field [availability.pickupAtLocationAvailability.availabilityType]"}]}]}

Solution

  • From the comments above, there were a few issues:

    1. Remove the surrounding parentheses
    2. Remove the surrounding quotes around the JSON
    3. Enum formatting

    After the last comment and confirming that removing the square brackets might have cleared up the availabilityType enum issue, here's what I think your final JSON should look like:

    String json = "{ \"availability\": { \"pickupAtLocationAvailability\": [ { \"availabilityType\": \"IN_STOCK\", \"fulfillmentTime\": { \"unit\": \"BUSINESS_DAY\", \"value\": 1 }, \"merchantLocationKey\": \"NJ\", \"quantity\": 1 } ], \"shipToLocationAvailability\": { \"quantity\": 1 } }, \"condition\": \"NEW\", \"conditionDescription\": \"New condition\","
                + "\"product\": { \"aspects\": \"object\", \"brand\": \"Outlite\", \"description\": \"ADJUSTABLE FOCUS: Intense Spotlight for long range observation can up to 600 feet, Circle Floodlight for large area illumination\", \"imageUrls\": [ \"https://images-na.ssl-images-amazon.com/images/I/71c57aJiDAL._SL1500_.jpg\" ], \"title\": \"Outlite A100 Portable Ultra Bright Handheld LED Flashlight\", \"sku\": \"sku546372817\" }}";
    

    Here it is split out into non-Java escapedness:

    {
        "availability": {
            "pickupAtLocationAvailability": [{
                "availabilityType": "IN_STOCK",
                "fulfillmentTime": {
                    "unit": "BUSINESS_DAY",
                    "value": 1
                },
                "merchantLocationKey": "NJ",
                "quantity": 1
            }],
            "shipToLocationAvailability": {
                "quantity": 1
            }
        },
        "condition": "NEW",
        "conditionDescription": "New condition",
        "product": {
            "aspects": "object",
            "brand": "Outlite",
            "description": "ADJUSTABLE FOCUS: Intense Spotlight for long range observation can up to 600 feet, Circle Floodlight for large area illumination",
            "imageUrls": ["https://images-na.ssl-images-amazon.com/images/I/71c57aJiDAL._SL1500_.jpg"],
            "title": "Outlite A100 Portable Ultra Bright Handheld LED Flashlight",
            "sku": "sku546372817"
        }
    }
    

    I modified the fulfillmentTime.unit and condition enums as well. It also looks like you might've been missing an a closing curly bracket at the end, so I added that as well.