[英]Accessing Amazon MWS using Google Apps Script
我以某種方式設法按照文檔編寫代碼。 但不幸的是,它不起作用。 下面是代碼,請看一下,請讓我知道我在做錯什么或缺少字段。 我已經通過引用某人的代碼編寫了此代碼。
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;
}
如您在代碼中所見,生成簽名后,將其作為最后一個參數附加到查詢字符串中。 那會是個問題嗎?
閱讀此處稱為“如果您創建自己的客戶端庫”的部分。 它討論了格式化URL和簽名。
此外,這是一個可能有用的鏈接: https : //stackoverflow.com/a/36417555/3047
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.