我正在设法按照文档编写代码。但不幸的是,它不起作用。下面是代码,请看一下,请让我知道我在做错什么或缺少字段。我已经通过引用某人的代码编写了此代码。
function test(){
var date = new Date();
var amz_date = date.toISOString(); //2018-05-01T20:40:50.940Z
var yearMonthDay= Utilities.formatDate(date, 'UTC', 'yyyyMMdd');
var hourMinuteSec = Utilities.formatDate(date, 'UTC', 'HHmmss');
var dateForStringToSign = yearMonthDay +'T'+hourMinuteSec+'Z'; //20180501T204050Z
var datestamp = yearMonthDay; // 20180501
var access_key = "xxxxxxxxxxxxx";
var secret_key = "xxxxxxxxxxxxx";
var method = 'POST';
var host = 'mws.amazonservices.com';
var endpoint = 'https://mws.amazonservices.com/Orders/2013-09-01';
var canonical_uri = '/Orders/2013-09-01';
var canonical_querystring = 'AWSAccessKeyId='+encodeURIComponent('xxxxxxxxxxxxx');
canonical_querystring += '&Action='+encodeURIComponent('GetOrder');
canonical_querystring += '&AmazonOrderId.Id.1='+encodeURIComponent('xxxxxxxxxxxxx');
canonical_querystring += '&MWSAuthToken='+encodeURIComponent('xxxxxxxxxxxxx');
canonical_querystring += '&SellerId='+encodeURIComponent('xxxxxxxxxxxxx');
canonical_querystring += '&SignatureMethod='+encodeURIComponent('HmacSHA256');
canonical_querystring += '&SignatureVersion='+encodeURIComponent('2');
canonical_querystring += '&Timestamp='+encodeURIComponent(amz_date);
canonical_querystring += '&Version='+encodeURIComponent('2013-09-01');
//To construct the finished canonical request, combine all the components
var finished_canonical= method + "\n"+host+"\n" +canonical_uri+ "\n" + canonical_querystring;
//Calculate the Signature
var signature=getSignature(finished_canonical,secret_key);
//Add signature to querystring
canonical_querystring += '&Signature='+encodeURIComponent(signature);
Logger.log(canonical_querystring);
var request_url = endpoint + '?' + canonical_querystring;
var options = {
'method' : 'post',
'muteHttpExceptions':true,
'host': host,
}
var x = UrlFetchApp.fetch(request_url, options);
Logger.log(x);
}
这是GAS中的签名生成器功能,
//Return Signatue (hash) from HmacSha256
function getSignature(message,secret){
var byteSignature = Utilities.computeHmacSha256Signature(message, 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;
},'');
return signature;
}
正如您在代码中看到的那样,生成签名后,将其作为最后一个参数附加到查询字符串中。那会是个问题吗?
答案 0 :(得分:0)
阅读here一节“如果您创建自己的客户端库”。它讨论了格式化URL和签名的方法。
此外,以下链接可能会有所帮助:https://stackoverflow.com/a/36417555/3047