无法通过EBay的库存API列出项目

时间:2017-11-20 15:23:28

标签: java json rest ebay ebay-api

我正在尝试使用EBay's Inventory API通过以下代码在EBay上列出项目(我正在使用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);


    }

但是我遇到以下错误:

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]"}]}]}

任何人都知道我为什么会收到此错误?

由于

1 个答案:

答案 0 :(得分:1)

从上面的评论中,有一些问题:

  1. 删除周围的括号
  2. 删除JSON周围的引号
  3. 枚举格式
  4. 在最后一条评论并确认删除方括号可能已清除availabilityType枚举问题后,我认为您的最终JSON应如下所示:

    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\" }}";
    

    这里将它拆分为非Java转义:

    {
        "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"
        }
    }
    

    我也修改了fulfillmentTime.unit和条件枚举。看起来你也可能在最后错过了一个结束的大括号,所以我也补充说。