Signature doesnt match


#1

Hi, i wrote a google app script to download a report from mws. the script is failing and returning the following message

“The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.”

i have tested the mws credentials on mws scratchpad and it works perfectly

Please let me know what could be wrng in the below script

function processData() {

var signature = getSignature(AWSAccessKeyId, sellerID, MWSAuthToken, Timestamp,reportId,SecretKey,MarketplaceIdList1 );

var datarows = extractAPIData(AWSAccessKeyId, sellerID, MWSAuthToken, Timestamp,reportId,SecretKey, signature,MarketplaceIdList1)

}

function getSignature(awsKeyId, sellerId, authToken, timeStamp, reportId, SecretKey, mktId) {

var TS = encodeURIComponent(timeStamp);

//Unsignged Query String.

var QS = 'GET\n'+'mws.amazonservices.com?'+'AWSAccessKeyId='+awsKeyId+ '&Action=GetReport' + '&MWSAuthToken='+authToken 
+ '&SellerId='+sellerId
+ '&ReportId='+reportId    
+ '&SignatureMethod=HmacSHA256'
+ '&SignatureVersion=2'
+ '&Timestamp='+TS
+ '&Version=2009-01-01'  

//Signed Query String using HmacSha256.
Logger.log(QS);

var Hmac = Utilities.computeHmacSha256Signature(QS, SecretKey); 
var Signature = Hmac.reduce(function(str,chr){
  chr = (chr < 0 ? chr + 256 : chr).toString(16);
  return str + (chr.length==1?'0':'') + chr;
},'');

//Converting Hmac variable to base64 then URIencode. Signature will not be accepted if not URI encoded.


var ST = Utilities.base64Encode(Hmac);
var Fin = encodeURIComponent(ST)

return Fin;

}

function extractAPIData(awsKeyId, sellerId, authToken, timeStamp, reportId, SecretKey, Signature,mktId) {

var Endpoint = 'https://mws.amazonservices.com/?’;

var SQS1 = Endpoint+‘AWSAccessKeyId=’+awsKeyId+’&Action=GetReport’;

var SQS2 = ‘&MWSAuthToken=’+authToken

  • ‘&MarketplaceId=’+mktId
  • ‘&ReportId=’+reportId
  • ‘&SellerId=’+sellerId
  • ‘&SignatureVersion=2’
  • ‘&Timestamp=’+timeStamp
  • ‘&Version=2009-01-01’
  • ‘&Signature=’+Signature
  • ‘&SignatureMethod=HmacSHA256’

var SQSURL = SQS1+SQS2

//Response is sent back as an Xml.

var data = extractData(SQSURL)

return data;

}


#2

If your credentials works via MWS then all you have to do to troubleshoot your code is to use the same timestamp and compare the signatures. Ensure that you are indeed getting the same signature.