我的MWS请求中的时间戳有什么问题?

时间:2018-08-14 18:52:28

标签: amazon-web-services timestamp amazon-mws superagent

如果我通过暂存器(AmazonServices/Scratchpad)向MWS提交了请求, 它成功,并且我能够查看成功请求的详细信息。特别是,请求上的时间戳看起来像这样:

&Timestamp=2018-08-14T18%3A30%3A02Z

如果我照原样使用此时间戳,并尝试在我的代码中使用它来发出相同的确切请求,则会收到错误消息:

<Message>Timestamp 2018-08-14T18%3A30%3A02Z must be in ISO8601 
format</Message>\n 

这是我要放置在其中的函数:(某些字符在敏感参数中已更改)

exports.sendRequest = () => {

  return agent
    .post('https://mws.amazonservices.com/Products/2011-10-01')
    .query({
      AWSAccessKeyId: encodeURIComponent('BINAJO5TPTZ5TTRLNGQA'),
      Action: encodeURIComponent('GetMatchingProductForId'),
      SellerId: encodeURIComponent('H1N1R958BK8TTH'),
      SignatureVersion: encodeURIComponent('2'),
      Timestamp: '2018-08-14T18%3A30%3A02Z',
      Version: encodeURIComponent('2011-10-01'),
      Signature: encodeURIComponent(exports.generateSignature()),
      SignatureMethod: encodeURIComponent('HmacSHA256'),
      MarketplaceId: encodeURIComponent('ATVPDKIKX0DER'),
      IdType: encodeURIComponent('UPC'),
      'IdList.Id.1': encodeURIComponent('043171884536')
    })
    .then(res => {
      console.log('here is the response');
      console.log(res)
    })
    .catch(error => {
      console.log('here is the error');
      console.log(error);
    })
} 

更奇怪的是,这是请求发送到的路径:

path: '/Products/2011-10-01? 

AWSAccessKeyId = BINAJO5ZPTZ5YTTPNGQA&动作= GetMatchingProductForId&SellerId = H1N1R958ET8THH&SignatureVersion = 2&时间戳= 2018-08-14T18%253A30%253A02Z&版本= 2011-10-01&签名= LwZn5of9NwCAgOOB0jHAbYMeQT31M6y93QhuX0d%252BCK8%253D&是SignatureMethod = HmacSHA256&MarketplaceId = ATVPDKIKX0DER&IdType = UPC&IdList.Id.1 = 043171884536' },< / p>

时间戳与我在查询中放置的时间戳不同。为什么会这样?

1 个答案:

答案 0 :(得分:2)

您的HTTP库已经在为您进行url编码,因此您正在对内容进行双重编码。删除所有对encodeURIComponent()的引用,并使用:而非%3A正常格式化时间戳。观察生成的URL发生了什么。

为什么?重复进行URL编码并不安全。

:一遍变成%3A,但是第二遍变成%253A,这是错误的。