这是我到目前为止所拥有的:
function sha256(stringToSign, secretKey) {
return CryptoJS.HmacSHA256(stringToSign, secretKey);
}
function getAmazonItemInfo(barcode) {
var parameters =
"Service=AWSECommerceService&"
+ "AWSAccessKeyId=" + appSettings.amazon.accessKey + "&"
+ "Operation=ItemLookup&"
+ "ItemId=" + barcode
+ "&Timestamp=" + Date.now().toString();
var stringToSign =
"GET\n"
+ "webservices.amazon.com\n"
+ "/onca/xml\n"
+ parameters;
var signature = "&Signature=" + encodeURIComponent(sha256(stringToSign, appSettings.amazon.secretKey));
var amazonUrl =
"http://webservices.amazon.com/onca/xml?"
+ parameters
+ signature;
// perform a GET request with amazonUrl and do other stuff
}
当作为HTTP GET请求执行时,上述代码中的amazonUrl
值会导致以下来自Amazon的响应:
<?xml version="1.0"?>
<ItemLookupErrorResponse xmlns="http://ecs.amazonaws.com/doc/2005-10-05/">
<Error>
<Code>SignatureDoesNotMatch</Code>
<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.
</Message>
</Error>
<RequestId>[REMOVED]</RequestId>
</ItemLookupErrorResponse>
有用的链接:
答案 0 :(得分:4)
我用你的代码破解了我的工作。
function sha256(stringToSign, secretKey) {
var hex = CryptoJS.HmacSHA256(stringToSign, secretKey);
return hex.toString(CryptoJS.enc.Base64);
}
function timestamp() {
var date = new Date();
var y = date.getUTCFullYear().toString();
var m = (date.getUTCMonth() + 1).toString();
var d = date.getUTCDate().toString();
var h = date.getUTCHours().toString();
var min = date.getUTCMinutes().toString();
var s = date.getUTCSeconds().toString();
if(m.length < 2) { m = "0" + m; }
if(d.length < 2) { d = "0" + d; }
if(h.length < 2) { h = "0" + h; }
if(min.length < 2) { min = "0" + min; }
if(s.length < 2) { s = "0" + s}
var date = y + "-" + m + "-" + d;
var time = h + ":" + min + ":" + s;
return date + "T" + time + "Z";
}
function getAmazonItemInfo(barcode) {
var PrivateKey = "";
var PublicKey = "";
var AssociateTag = "";
var parameters = [];
parameters.push("AWSAccessKeyId=" + PublicKey);
parameters.push("ItemId=" + barcode);
parameters.push("Operation=ItemLookup");
parameters.push("Service=AWSECommerceService");
parameters.push("Timestamp=" + encodeURIComponent(timestamp()));
parameters.push("Version=2011-08-01");
parameters.push("AssociateTag=" + AssociateTag);
parameters.sort();
var paramString = parameters.join('&');
var signingKey = "GET\n" + "webservices.amazon.com\n" + "/onca/xml\n" + paramString
var signature = sha256(signingKey,PrivateKey);
signature = encodeURIComponent(signature);
var amazonUrl = "http://webservices.amazon.com/onca/xml?" + paramString + "&Signature=" + signature;
console.log(amazonUrl);
}
我用过Javascript的标题作为参考。
<script src="hmac-sha256.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/enc-base64-min.js"></script>
<script src="amazon.js"></script>
您需要修改部分内容,因为我更改了一些参数并且没有引用您的应用程序&#34;对象
我做了什么来解决它(从我记忆中)。
参数必须是按字母顺序排列的。我把它们放在一个数组中,然后对它们进行排序。我通过与&符号的连接来跟进。
我修改了sha256函数以返回RAW sha256的base64。在它以小写形式返回六角形之前,这是不正确的。
我打算在编码前添加base64,但sha256现在可以处理所有签名。
日期格式不正确。它返回了一个纪元时间戳而不是一个字符串时间戳。我乱砍了一个简单的时间戳选项。
此代码要求您还包括用于CryptoJS的Base64库。
答案 1 :(得分:1)
使用this Node.js library for AWS。它甚至包括an example专门用于产品广告API。
答案 2 :(得分:0)
在大卫的伟大答案的基础上,我做了一些调整。以下解决方案使用moment.js和crytpo-js,可用于按关键字搜索项目。我使用amazon scratch-pad来帮助构建目标调用。我发现了几件事:
const getAmazonItemInfo = (keywords) => {
let date = moment().startOf().add(-9, 'hours').format("YYYY-MM-DDThh:mm:ss.000") + 'Z'
let SecretKey = "GENERATED_IN_AFFILATES_ACCOUNT";
let AccessKey = "GENERATED_IN_AFFILATES_ACCOUNT";
let AssociateTag = "FOUND_IN_AFFILATES_ACCOUNT";
let parameters = [];
let url = 'webservices.amazon.co.uk' // UK account
//let url = 'webservices.amazon.com'// US account
parameters.push("AWSAccessKeyId=" + AccessKey);
parameters.push("Keywords=" + keywords);
parameters.push("Operation=ItemSearch");
parameters.push("SearchIndex=All");
parameters.push("ResponseGroup=" + encodeURIComponent('Images,ItemAttributes,Offers'));
parameters.push("Service=AWSECommerceService");
parameters.push("Timestamp=" + encodeURIComponent(date));
parameters.push("AssociateTag=" + AssociateTag);
parameters.sort();
let paramString = parameters.join('&');
let string_to_sign = "GET\n" + url + "\n" + "/onca/xml\n" + paramString
let signature = CryptoJS.HmacSHA256(string_to_sign, SecretKey);
signature = CryptoJS.enc.Base64.stringify(signature);
let amazonUrl = "http://" + url + "/onca/xml?" + paramString + "&Signature=" + signature;
return amazonUrl;
}
let keywords = 'iphone'
console.log(getAmazonItemInfo(keywords))