Skip to main content

Python: Amazon Product Advertising API

If you use Amazon Associates Web Service, then you may have recently received an email explaining that they are renaming the service to the "Product Advertising API". They're also now requiring you to sign your requests. This was a problem for me. Previously, I was making requests without using an Amazon client library. It's pretty simple to use with just urllib2. However, the logic to sign requests is non-trivial.

I couldn't find any modern Python bindings for the Product Advertising API. However, I knew that boto was a popular client library for some of Amazon's other services like S3 and EC2. I figured the signing logic was probably the same. Even though boto doesn't support the Product Advertising API, I managed to get it to work.

Here's a simple proof of concept. You'll need your own access key, etc.:
import time
import urllib

from boto.connection import AWSQueryConnection

AWS_ACCESS_KEY_ID = '...'
AWS_ASSOCIATE_TAG = '...'
AWS_SECRET_ACCESS_KEY = '...'


search_index = 'All'
keywords = 'pink'
aws_conn = AWSQueryConnection(
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY, is_secure=False,
host='ecs.amazonaws.com')
aws_conn.SignatureVersion = '2'
params = dict(
Service='AWSECommerceService',
Version='2008-08-19',
SignatureVersion=aws_conn.SignatureVersion,
AWSAccessKeyId=AWS_ACCESS_KEY_ID,
AssociateTag=AWS_ASSOCIATE_TAG,
Operation='ItemSearch',
SearchIndex=search_index,
Keywords=keywords,
ResponseGroup='ItemAttributes,Images',
Order='salesrank',
Timestamp=time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime()))
verb = 'GET'
path = '/onca/xml'
qs, signature = aws_conn.get_signature(params, verb, path)
qs = path + '?' + qs + '&Signature=' + urllib.quote(signature)
print "verb:", verb, "qs:", qs
response = aws_conn._mexe(verb, qs, None, headers={})
print response.read()

Comments

andy said…
This comment has been removed by a blog administrator.
THE HIRE said…
Nice code. Very helpful. Thanks. One question...What is the Associate Tag? Is it the Tracking ID given from Amazon Associates?
jjinux said…
When you make your way through the http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/, you'll end up becoming an "associate", and they'll give it to you. Mine is "jji-20", so it's not a secret or anything.
Matt Cain said…
That was easy! Thanks man!
jjinux said…
Awesome! Glad to hear someone benefited :)
sriram said…
Thank you. This did it for me. I linked to your post and copied your code (with minor mods) into stackoverflow
jjinux said…
Cool. Glad it worked for you :)
Unknown said…
Thanks for the snippet, this is helpful. By the by, it appears that someone else has claimed copyright on this code, "all rights reserved", you might want to sort them out:
link
jjinux said…
Thanks, Ian. There's code looks different enough that I'm not going to worry about it.
Pixelismo said…
Hi every one.. sorry for my english
i'm a little confused, i'm not crear in the way to get the Associate Tag and how to complete the fields Timestamp and Signature.

there's an example running over there?

Thanks a lot

Diego
jjinux said…
> i'm not crear in the way to get the Associate Tag

You'll have to read Amazon's documentation. It's not that big a deal.

> and how to complete the fields Timestamp and Signature.

The code above does that for you.

> there's an example running over there?

The last I checked, we had this code running in production.
Anonymous said…
thanks, but i am getting the following error:
boto.exception.TooManyAuthHandlerReadyToAuthenticate: 6 AuthHandlers ready to authenticate, only 1 expected:

can you help please
jjinux said…
Sorry, I haven't used that library for several years. I hate to say it, but you're on your own. Best of luck!
Yoav Aviram said…
If what you are looking for is a simple, object oriented access to Amazon products (lookup and search), try python-amazon-simple-product-api:

http://github.com/yoavaviram/python-amazon-simple-product-api

Its the new kid on the block!
jjinux said…
Yoav, cool, thanks!

Popular posts from this blog

Drawing Sierpinski's Triangle in Minecraft Using Python

In his keynote at PyCon, Eben Upton, the Executive Director of the Rasberry Pi Foundation, mentioned that not only has Minecraft been ported to the Rasberry Pi, but you can even control it with Python . Since four of my kids are avid Minecraft fans, I figured this might be a good time to teach them to program using Python. So I started yesterday with the goal of programming something cool for Minecraft and then showing it off at the San Francisco Python Meetup in the evening. The first problem that I faced was that I didn't have a Rasberry Pi. You can't hack Minecraft by just installing the Minecraft client. Speaking of which, I didn't have the Minecraft client installed either ;) My kids always play it on their Nexus 7s. I found an open source Minecraft server called Bukkit that "provides the means to extend the popular Minecraft multiplayer server." Then I found a plugin called RaspberryJuice that implements a subset of the Minecraft Pi modding API for B

Ubuntu 20.04 on a 2015 15" MacBook Pro

I decided to give Ubuntu 20.04 a try on my 2015 15" MacBook Pro. I didn't actually install it; I just live booted from a USB thumb drive which was enough to try out everything I wanted. In summary, it's not perfect, and issues with my camera would prevent me from switching, but given the right hardware, I think it's a really viable option. The first thing I wanted to try was what would happen if I plugged in a non-HiDPI screen given that my laptop has a HiDPI screen. Without sub-pixel scaling, whatever scale rate I picked for one screen would apply to the other. However, once I turned on sub-pixel scaling, I was able to pick different scale rates for the internal and external displays. That looked ok. I tried plugging in and unplugging multiple times, and it didn't crash. I doubt it'd work with my Thunderbolt display at work, but it worked fine for my HDMI displays at home. I even plugged it into my TV, and it stuck to the 100% scaling I picked for the othe

Creating Windows 10 Boot Media for a Lenovo Thinkpad T410 Using Only a Mac and a Linux Machine

TL;DR: Giovanni and I struggled trying to get Windows 10 installed on the Lenovo Thinkpad T410. We struggled a lot trying to create the installation media because we only had a Mac and a Linux machine to work with. Everytime we tried to boot the USB thumb drive, it just showed us a blinking cursor. At the end, we finally realized that Windows 10 wasn't supported on this laptop :-/ I've heard that it took Thomas Edison 100 tries to figure out the right material to use as a lightbulb filament. Well, I'm no Thomas Edison, but I thought it might be noteworthy to document our attempts at getting it to boot off a USB thumb drive: Download the ISO. Attempt 1: Use Etcher. Etcher says it doesn't work for Windows. Attempt 2: Use Boot Camp Assistant. It doesn't have that feature anymore. Attempt 3: Use Disk Utility on a Mac. Erase a USB thumb drive: Format: ExFAT Scheme: GUID Partition Map Mount the ISO. Copy everything from