2

我可以使用 Nodejs 从 Walmart Canada Marketplace API 获取数据,但我无法使用他们的批量提要端点发布新商品。我收到了 404“错误请求”。我不确定错误是在 POST 请求中还是在 JSON 中。我也不确定沃尔玛加拿大是否真的接受 JSON 文件或只是声称接受。

这是我的代码:

import { createSign, randomBytes } from 'crypto'
import fetch from 'node-fetch';
import { resolve } from 'url';
import fs from 'fs';


const method = 'post';

const BASE_URL = 'https://marketplace.walmartapis.com/'

const PK_HEADER = '\n-----BEGIN PRIVATE KEY-----\n'
const PK_FOOTER = '\n-----END PRIVATE KEY-----\n'

const WALMART_CONSUMER =  "consumer";
const WALMART_CHANNEL = 'channel';
const WALMART_SECRET = 'secret';


function generateCorrelationId () {
  return randomBytes(16).toString('hex')
}

function generateSignature (url, method, timestamp) {

  const privateKey = `${PK_HEADER}${WALMART_SECRET}${PK_FOOTER}`



  const stringToSign = WALMART_CONSUMER + '\n' +
                     url + '\n' +
                     method.toUpperCase() + '\n' +
                     timestamp + '\n'

  const sign = createSign('RSA-SHA256')

  sign.update(stringToSign)
  return sign.sign(privateKey, 'base64')
}
  const url = resolve(BASE_URL, "/v3/ca/feeds?feedType=item")

  const timestamp = Date.now()
  const signature = generateSignature(url, method, timestamp)

  const headers = {
    'WM_SVC.NAME': 'Walmart Marketplace',
    'WM_CONSUMER.ID': WALMART_CONSUMER,
    'WM_SEC.TIMESTAMP': timestamp,
    'WM_SEC.AUTH_SIGNATURE': signature,
    'WM_QOS.CORRELATION_ID': generateCorrelationId(),
    'WM_CONSUMER.CHANNEL.TYPE': WALMART_CHANNEL,
    'WM_TENANT_ID' : 'WALMART.CA',
    'WM_LOCALE_ID' : 'en_CA',
    'Accept': 'application/xml',
    'Content-Type': 'multipart/form-data'

  }

  fetch(url, {
    method: method,
    headers: headers,
    formData: {
        'file': {
          'value': fs.createReadStream('wm_new_item.json'),
          'options': {
            'filename': 'wm_new_item.json'
          }
        }
      }
  })
  .then(response => console.log(response))
  .catch(function (error) {
    console.log(error)
  });

这是我的 json 文件:

{
"MPItemFeedHeader":{
   "subset":"INTERNAL",
   "mart":"WALMART_CA",
   "sellingChannel":"marketplace",
   "subCategory":"toys_other",
   "locale":"en",
   "version":"3.2",
   "requestId": "1606485958",
   "requestBatchId": "1606485958",
   "processMode":"REPLACE"
},
"MPItem":[
   {
      "Orderable":{
         "sku":"2663d9467e64a0a94ab0eeeccd9f37dd",
         "productIdentifiers":{
            "productIdType":"UPC",
            "productId":"078257310609"
         },
         "productName":"abc",
         "brand":"abc",
         "price":1.00,
         "fulfillmentLagTime":6,
         "floorPrice":1.00,
         "startDate":"2021-04-20T08:37:30Z",
         "endDate":"2021-04-20T08:37:30Z",
         "ProductIdUpdate":"No",
         "SkuUpdate":"No",
         "ShippingWeight":1
      },
      "Visible":{
         "Toys":{
            "shortDescription":"abc",
            "mainImageUrl":"https://images-na.ssl-images-amazon.com/images/I/71X4z76HUTS._AC_SL1500_.jpg",
            "productSecondaryImageURL":[
               "https://images-na.ssl-images-amazon.com/images/I/71X4z76HUTS._AC_SL1500_.jpg",
               "https://images-na.ssl-images-amazon.com/images/I/71X4z76HUTS._AC_SL1500_.jpg"
            ],
            "msrp":1.00,
            "variantGroupId":"abc",
            "variantAttributeNames":[
               "color"
            ],
            "isPrimaryVariant":"Yes"

         }
      }
   }
]

}

谢谢!

4

1 回答 1

0

我在另一个问题的这个答案的帮助下弄清楚了。

你最好参考这个而不是沃尔玛官方文档

"https://marketplace.walmartapis.com/v3/feeds您需要向在 url 上附加您的类型的端点提交发布请求?feedType=[something]

如果您向他们发送 JSON ,您需要确保您的"Content-Type"is 。"application/json"

您不需要像文档建议的那样多部分发送它,只需将整个 JSON 文件作为正文发送,它应该可以正常工作。

于 2021-07-30T14:08:25.473 回答