Search code examples
javascriptnode.jsgoogle-api-nodejs-clientgoogle-shopping-api

Google Shopping API node.js product insert returns "INSERT request must specify product" error


I'm trying to insert a product to the Google Shopping API using the following node.js code but I keep getting the error:

{ [Error: [product] INSERT request must specify product]
code: 400,
errors:
[ { domain: 'global',
   reason: 'required',
   message: '[product] INSERT request must specify product' } ] }

Here is my javascript code (I'm using the node client here: https://github.com/google/google-api-nodejs-client/):

var google = require('googleapis');

var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2(*OAUTHDETAILS*);

oauth2Client.setCredentials({
    access_token: '*ACCESSTOKEN*',
  //refresh_token: 'REFRESH TOKEN HERE'
});

var content = google.content({ version: 'v2', auth: oauth2Client });

var product = {
    "channel": "online",
    "contentLanguage": "en",
    "offerId": *PRODUCTID*,
    "targetCountry": "us",
    "identifierExists": false,
    "condition": "new",
    "link": "*PRODUCTLINK*",
    "price": {
        "currency": "usd",
        "value": *VALUE*
    },
    "title": *PRODUCTTITLE*,
    "availability": "in stock",
    "description": *DESCRIPTION*,
    "googleProductCategory": *PRODUCTCATEGORY*,
    "ageGroup": "adult",
    "color": *PRODUCTCOLOR*,
    "gender": "unisex",
    "sizes": [
        "XS",
        'S',
        'M',
        'L',
        'XL'
    ],
    "imageLink": *IMGURL*
};

content.products.insert({merchantId:*MERCHANTID*,product:product},function(err, resp) {
    // handle err and response
    console.log(err);
    console.log(resp);
});

Thanks in advance for any help!


Solution

  • The Insert function of the products has the following signature

    /** 
    * content.products.insert 
    * 
    * @desc Uploads a product to your Merchant Center account. 
    * 
    * @alias content.products.insert 
    * @memberOf! content(v2) 
    * 
    * @param  {object} params - Parameters for request 
    * @param  {boolean=} params.dryRun - Flag to run the request in dry-run mode. 
    * @param  {string} params.merchantId - The ID of the managing account. 
    * @param  {object} params.resource - Request body data 
    * @param  {callback} callback - The callback that handles the response. 
    * @return {object} Request object 
    */ 
    

    This signature can be found in the file: https://github.com/google/google-api-nodejs-client/blob/master/apis/content/v2.js.

    As you can see there the param object has a property called resource which is the actual object you want to send to the service.

    As a result you need to change the parameter you pass to the insert function from {merchantId:MERCHANTID,product:product},... to {merchantId:MERCHANTID,resource:product},...