【问题标题】:Unable to List Item via EBay's Inventory API无法通过 eBay 的 Inventory API 列出项目
【发布时间】:2017-11-20 15:23:28
【问题描述】:

我正在尝试通过以下代码使用EBay's Inventory API 在 EBay 上列出一个项目(我正在使用 Apache HTTP 客户端):

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

【问题讨论】:

  • 去掉有效载荷中的括号。它不是有效的 JSON,错误消息指出。
  • 另外,我不确定您是否也需要整个有效负载周围的引号。
  • 谢谢。我删除了引号/括号,但现在出现新错误: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]"}]}]}
  • 您是否也删除了开头和结尾的\"
  • 是的,几乎肯定是方括号。不得不挖掘一下样本,但他们的枚举只使用所有大写字符串值:developer.ebay.com/api-docs/sell/inventory/resources/…

标签: java json rest ebay-api


【解决方案1】:

从上面的 cmets 来看,有几个问题:

  1. 删除周围的括号
  2. 删除 JSON 周围的引号
  3. 枚举格式

在最后一条评论并确认删除方括号可能已经解决了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 和条件枚举。看起来您可能在末尾缺少一个右大括号,所以我也添加了它。

【讨论】:

  • 完美运行。接受并投票赞成。感谢您的时间和深入的解释。
  • 没问题!祝你好运,玩得开心!
猜你喜欢
  • 2018-04-30
  • 2016-11-13
  • 1970-01-01
  • 1970-01-01
  • 2019-01-18
  • 1970-01-01
  • 1970-01-01
  • 2014-07-12
  • 1970-01-01
相关资源
最近更新 更多