Every request to the Amazon API needs to be signed by using HMAC-SHA signatures. The request needs a signature and a timestamp. “A signature is created by using the request type, domain, the URI, and a sorted string of every parameter in the request (except the Signature parameter itself) with the following format <parameter>=<value>&. Once properly formatted, you create a base64-encoded HMAC_SHA256 signature using your AWS secret key.” Amazon API [1]
The timestamp needs to be in YYYY-MM-DDThh:mm:ssZ Format.
Examples can be found in the Amazon API Docs [2]. For the encryption I used the as3crypto lib [3] and a solution of Brendon Wilson [4].
[1] http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html?HMACAuth_ItemsRequired.html
[2] http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html?rest-signature.html
[3] http://code.google.com/p/as3crypto/
[4] http://www.brendonwilson.com/
the final function:
public function aws_signed_request(p : Array):void{
var public_key : String = "Your Access Key ID";
var private_key : String = "Your Secret Access Key:";
var requestBytes:ByteArray = new ByteArray();
var hmacc:HMAC = new HMAC(new SHA256());
var keyBytes:ByteArray = new ByteArray();
var hmacBytes:ByteArray;
var encoder:Base64Encoder = new Base64Encoder();
var formatter:DateFormatter = new DateFormatter();
var now:Date = new Date();
var string_to_sign : String = "";
var timestamp : String;
var method : String = "GET";
var host : String = "ecs.amazonaws.com";
var uri : String = "/onca/xml";
var pattern:RegExp = /%7E/gi;
var signature : String;
var params : Array = new Array();
// Do not change the order of following commands
params["AWSAccessKeyId"] = public_key;
for(var i:int = 0; i
params[p[i]] = p[(i+1)];
}
// creating Timestamp
formatter.formatString = "YYYY-MM-DDTHH:NN:SSZ";
now.setTime(now.getTime() + (now.getTimezoneOffset() * 60 * 1000));
timestamp = formatter.format(now);
params["Version"] = "2009-03-31";
params["Timestamp"] = timestamp;
params["Service"] = "AWSECommerceService";
params.sort();
// cannonicalized_query
var can_query : Array = new Array();
for( var key in params ) {
key = escape(key.replace(pattern, "~"));
params[key] = escape(params[key].replace(pattern, "~"));
can_query.push(key + "=" + params[key]);
}
var fin_query : String = "";
// implode the query
for(var i:int = 0; i
fin_query += can_query[i];
if(i != (can_query.length-1))
fin_query += "&";
}
string_to_sign = method + " " + host + " " + uri + "\n" + fin_query;
requestBytes.writeUTFBytes(string_to_sign);
keyBytes.writeUTFBytes(private_key);
hmacBytes = hmacc.compute(keyBytes, requestBytes);
encoder.encodeBytes(hmacBytes);
encoder.toString();
signature = encoder.toString() + encoder.toString();
signature = escape(signature.replace(pattern, "~"));
var request : String = "http://" + host+uri+"?"+fin_query+"&Signature="+signature;
somaLoader.add(request, null, SomaLoader.TYPE_XML);
somaLoader.addEventListener(SomaLoaderEvent.COMPLETE, responseReceived);
somaLoader.start();
}