Product Advertising API Signed Requests Sample Code - Java REST/QUERY

Sample Code & Libraries>Product Advertising API>Product Advertising API Signed Requests Sample Code Java REST/QUERY
Community Contributed Software

  • Amazon Web Services provides links to these packages as a convenience for our customers, but software not authored by an "@AWS" account has not been reviewed or screened by AWS.
  • Please review this software to ensure it meets your needs before using it.

Shows how to send a signed REST/Query request to the Product Advertising API (previously Amazon Associates Web Service) using Java.


Submitted By: ProductAdvtAPI@AMZN
AWS Products Used: Amazon Associates Web Service
Language(s): Java
License: Apache License 2.0
Created On: May 7, 2009 5:40 PM GMT
Last Updated: July 8, 2009 2:06 PM GMT

This sample shows how to send a signed ItemLookup request to the Product Advertising API (previously Amazon Associates Web Service) using REST/Query protocol in Java. It displays the title of the ASIN provided.

To get started this this sample, extract the contents of the ZIP file and follow the instructions in README.html.

Please post questions and feedback at the Product Advertising API Community Forum.


Some notes
The Documentation that covers the technology used in this sample is here: Use Commons Codec Version 1.3 (commons-codec-1.3.jar) and the requests are signed properly. The 1.4 Version of the Commons Codec does not sign the requests properly with the code as is.
robertkielty on May 5, 2010 12:11 PM GMT
Better Than Expected
Based on the low ratings, I expected to spend hours getting this to work. The only mod I needed to make was the one ifuice mentioned which is to strip out the unnecessary chars at the end of the request. Thanks ifuice!
M. Frisch on March 13, 2010 11:36 AM GMT
yet another bug
First of all, to get here I had to enable cookies GLOBALLY. Otherwise several parts of the amazon website are unreachable. That's just unacceptable. I would have normally stopped here. Second, I searched for Java APIs for the advertisement API. There are 2 Java examples on this site for this purpose. But both are rated badly (very poor = 1 star, and poor = 2 stars). So I decided to start with the less poor one (Java REST/QUERY). The good thing is that this example is well documented and easy to setup and execute. The bad thing is that it creates wrong HTTP requests and therefore fails deterministic with an exception. The reason for this is the "%0D%0A" prefix appended at the signature parameter. This can be fixed by replacing the following line in "http://" + this.endpoint + REQUEST_URI + "?" + canonicalQS + "&Signature=" + sig; with "http://" + this.endpoint + REQUEST_URI + "?" + canonicalQS + "&Signature=" + sig.replaceAll("%0D%0A", ""); Since the signature is only a cryptic sequence of characters, the problem is not so easy to see for someone that experiments with this API for the first time. I spent several hours in finding this bug! Argh! I suggest you, when putting an example program for first time users on the website, test it beforehand! Thanks.
ifuice on February 2, 2010 4:20 PM GMT
Works perfectly
It works perfectly, just follow the instructions and get the required library and it will work correctly. I spent hours searching for this.
Oscar Bolanos on February 1, 2010 7:25 AM GMT
Don't ignore this / it works
Because of the low ratings I ignored the code thinking it would not work. However, I was able to get the REST example running, with signing, very easily. Make sure you have all the necessary supporting libraries (Ant & DOM things). Obviously it will take some modification to do more than search for Harry Potter, but it's a better place to start than I was expecting.
dannodaws on January 26, 2010 5:42 AM GMT
Totally Sucks
The code is great except for one huge problem - it is not obvious that you have to supply your AssociateTag in the requests. If you don't then the DetailPageUrl links that you get back don't contain your associate tag and therefore DON'T TRACK on your account. I found out the hard way after losing a lot of money on un-tracked orders....
PolishBen on November 22, 2009 7:46 PM GMT
Incorrect logic double-encodes cart HMAC
If you request a shopping cart, it returns an id and HMAC which need to be passed in subsequent requests -- when used in a REST query, the HMAC must be URL encoded in order to create a valid URI (indeed, the API also returns an encoded version of the HMAC for this purpose. This is a different HMAC than the one needed to create a signature. However, the sample code provided here calls a "canonicalize" method, which arbitrarily URL Encodes all names and values. while (iter.hasNext()) { Map.Entry kvpair =; buffer.append(percentEncodeRfc3986(kvpair.getKey())); buffer.append("="); buffer.append(percentEncodeRfc3986(kvpair.getValue())); if (iter.hasNext()) { buffer.append("&"); } } The call to percentEncodedRfc3986 is benign if there are no invalid characters (so it usually works in most calls), but if an already encoded HMAC is passed, for example "snZtZM99lw2h9RyzAezvpNjx0lk%3D" it will be re-encoded, with the result "snZtZM99lw2h9RyzAezvpNjx0lk%253D". There is no need to call the percentEncodeRfc3986 method anyway, since in the sample code, the input is a URL already, and thus can be presumed to be valid. Oh, and can't anyone add documentation to libraries like this? Yes, I know this code is delivered "as is", and we're responsible for making sure it works, but shouldn't Amazon be reviewing and testing this stuff? Sorry, I speak in anger. I'll get over it -- my goal is just to help make sure someone else doesn't waste a day on this as I have.
"sublime1" on September 17, 2009 3:52 PM GMT
Base64 lineLength
FYI, in order to get this sample code to work, I had to make the following change: From: Base64 encoder = new Base64(); To: Base64 encoder = new Base64(0); The '0' is the lineLength, a value of zero means no "chunking". If chunking is enable, my library was appending CRLF, which caused the following error: SignatureDoesNotMatch
pmoosman on August 18, 2009 6:34 AM GMT
We are temporarily not accepting new comments.
©2014, Amazon Web Services, Inc. or its affiliates. All rights reserved.