无法使用 Walmart API 更新提要 [英] Unable to update feed using Walmart API

查看:53
本文介绍了无法使用 Walmart API 更新提要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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) 响应,但值未更新到 Walmart 提要中.

in API response getting 200 (ok) response but values are not updating into Walmart feed.

推荐答案

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

When I searched about the endpoint of https://marketplace.walmartapis.com/v3/feeds?feedType=inventory of Walmart API, I found 2 patterns.

  1. 好像这是官方文档.参考
  2. 这好像是Walmart Partner Apis Prod_Publish"在邮递员.参考

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

In this answer, I would like to propose an answer using above 2 documents.

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

In this pattern, the official document is used. In this case, the data is sent with multipart/form-data. When your script is modified, it becomes as follows. The data is used from your script.

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

  • 当使用 UrlFetchApp 请求 multipart/form-data 时,不需要设置内容类型.自动设置边界.
  • 当我看到您的脚本时,data 以内容类型为 application/x-www-form-urlencoded 的形式发送.我认为这可能是您出现问题的原因.
    • When multipart/form-data is requested with UrlFetchApp, it is not required to set the content type. It is automatically set with the boundary.
    • When I saw your script, data is sent as form with the content type of application/x-www-form-urlencoded. I thought that this might be the reason of your issue.
    • 在这种模式中,Walmart Partner Apis Prod_Publish"在邮递员使用.在这种情况下,数据通过 application/json 发送.示例卷曲如下.

      In this pattern, "Walmart Partner Apis Prod_Publish" at Postman is used. In this case, the data is sent with application/json. And the sample curl is as follows.

      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 脚本.

      This is converted to Google Apps Script.

      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 的正确值.请注意这一点.
      • Note:

        • "mimeType": "multipart/form-data" is not used in the request header.
        • In above scripts, it supposes that your values of data and the values in the request header are correct values for using the API. Please be careful this.
        • 这篇关于无法使用 Walmart API 更新提要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆