Hi I’m trying to convert the PHP Code example for an AWIS request found here:
http://aws.amazon.com/code/AWIS/402
to javascript / jquery. I feel like I’m very close, but I’m getting an “unauthorized” 401 response. I would love to get this up and running, and I was wondering if anyone could tell me what I’m doing wrong, or point me in the direction of an example using javascript.
<html>
<head>
<title>AWIS</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript" src="http://crypto-js.googlecode.com/files/2.3.0-crypto-sha256-hmac.js"></script>
<script type="text/javascript" src="http://crypto-js.googlecode.com/files/2.3.0-crypto-min.js"></script>
</head>
<body>
<div id="output"></div>
<script>
var actionName = 'UrlInfo';
var responseGroupName = 'Rank,LinksInCount';
var serviceHost = 'awis.amazonaws.com';
var count = 10;
var startNum = 1;
var sigVersion = '2';
var hashVersion = 'HmacSHA256';
var accessKeyID = 'XXXAJA664T37BDNPSXXX';
var accessKey = 'XXXoImq0sZ4J/vYRewLuNjPFXYQ809DfLmzcpXXX';
var site = 'http://site.com';
function getURLInfo(){
var queryParams = buildQueryParams();
var sig = generateSignature(queryParams);
var requestURL = 'http://' + serviceHost + '/?' + queryParams + '&Signature=' + sig;
console.log('requestURL:' + requestURL);
$.ajax({
type: 'GET',
url: requestURL,
dataType: 'xml',
crossDomain: true,
error: function(jqXHR,textStatus, errorThrown){
console.log(textStatus + "|" + errorThrown)
},
success: function(data, textStatus, jqXHR){
console.log(data);
}
});
}
function ISODateString(d){
function pad(n){return n<10 ? '0'+n : n}
return d.getUTCFullYear()+'-'
+ pad(d.getUTCMonth()+1)+'-'
+ pad(d.getUTCDate())+'T'
+ pad(d.getUTCHours())+':'
+ pad(d.getUTCMinutes())+':'
+ pad(d.getUTCSeconds())+'.000Z'
}
function getTimeStamp(){
var d = new Date();
var now = ISODateString(d);
//var hardcoded_time = "2011-10-28T16:33:03.000Z"; //USE THIS TO TEST BETWEEN SAMPLE PHP AND JS
return now;
}
function buildQueryParams(){
var params = {};
params.AWSAccessKeyId = accessKeyID;
params.Action = actionName;
params.Count = count;
params.ResponseGroup = responseGroupName;
params.SignatureMethod = hashVersion;
params.SignatureVersion = sigVersion;
params.Start = startNum;
params.Timestamp = getTimeStamp();
params.Url = site;
paramString = $.param(params);
return paramString;
}
function generateSignature(sigParams){
var sign = "GET\n" + serviceHost + "\n/\n" + sigParams;
console.log("SIGN: \n" + sign);
var sigHash = Crypto.HMAC(Crypto.SHA256, sign, accessKey, { asString: true });
console.log("HMAC:" + sigHash);
var sigBytes = Crypto.charenc.Binary.stringToBytes(sigHash);
var sig64 = Crypto.util.bytesToBase64(sigBytes);
console.log("BASE 64: " + sig64);
var sigEnc = encodeURIComponent(sig64);
console.log("ENCODED URL: " + sigEnc)
return sigEnc;
}
function awisResponse(response){
console.log(response);
}
getURLInfo();
</script>
</body>
</html>
[UPDATE]
Derp. I think I pointed out several outstanding issues here, but must admit I still wasn’t quite catching onto the problem of cross site.
The final piece of the puzzle is using a proxy so PHP can get the xml. So this isn’t a javascript only solution, but mostly it is:
http://benalman.com/projects/php-simple-proxy/
[/UPDATE]
I’d prefer you don’t give up on this for the wrong reason. You’ll remember that the X in AJAX stands for XML 🙂
So two ideas based on duskwuff’s commments.
Here’s the code:
Your more pressing problem is in the authentication, but perhaps you will be able to get more details now using the error function and it’s errorThrown argument.
My recommendation is to see if you can get the URL generated in another context and compare that to what you have to see if you’re doing it right (it’s been a few months, but I thought I remember there being an amazon tool that generated the request URL for you).