Accessing Amazon’s Product Advertising API with Python 5

Share on Google+Tweet about this on TwitterShare on LinkedInShare on FacebookEmail this to someonePrint this page

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

from hashlib import sha256 as sha256

hmac =, digestmod=sha256)

def getSignedUrl(params):
    action = 'GET'
    server = ""
    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()]

    # 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="+\

    return urlstring

if __name__ == "__main__":
    params = {'ResponseGroup':'Small,BrowseNodes,Reviews,EditorialReview,AlternateVersions',
    url = getSignedUrl(params)
    print url
  • Angke Chen

    Thanks a bunch! this really worked for me! Great job

  • Yoav

    If what you are looking for is a simple, object oriented access to Amazon products (lookup and search), try python-amazon-simple-product-api:

  • Michelle Chen

    Thank you so much for sharing your work. It was very helpful to find an example that doesn’t require boto. What’s strange is that the urls sometimes work and sometimes don’t (and I got the error of SignatureDoesNotMatch). Any idea why? Again, thanks, Michelle

  • synedra

    If the same URL works sometime and doesn’t work sometimes you’ll want to dump out the signatures/headers/etc to see if there’s differences. If, on the other hand, the URL consistently doesn’t work for specific products there’s probably a special character in the signed URL, look for issues with characters there. The getSignedUrl thing may not be gracefully handling specific characters… try to use binary testing to figure out which things are causing the trouble.

  • Kishor Pawar

    Is the same example works for ItemSearch?
    I am not getting it worked. I am using python’s requests api, so I am skipping the quote step.

    here are the debug statements


    Signature irS8s6139PjAHRqRrlfn5WgRHg0TIMxjd++8kHWJKZ4=

    request {‘AWSAccessKeyId': ‘AKIAJ6BIT4FKXXKIFNNQ’, ‘ItemPage': 1, ‘SearchIndex': ‘Electronics’, ‘Service': ‘AWSECommerceService’, ‘Title': u’KP’, ‘Timestamp': ‘2015-03-28T12:03:23Z’, ‘Version': ‘2011-08-01′, ‘Signature': ‘irS8s6139PjAHRqRrlfn5WgRHg0TIMxjd++8kHWJKZ4=’, ‘Keywords': ‘smartphones’, ‘Operation': ‘ItemSearch’, ‘AssociateTag': ‘youtellmeco0a-21′, ‘Condition': ‘New’, ‘ResponseGroup': ‘ItemAttributes,OfferSummary,Images’}