aws4: Aws Sign4 wrong calcualation

I am using API method “b) Directly making calls to Selling partner APIs”.

I followed all the steps correctly:

Step 1. Request a Login with Amazon access token –> This API is working successfully & Getting access token.

Step 2. Construct a Selling Partner API URI

After that, I took access_token and pass into options header request- x-amz-access-token. Step 3. Add headers to the URI Step 4. Create and sign your request After completing 4 steps, my code are as follows:

let options = {
    service: 'execute-api',
    region:'eu-west-1',
    method: 'GET',
    url: 'https://sellingpartnerapi-eu.amazon.com/vendor/orders/v1/purchaseOrders/?includeDetails=true&createdBefore=2020-04-20T14:00:00-08:00&createdAfter=2020-04-14T00:00:00-08:00',
    headers: {
      'Content-Type': 'application/json',
      'host':'sellingpartnerapi-eu.amazon.com',
      'x-amz-access-token':access_token,
      'x-amz-date': '20200604T061745Z',
      'user-agent':'My App(Language=Node.js;Platform=Windows/10)'
    }
  }

let signedRequest = aws4.sign(options,
{
  secretAccessKey: secretAccessKey,
  accessKeyId: accessKeyId,
})
      
console.dir("signedRequest");
console.dir(signedRequest);

delete signedRequest.headers['Host']
delete signedRequest.headers['Content-Length']
      
request(awsres, function(err, res, body) {
    console.dir("err");
    console.dir(err);
    console.dir("res");
    console.dir(res.body);
});

But, After calling API ‘https://sellingpartnerapi-eu.amazon.com/vendor/orders/v1/purchaseOrders’. I am getting a sign response with error- ‘The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method.’

Please suggest for how to calculate correct was4 signature in details.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (11 by maintainers)

Most upvoted comments

Thank you so much @mhart I am getting an API response successfully, because of your code. I have also done a few changes to it. Here I am giving my code are as follows:

           var access_token = data.access_token;
            var endpoint = "sellingpartnerapi-eu.amazon.com";
            var path = "/vendor/orders/v1/purchaseOrders";
            var queryString ="?includeDetails=true&createdBefore=2020-05-20T14:00:00-08:00&createdAfter=2020-05-14T00:00:00-08:00";
            let options = {
              service: 'execute-api',
              region: 'eu-west-1',
              host: endpoint,
              path: path+queryString,
              headers: {
                'x-amz-access-token': access_token,
              }
            }
            
            let signedRequest = aws4.sign(options, {
              secretAccessKey: secretAccessKey,
              accessKeyId: accessKeyId,
            })

            signedRequest.url = 'https://' + signedRequest.host + signedRequest.path
 
            request(signedRequest, function(err, res, body) {
                // console.dir("err");
                // console.dir(err);
                console.dir("res");
                console.dir(res.body);
            });

Thank you so much once again.