Accessing Amazon’s Product Advertising API with Python
I’m working on a little hacky toy for our upcoming hack day at work, and one of the pieces requires that I get some info from the Amazon “REST” API. It’s not REST in the least, but that’s not what I’m here to talk about. I’m here to talk about creating a signed request which works with the “Amazon Signature 2” format needed for the API. There’s a Perl library to do everything with the API, which is great if that’s what you’re looking for, but it wasn’t. I just wanted to sign a simple static request to get information about books for which I had the ISBN. No Python library exists, but fortunately if you’re using Python 2.5+ you’ve got everything you need with very little code. I’m including it here to save you the effort of pulling together the pieces yourself.
# Create a signed request to use Amazon's API import base64 import hmac import urllib, urlparse import time</code> from hashlib import sha256 as sha256 AWS_ACCESS_KEY_ID = 'XXX' AWS_SECRET_ACCESS_KEY = 'YYY' hmac = hmac.new(AWS_SECRET_ACCESS_KEY, digestmod=sha256) def getSignedUrl(params): action = 'GET' server = "webservices.amazon.com" path = "/onca/xml" params['Version'] = '2009-11-02' params['AWSAccessKeyId'] = AWS_ACCESS_KEY_ID params['Service'] = 'AWSECommerceService' params['Timestamp'] = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) # Now sort by keys and make the param string key_values = [(urllib.quote(k), urllib.quote(v)) for k,v in params.items()] key_values.sort() # Combine key value pairs into a string. paramstring = '&'.join(['%s=%s' % (k, v) for k, v in key_values]) urlstring = "http://" + server + path + "?" + \ ('&'.join(['%s=%s' % (k, v) for k, v in key_values])) # Add the method and path (always the same, how RESTy!) and get it ready to sign hmac.update(action + "\n" + server + "\n" + path + "\n" + paramstring) # Sign it up and make the url string urlstring = urlstring + "&Signature="+\ urllib.quote(base64.encodestring(hmac.digest()).strip()) return urlstring if __name__ == "__main__": params = {'ResponseGroup':'Small,BrowseNodes,Reviews,EditorialReview,AlternateVersions', 'AssociateTag':'xxx-20', 'Operation':'ItemLookup', 'SearchIndex':'Books', 'IdType':'ISBN', 'ItemId':'9780385086950'} url = getSignedUrl(params) print url
UNCATEGORIZED
amazon authentication aws python