【发布时间】: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/…