google-api-java-clientgoogle-api-clientgoogle-shopping-apigoogle-shopping

google shop client API returns 400 bad request error while adding an item


I'm working on a solution to add items in the google shopping market with a java application. Following the tutorial was a real mess because of the missing classes of the laste release, however after setting the 1.5-beta with maven I managed to make the base example to typecheck correctly all the classes. However I still can get it to make the simple task of adding one item. Instead of posting the whole code I'm posting the part that only creates the Product 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;
  }

I managed to take the content of the html request:

    <?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>

Following the advice of a guy in the google-api forum I've set the website link, shipping and taxes in the merchant central.

I really don't know what to do, somebody has a clue of what's going on?


Solution

  • In the end I found a solution thanks to the google groups of the api. After I was able to get the content of my http request with this code:

    // 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();
    

    I used the content api demo tool to send my insert request by copy-pasting my raw xml. From the tool I was able to get the errors in the response, however I noticed that the response I get by using the java api is different, because the HttpClient class trows an exception if it can't parse a Product.