3
var data = {
"file": "<InventoryFeed xmlns=\"http://walmart.com/\">\n    <InventoryHeader>\n        <version>1.4</version>\n    </InventoryHeader>\n    <inventory>\n        <sku>JW00726</sku>\n        <quantity>\n            <unit>EACH</unit>\n            <amount>25</amount>\n        </quantity>\n    </inventory>\n    <inventory>\n        <sku>JW00663</sku>\n        <quantity>\n            <unit>EACH</unit>\n            <amount>20</amount>\n        </quantity>\n    </inventory>\n</InventoryFeed>\n"
  };
  
  var options = {
    "method" : "POST",
    "headers": {
        "Authorization": "Basic "+Global_Auth,
        "WM_QOS.CORRELATION_ID": Global_CORRELATION_ID,
        "WM_SVC.NAME": Global_SVC_NAME,
        "WM_SEC.ACCESS_TOKEN":GetAccessToken(),
        "WM_CONSUMER.CHANNEL.TYPE": "#",
        "Accept": "application/json",
        "mimeType": "multipart/form-data",
        "Content-Type": "application/x-www-form-urlencoded"
      },
    "payload" : data,
    "muteHttpExceptions" : true
  };
  var url = "https://marketplace.walmartapis.com/v3/feeds?feedType=inventory";
  var response = UrlFetchApp.fetch(url, options);
  var res = JSON.parse(response.getContentText());

在 API 响应中获得 200(ok)响应,但值未更新到沃尔玛提要中。

4

1 回答 1

3

当我搜索https://marketplace.walmartapis.com/v3/feeds?feedType=inventoryWalmart API 的端点时,我发现了 2 个模式。

  1. 貌似这是官方文档。参考
  2. 这似乎是 Postman 的“Walmart Partner Apis Prod_Publish”。参考

在这个答案中,我想使用上述 2 个文档提出一个答案。

模式一:

在这个模式中,使用的是官方文档。在这种情况下,数据是用 发送的multipart/form-data。当你的脚本被修改后,它变成如下。数据来自您的脚本。

修改后的脚本:

var data = `<InventoryFeed xmlns="http://walmart.com/"><InventoryHeader><version>1.4</version></InventoryHeader><inventory><sku>JW00726</sku><quantity><unit>EACH</unit><amount>25</amount></quantity></inventory><inventory><sku>JW00663</sku><quantity><unit>EACH</unit><amount>20</amount></quantity></inventory></InventoryFeed>`;
var options = {
  "method": "POST",
  "headers": {
    "Authorization": "Basic " + Global_Auth,
    "WM_QOS.CORRELATION_ID": Global_CORRELATION_ID,
    "WM_SVC.NAME": Global_SVC_NAME,
    "WM_SEC.ACCESS_TOKEN": GetAccessToken(),
    "WM_CONSUMER.CHANNEL.TYPE": "#",
    "Accept": "application/json",
  },
  "payload": { "file": Utilities.newBlob(data, "text/xml") }, // or "application/xml" instead of "text/xml"
  "muteHttpExceptions": true
};
var url = "https://marketplace.walmartapis.com/v3/feeds?feedType=inventory";
var response = UrlFetchApp.fetch(url, options);
var res = JSON.parse(response.getContentText());
  • multipart/form-data使用 UrlFetchApp 请求时,不需要设置内容类型。它是自动设置的边界。
  • 当我看到您的脚本时,data会以内容类型的形式发送application/x-www-form-urlencoded。我认为这可能是您的问题的原因。

模式二:

在此模式中,使用 Postman 的“Walmart Partner Apis Prod_Publish”。在这种情况下,数据是用 发送的application/json。样品卷曲如下。

curl --location --request POST 'https://marketplace.walmartapis.com/v3/feeds?feedType=inventory' \
--header 'WM_SVC.NAME: Walmart Marketplace' \
--header 'WM_QOS.CORRELATION_ID: test' \
--header 'Accept: application/json' \
--header 'WM_SEC.ACCESS_TOKEN: {{token}}' \
--header 'Content-Type: application/json' \
--data-raw '{"InventoryHeader":{"version":"1.4"},"Inventory":[{"sku":"1068155","quantity":{"unit":"EACH","amount":"10"}},{"sku":"10210321","quantity":{"unit":"EACH","amount":"20"}}]}'

这将转换为 Google Apps 脚本。

示例脚本:

var data = {"InventoryHeader":{"version":"1.4"},"Inventory":[{"sku":"JW00726","quantity":{"unit":"EACH","amount":25}},{"sku":"JW00663","quantity":{"unit":"EACH","amount":20}}]};
var options = {
  "method": "POST",
  "headers": {
    "Wm-Qos.Correlation-Id": Global_CORRELATION_ID,
    "Wm-Svc.Name": Global_SVC_NAME,
    "Wm-Sec.Access-Token": GetAccessToken(),
    "Accept": "application/json",
  },
  "contentType": "application/json",
  "payload": JSON.stringify(data),
  "muteHttpExceptions": true
};
var url = "https://marketplace.walmartapis.com/v3/feeds?feedType=inventory";
var response = UrlFetchApp.fetch(url, options);
console.log(response.getContentText())
var res = JSON.parse(response.getContentText());

笔记:

  • "mimeType": "multipart/form-data"未在请求标头中使用。
  • 在上面的脚本中,它假设您的值data和请求标头中的值是使用 API 的正确值。请注意这一点。

参考:

于 2021-04-20T02:16:29.477 回答