1

我嘗試使用下面的Node.js的代碼中插入一個產品到谷歌購物API,但我不斷收到錯誤:谷歌購物API Node.js的產品說明書申報表「INSERT請求必須註明產品」的錯誤

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

這裏是我的javascript代碼(我使用節點客戶端在這裏: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); 
}); 

在此先感謝您的幫助!

+0

的參數,因爲它似乎在產品屬性已被命名爲資源的結果。你可以嘗試使用這個對象簽名{merchantId:* MERCHANTID *,資源:產品}? – Boot750

+0

完美!謝謝@ Boot750! – user2890042

+0

我會提供一個awnser,這樣可能會幫助你多一點 – Boot750

回答

2

產品的插入函數具有以下簽名

/** 
* 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 
*/ 

這個簽名可以在文件中找到:https://github.com/google/google-api-nodejs-client/blob/master/apis/content/v2.js

正如你所見,param對象有一個叫做resource的屬性,它是你想要發送給服務的實際對象。

當你需要改變你傳給插入功能從{merchantId:MERCHANTID,product:product},...{merchantId:MERCHANTID,resource:product},...