--- ecs.py.orig	2009-08-19 10:04:34.000000000 +0900
+++ ecs.py	2009-08-19 11:13:47.000000000 +0900
@@ -6,19 +6,25 @@
 The Amazon's web APIs specication is described here:
 http://www.amazon.com/webservices
 
-You need a Amazon-provided license key to use these services.
+You need a Amazon-provided license key and secret key to use these services.
 Follow the link above to get one.  These functions will look in
 several places (in this order) for the license key:
 	- the "license_key" argument of each function
 	- the module-level LICENSE_KEY variable (call setLicense once to set it)
 	- an environment variable called AMAZON_LICENSE_KEY
 	- foo would return the python object, XMLfoo returns the DOM object
+And the secret key:
+	- the "secret_key" argument of each function
+	- the module-level SECRET_KEY variable (call setSecretKey once to set it)
+	- an environment variable called AWS_SECRET_KEY
+	- foo would return the python object, XMLfoo returns the DOM object
 
 License: Python Software Foundation License
 """
 
 
 import os, urllib, string, inspect
+import datetime, hmac, hashlib, base64
 from xml.dom import minidom
 
 __author__ = "Kun Xi < kunxi@kunxi.org >"
@@ -29,6 +35,7 @@
 """Package-wide variables:
 """
 LICENSE_KEY = None;
+SECRET_KEY = None
 HTTP_PROXY = None
 LOCALE = "us"
 VERSION = "2007-02-22"
@@ -49,12 +56,19 @@
 	(lambda key: os.environ.get('AWS_LICENSE_KEY', None))
    )
 
+__secretKeys = (
+	(lambda key: key),
+	(lambda key: SECRET_KEY), 
+	(lambda key: os.environ.get('AWS_SECRET_KEY', None))
+   )
+
 
 class AWSException(Exception) : 
 	'''Base class for all AWS exceptions'''
 	pass
 
 class NoLicenseKey(AWSException) : pass
+class NoSecretKey(AWSException) : pass
 class BadLocale(AWSException) : pass
 # Runtime exception
 class ExactParameterRequirement(AWSException): pass
@@ -76,11 +90,13 @@
 class MissingOperationParameter(AWSException): pass
 class MissingParameterCombination(AWSException): pass
 class MissingParameters(AWSException): pass
+class MissingParameter(AWSException): pass
 class MissingParameterValueCombination(AWSException): pass
 class MissingServiceParameter(AWSException): pass
 class ParameterOutOfRange(AWSException): pass
 class ParameterRepeatedInRequest(AWSException): pass
 class RestrictedParameterValueCombination(AWSException): pass
+class SignatureDoesNotMatch(AWSException): pass
 class XSLTTransformationError(AWSException): pass
 
 class Bag : 
@@ -133,6 +149,32 @@
 	return LICENSE_KEY
 	
 
+def setSecretKey(secret_key=None):
+	"""set secret key
+
+	secret key can come from any number of locations;
+	see module docs for search order.
+	if no secret key is specified, NoSecretKey is raised."""
+
+	global SECRET_KEY
+	for get in __secretKeys:
+		rc = get(secret_key)
+		if rc: 
+			SECRET_KEY = rc;
+			return
+	raise NoSecretKey, ("Please get the secret key from http://www.amazon.com/webservices")
+
+
+def getSecretKey():
+	"""get secret key
+
+	if no secret key is specified, NoSecretKey is raised."""
+
+	if not SECRET_KEY:
+		raise NoSecretKey, ("Please get the secret key from http://www.amazon.com/webservices")
+	return SECRET_KEY
+	
+
 def getVersion():
 	"""get version"""
 	return VERSION
@@ -143,8 +185,17 @@
 	
 	all key, value pairs in argv are quoted."""
 
-	url = "http://" + __supportedLocales[getLocale()] + "/onca/xml?Service=AWSECommerceService&"
-	return url + '&'.join(['%s=%s' % (k,urllib.quote(str(v))) for (k,v) in argv.items() if v]) 
+	req = argv.copy()
+	req['Service'] = 'AWSECommerceService'
+	req['Timestamp'] = datetime.datetime.utcnow().replace(microsecond=0).isoformat() + 'Z'
+
+	host = __supportedLocales[getLocale()]
+	path = '/onca/xml'
+	request = ['%s=%s' % (k, urllib.quote(str(req[k]))) for k in sorted(req.keys()) if req[k]]
+	sign = urllib.quote(base64.standard_b64encode(hmac.new(
+		getSecretKey(), '\n'.join(['GET', host, path, '&'.join(request)]), hashlib.sha256).digest()))
+
+	return 'http://' + host + path + '?' + '&'.join(request + ['Signature=%s' % sign])
 
 
 def buildException(els):
@@ -153,7 +204,9 @@
 	Only the first exception is raised."""
 
 	error = els[0]
-	class_name = error.childNodes[0].firstChild.data[4:]
+	class_name = error.childNodes[0].firstChild.data
+	if class_name[:4] == 'AWS.':
+		class_name = class_name[4:]
 	msg = error.childNodes[1].firstChild.data 
 
 	e = globals()[ class_name ](msg)
@@ -700,6 +753,7 @@
 
 if __name__ == "__main__" :
 	setLicenseKey("YOUR-LICENSE-HERE");
+	setSecretKey("YOUR-SECRET-HERE")
 	sll = SellerListingLookup("A3ENSIQ3ZA4FFN", "1106K206331")
 	print dir(sll[0])
 	
