如何将此 POST 方法转换为有效的 Node.js 代码?

How can I turn this POST method into valid Node.js code?

使用 https://mws.amazonservices.com/scratchpad/index.html,我能够向 MWS 端点发出有效请求,其详细信息如下所示:

POST /Products/2011-10-01?AWSAccessKeyId=myAWSAccessKeyId
  &Action=GetMatchingProductForId
  &SellerId=mySellerId
  &SignatureVersion=2
  &Timestamp=2018-08-14T01%3A00%3A39Z
  &Version=2011-10-01
  &Signature=6xwEi3Mk9Ko9v9DyF9g6zA4%2Buggi7sZWTlUmNDxHTbQ%3D
  &SignatureMethod=HmacSHA256
  &MarketplaceId=ATVPDKIKX0DER
  &IdType=UPC
  &IdList.Id.1=043171884536 HTTP/1.1
Host: mws.amazonservices.com
x-amazon-user-agent: AmazonJavascriptScratchpad/1.0 (Language=Javascript)
Content-Type: text/xml 

我怎样才能把它变成一个有效的 URL,我可以用它来从我的应用程序发出请求,即使用 fetch 或其他一些 javascript 实现。我试着获取信息并像这样制作 URL:

https://mws.amazonservices.com/Products/2011-10-01? 
AWSAccessKeyId=myAWSAccessKeyId
&Action=GetMatchingProductForId
&SellerId=mySellerId
&SignatureVersion=2
&Timestamp=2018-08-14T01%3A00%3A39Z
&Version=2011-10-01
&Signature=6xwEi3Mk9Ko9v9DyF9g6zA4%2Buggi7sZWTlUmNDxHTbQ%3D
&SignatureMethod=HmacSHA256
&MarketplaceId=ATVPDKIKX0DER
&IdType=UPC
&IdList.Id.1=043171884536 

,我试图通过邮递员向其发送 POST 请求,但出现此错误:

<?xml version="1.0"?>
<ErrorResponse xmlns="https://mws.amazonservices.com/">
<Error>
    <Type>Sender</Type>
    <Code>InvalidParameterValue</Code>
    <Message>Value 2
   for parameter SignatureVersion is invalid.</Message>
</Error>
<RequestID>6ded1eed-eb92-4db6-9837-3453db0f8a77</RequestID>
</ErrorResponse> 

如何使用 javascript 向 MWS 端点发出有效请求?

您可以使用 superagent、axios 或 request 等 npm 模块。

const agent = require('superagent)

agent
  .post('https://mws.amazonservices.com/Products/2011-10-01')
  .query({
    AWSAccessKeyId: myAWSAccessKeyId,
    Action: GetMatchingProductForId,
    SellerId: mySellerId,
    SignatureVersion: 2,
    Timestamp: 2018-08-14T01%3A00%3A39Z,
    Version: 2011-10-01,
    Signature: 6xwEi3Mk9Ko9v9DyF9g6zA4%2Buggi7sZWTlUmNDxHTbQ%3D,
    SignatureMethod: HmacSHA256,
    MarketplaceId: ATVPDKIKX0DER,
    IdType: UPC,
    IdList.Id.1: 043171884536
  })
  .then(res => {
    console.log('here is the response');
    console.log(res)
  })
  .catch(error => {
    console.log('here is the error');
    console.log(error);
  })

我没有写过针对 AWS 的文章,但您确定这些参数应该与查询字符串一起发送吗?通常用post,参数随body一起发送?

您从 Postman 收到的错误告诉您您正在访问服务器,但您发送的值有问题。例如:SignatureVersion 应该是 1(或其他)。

我使用 node-fetch 向 MWS 端点发出有效请求。您可以查看下面给出的代码。

  var param = {};
  param['AWSAccessKeyId']   =  'xxxxxxxxxxxxx'; 
  param['Action']           = 'GetMatchingProductForId';
  param['MarketplaceId']    =  'xxxxxxxxxxxxx'; 
  param['SellerId']         =  'xxxxxxxxxxxxx'; 
  param['IdType']           =  'ISBN'; 
  param['IdList.Id.1']      =  'xxxxxxxxxx'; 
  param['ItemCondition']    = 'New'; 
  param['SignatureMethod']  = 'HmacSHA256';  
  param['SignatureVersion'] = '2'; 
  param['Timestamp']        = encodeURIComponent(new Date().toISOString());
  param['Version']          = '2011-10-01'; 
  secret = 'xxxxxxxxxxxxx'; 

  var url = [];

  for(var i in param){
    url.push(i+"="+param[i])
  }

  url.sort();
  var arr = url.join("&");

  
  var sign  = 'POST\n'+'mws.amazonservices.com\n'+'/Products/2011-10-01\n'+arr;

  const crypto = require('crypto');
  let s64 = crypto.createHmac("sha256", secret).update(sign).digest('base64');

  let signature = encodeURIComponent(s64);

  var bodyData = arr+"&Signature="+signature;


  await fetch('https://mws.amazonservices.com/Products/2011-10-01', {
    method: 'POST',
    body: bodyData,
    headers: {
      'content-type': 'application/x-www-form-urlencoded',
       'Accept': '',
    },
  })
  .then(res => {
   console.log(res)
    
  })
  .catch(error => {
     console.log('Request failed', error);
  });
  
}

amazon-mws 包也可用于 Node。

https://www.npmjs.com/package/amazon-mws