Я создаю надстройку Google Sheet, в которую загружаю заказы FBA. Я пытался сделать успешный вызов API. Возможно, я что-то здесь упускаю?
Получаемый ответ:
Рассчитанная нами подпись запроса не соответствует предоставленной вами подписи. Проверьте свой AWS секретный ключ доступа и метод подписи. За подробностями обращайтесь к документации по сервису.
Google Sheet JavaScript код:
function POSTRequest() {
var config = amazonMWSConfigProperties.getProperty('amazonConfig');
var configData = JSON.parse(config);
var sellerID=configData.sellerID;
var accessKey=configData.accessKey;
var secretKey=configData.secretKey;
var authToken=configData.authToken;
var defaultMarket=configData.defaultMarket;
var url = 'https://mws.amazonservices.in/Orders/2013-09-01?';
var today1 = new Date();
var today = new Date();
var todayTime = Utilities.formatDate(today, "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'");
var yesterday = new Date();
yesterday.setDate(today.getDate() - 1);
yesterday.setHours(0,0,0,0);
var yesterdayTime = Utilities.formatDate(yesterday, "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'");
var dayBeforeYesterday = new Date();
dayBeforeYesterday.setDate(today.getDate() - 2);
dayBeforeYesterday.setHours(0,0,0,0);
var dayBeforeYesterdayTime = Utilities.formatDate(dayBeforeYesterday, "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'");
var unsignedURL =
'POST\nmws.amazonservices.in\n/Orders/2013-09-01\n'+
'AWSAccessKeyId=' +accessKey+
'&Action=ListOrders'+
'&CreatedAfter='+encodeURIComponent(Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'")) +
//'&CreatedBefore=2020-01-18T18%3A30%3A00Z' + //yesterdayTime +
'&FulfillmentChannel.Channel.1=AFN' +
'&MWSAuthToken=' +authToken+
'&MarketplaceId.Id.1=' +defaultMarket+
'&SellerId='+sellerID+
'&SignatureMethod=HmacSHA256'+
'&SignatureVersion=2'+
'&Timestamp='+encodeURIComponent(Utilities.formatDate(today1, "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'")) +
'&Version=2013-09-01';
Logger.log(unsignedURL);
var SignedRequest = calculatedSignature(unsignedURL, secretKey);
var Encoded = Utilities.base64Encode(SignedRequest);
var param = 'AWSAccessKeyId=' +accessKey+
'&Action=ListOrders'+
'&CreatedAfter='+encodeURIComponent(Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'")) +
//'&CreatedBefore=2020-01-18T18%3A30%3A00Z' + //yesterdayTime +
'&FulfillmentChannel.Channel.1=AFN' +
'&MWSAuthToken=' +authToken+
'&MarketplaceId.Id.1=' +defaultMarket+
'&SellerId='+sellerID+
'&Signature='+encodeURIComponent(SignedRequest) +
'&SignatureMethod=HmacSHA256'+
'&SignatureVersion=2'+
'&Timestamp='+encodeURIComponent(Utilities.formatDate(today1, "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'")) +
'&Version=2013-09-01';
Logger.log(url+param);
var result = UrlFetchApp.fetch(url+param);
//writeDataToXML(result);
Logger.log(result);
if (result.getResponseCode() == 200) {
//writeDataToXML(result);
}
}
function calculatedSignature(url,secret) {
var urlToSign = url;
var byteSignature = Utilities.computeHmacSha256Signature(urlToSign, secret);
// convert byte array to hex string
var signature = byteSignature.reduce(function(str,chr){
chr = (chr < 0 ? chr + 256 : chr).toString(16);
return str + (chr.length==1?'0':'') + chr;
},'');
//Logger.log("URL to sign: " + urlToSign);
//Logger.log("");
//Logger.log("byte " + byteSignature);
//Logger.log("");
//Logger.log("reg " + signature);
var byte64 = Utilities.base64Encode(byteSignature)
//Logger.log("base64 byte " + Utilities.base64Encode(byteSignature));
//Logger.log("");
//Logger.log("base64 reg " + Utilities.base64Encode(signature));
Logger.log("base64 reg " + byte64)
return byte64;
}