谷歌商店客户端API在添加项目时返回400错误请求错误

时间:2014-03-24 15:57:07

标签: google-api-java-client google-api-client google-shopping-api google-shopping

我正在研究一种使用java应用程序在Google购物市场中添加商品的解决方案。 由于缺少最新版本的类,教程之后是一个真正的混乱,但是在使用maven设置1.5-beta之后,我设法使基本示例正确地检查所有类。但是我仍然可以让它完成添加一个项目的简单任务。我没有发布整个代码,而是发布仅创建产品Feed的部分。

 private Product createProduct(String id) {
    Product product = new Product();
    product.title = "Red wool sweater";
    product.content =
        new Content(
            "text",
            "Comfortable and soft, this sweater will keep you warm on those "
            + "cold winter nights. Red and blue stripes.");
    product.appControl = new AppControl();
    product.appControl.addRequiredDestination("ProductAds");

    product.externalId = id;
    product.lang = "it";
    product.country = "it";
    product.condition = "nuovo";
    product.price = new Price("EUR", new BigDecimal("12.99"));
    product.googleProductCategory = "Sporting Goods > Exercise & Fitness > Cardio Machines > Exercise Bikes";

    // add a link
    Link link = new Link();
    link.rel = "alternate";
    link.href = homepage + "item1-info-page.html";
    link.type = "text/html";
    product.links.add(link);

    //shipping
    List<com.google.api.client.sample.structuredcontent.model.Shipping> shipping = new ArrayList<com.google.api.client.sample.structuredcontent.model.Shipping>();
    com.google.api.client.sample.structuredcontent.model.Shipping s = new com.google.api.client.sample.structuredcontent.model.Shipping();

    s.shippingCountry="IT";
    Price p = new Price();
    p.unit = "EUR";
    p.value = new BigDecimal("0");
    s.shippingPrice= p;
    //s.shippingRegion=""
    s.shippingService = "Speedy Shipping - Ground";
    shipping.add(s);
    product.shippingRules = shipping;

    //tax
    //Useless in italy
    // set image links
    List<String> imageLinks = new ArrayList<String>();
    imageLinks.add("http://www.example.com/image1.jpg");
    imageLinks.add("http://www.example.com/image2.jpg");
    product.imageLinks = imageLinks;

    return product;
  }

我设法获取了html请求的内容:

    <?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns:sc="http://schemas.google.com/structuredcontent/2009" xmlns:scp="http://schemas.google.com/structuredcontent/2009/products"><app:control><sc:required_destination dest="ProductAds" />
</app:control>
<content type="text">
Comfortable and soft, this sweater will keep you warm on those cold winter nights. Red and blue stripes.</content>
<link href="http://www.joinstore.it/item1-info-page.html" rel="alternate" type="text/html" />
<sc:adult>false</sc:adult><sc:content_language>it</sc:content_language>
<sc:id>1234567</sc:id>
<sc:image_link>http://www.example.com/image1.jpg</sc:image_link>
<sc:image_link>http://www.example.com/image2.jpg</sc:image_link>
<sc:target_country>it</sc:target_country>
<scp:condition>nuovo</scp:condition>
<scp:featured_product>false</scp:featured_product>
<scp:google_product_category>Sporting Goods > Exercise &amp; Fitness > Cardio Machines > Exercise Bikes</scp:google_product_category>
<scp:price unit="EUR">12.99</scp:price>
<scp:shipping><scp:shipping_country>IT</scp:shipping_country>
<scp:shipping_price unit="EUR">0</scp:shipping_price>
<scp:shipping_service>Speedy Shipping - Ground</scp:shipping_service></scp:shipping>
<title>Red wool sweater</title>
</entry>

根据google-api论坛中某人的建议,我在商家中心设置了网站链接,运费和税金。

我真的不知道该怎么做,有人知道发生了什么事吗?

1 个答案:

答案 0 :(得分:0)

最后,我找到了一个解决方案,感谢api的google群组。 在我能够使用以下代码获取我的http请求的内容之后:

// HTTP request
HttpRequest request = requestFactory.buildPostRequest(new GoogleUrl(url), atomContent);
File f = new File(Main.BASE_FOLDER_PATH + "ciao.xml");
FileOutputStream out = new FileOutputStream(f);
request.getContent().writeTo(out);
out.close();
HttpResponse re = null;
re =  request.execute();

我使用content api demo tool通过复制粘贴原始xml来发送插入请求。 从工具中我能够得到响应中的错误,但是我注意到使用java api得到的响应是不同的,因为如果它无法解析产品,HttpClient类会抛出异常。