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

时间:2018-08-14 01:15:40

标签: node.js amazon-web-services api amazon-mws

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

<template>
    <div>
        <p contenteditable="true" @input="update">{{ text }}</p>
    </div>
</template>

<script>
    export default {
        name: 'ReadWrite',
            data () {
                return {
                    text: 'edit here!'
                }
            },
        methods: {
            update (e) {
                this.text = e.target.textContent
            }
        }
    }
</script>

<style scoped>
    :read-write {
        border: solid 1px #cccccc;
        padding: 5px;
    }
</style>

如何处理此问题,并将其转换为有效的网址,我可以使用该网址从我的应用发出请求,即使用提取或其他一些JavaScript实现。我试图获取信息并创建如下网址:

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 

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

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 

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

2 个答案:

答案 0 :(得分:1)

您可以使用诸如超级代理,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进行过编写,但是您确定这些参数应该与querystring一起发送。通常带有post,参数随主体一起发送吗?

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

答案 1 :(得分:0)

我使用 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