Home > Programming > Amazon product information via Amazon Web Services

Amazon product information via Amazon Web Services

I recently suggested to a client that a good way to increase their sales on Amazon would be to show the Amazon price next to the “Buy from Amazon” link. I figured that there would be a simple, straightforward solution to this. I thought to myself “Amazon will have an API for that; they’ll have useful, up-to-date documentation …”

Nothing in life is as easy as it should be

Nothing in life is as easy as it should be

Well, I’m sure all of their documentation was useful, once, but not much was up-to-date. I ended up spending over an hour reading through various instructions, tutorials and references, before I was able to work out how to produce a simple REST request.

I’m hoping that by sharing my solution with you, you won’t have to go through all the brain-ache I had to endure. Feel free to leave a comment if it helped, or if any of the information didn’t work for you. This is my first how-to post so I welcome any feedback.

The AWS Signed Request Helper

I’m assuming you have an Amazon Web Services account. If not, click the link to get one.

The AWS Signed Request Helper is your friend

After spending what felt like days reading through everything I could find on the subject, everything clicked when I discovered Amazon Web Services’ Signed Request Helper. This useful little tool lets you specify the basic request parameters, and will walk you through the steps required to turn that into a fully functioning signed REST URL. Note that all requests are time-stamped, so you won’t be able to use a URL you made with the Signed Request Helper for longer than about 15 minutes. Here’s a sample session with the Signed Request Helper:

Unsigned URL:

http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService
&Version=2009-03-31
&Operation=ItemSearch
&SearchIndex=Books
&Keywords=harry+potter

Name-Value Pairs:

Service=AWSECommerceService
Version=2009-03-31
Operation=ItemSearch
SearchIndex=Books
Keywords=harry+potter
Timestamp=2010-02-09T22:27:04.000Z
AWSAccessKeyId=HIDDEN

Sorted Pairs:

AWSAccessKeyId=HIDDEN
Keywords=harry%20potter
Operation=ItemSearch
SearchIndex=Books
Service=AWSECommerceService
Timestamp=2010-02-09T22%3A27%3A04.000Z
Version=2009-03-31

String-To-Sign:

GET
ecs.amazonaws.com
/onca/xml
AWSAccessKeyId=HIDDEN&Keywords=harry%20potter&Operation=ItemSearch&SearchIndex=Books&Service=AWSECommerceService&Timestamp=2010-02-09T22%3A27%3A04.000Z&Version=2009-03-31

Signed URL:

http://ecs.amazonaws.com/onca/xml?AWSAccessKeyId=HIDDEN&Keywords=harry%20potter&Operation=ItemSearch&SearchIndex=Books&Service=AWSECommerceService&Timestamp=2010-02-09T22%3A27%3A04.000Z&Version=2009-03-31&Signature=7hZh9Dzo2WPh8JhEoAMs8mm2I6OgMaxL8pZifFcaVZY%3D

Huh?

If you’re unsure about what that all meant, here’s an explanation of each step:

Unsigned URL

The unsigned URL is what you’d use if Amazon had a simple REST API that didn’t require any authentication or signing. We’ve supplied a number of parameters. If you want to know more about these parameters, have a look at Amazon’s documentation on the ItemSearch Operation and the ItemLookup Operation.

Name-Value Pairs

This splits the query string into its constituent parts. You’ll notice that two new parameters have been added: Timestamp and AWSAccessKeyId. Timestamp is a current timestamp, specially-formatted. You’ll be able to find your AWSAccessKeyId when you log into Amazon Web Services.

Sorted Pairs

Now the Name-Value pairs have been sorted in byte order and URL encoded (e.g. + has been changed to %20).

String-To-Sign

This is the HTTP request representation of the original URL. Note the newlines – these must be included in the string for the next stage to work properly.

Signed URL

This is where the voodoo happens. The String-To-Sign and your AWS secret key are used to create an HMAC-SHA signature. For some more in-depth information on this procedure have a look at Amazon’s description.

HMAC Authentication Process

A sample implementation

Now let’s code it up. I’m going to be using PHP; implementation in other languages is left as an exercise to the reader.

This example will show how to get information on an item from Amazon.co.uk using the AWS API. We will be creating a function that will produce a signed REST URL from an Amazon product code. I’ve borrowed heavily from Amazon’s Introduction to AWS for PHP Developers, which is a useful resource, but with some crucial bits that were out of date.

The first thing to do is place your AWS credentials in a safe place. I usually use a directory called ‘safeinc’ in the parent directory to the web root. Make a file in that directory called aws.conf, as follows:

; AWS credentials
; Access Key Id
access_key = "Your access key"
; Secret Access Key
secret_key = "Your secret key"

Now it’s time to make a PHP file for our function. This function is going to take an ASIN (Amazon Standard Identification Number) and return a URL.

function makeUrl($itemId) {
  //load and parse credentials
  $creds = parse_ini_file($_SERVER["DOCUMENT_ROOT"] . '/../safeinc/aws.conf');
  //set up the base AWS URL. This can be changed to .com if necessary.
  $awsUrl = 'ecs.amazonaws.co.uk';
  $params = array(
          //AWS version date
          'Version' => '2009-03-31',
          //Look up an item by ASIN
          'Operation' => 'ItemLookup',
          //The ASIN
          'ItemId' => strval($itemId),
          //The AWS service being used
          'Service' => 'AWSECommerceService',
          //The timestamp must use this format
          'Timestamp' => gmdate('Y-m-d\TH:i:s.000\Z'),
          //Your Access Key
          'AWSAccessKeyId' => $creds['access_key'],
          //To make sure the response includes the Amazon price use this
          'ResponseGroup' => 'Offers',
          //Get Amazon prices, not third party merchants.
          'Merchant' => 'Amazon'
          );
  //Sort the params array by key
  uksort($params, 'strnatcmp');

We’ve reached the Sorted Pairs stage now, with hopefully no massive surprises. The next step is to produce the String-To-Sign.

  $qstr = '';
  foreach($params as $key => $param) {
    //each key-param combo will produce '&key=param'
    $qstr .= "&{$key}=".rawurlencode($param);
  }
  //Need to remove the initial '&'
  $qstr = substr($qstr, 1);

  //Note the newlines, and lack of '?' before query string
  $stringToSign = "GET\n"
    . $awsUrl . "\n"
    . "/onca/xml\n"
    . $qstr;

The final step is to produce the signature to make the signed URL. We’re going to be using PHP’s hash_hmac function for this.

  //hash_hmac's function signature:
  //string hash_hmac ( string $algo , string $data , string $key [, bool $raw_output = false ] )
  //The last parameter will be the signature, which must be base64-encoded.
  $params['Signature'] = base64_encode(hash_hmac('sha256',
                         $stringToSign,
                         $creds['secret_key'],
                         true)
                   );

  //Construct the URL
  $req = "http://$awsUrl/onca/xml?" . http_build_query($params);
  return $req;
}

Well, there you have it

Hopefully that solves your Amazon API woes. It turns out that it’s not really a difficult process, but you have to jump through a lot of hoops to get there. It’s a shame Amazon’s documentation isn’t more up-to-date, but then again, maybe that’s what blog posts like this one are there for. If this helped, or if you’ve spotted any mistakes or inaccuracies, let me know in the comments.

Categories: Programming Tags: ,
  1. June 19th, 2015 at 19:36 | #1

    There is no need to purchase bottled water, which is expensive; you can store your own fresh drinking water for purposes of drinking in vertical storage tanks. Pipes and fittings are often made of the same base material such as copper, steel, polyvinyl chloride (PVC), and chlorinated polyvinyl chloride (CPVC). Today there are various insulation materials that are used directly over the exterior of the tire before the finish is applied.

  2. June 20th, 2015 at 14:42 | #2

    There is no need to purchase bottled water, which is expensive; you can store your own fresh drinking water for purposes of drinking in vertical storage tanks. Pipes and fittings are often made of the same base material such as copper, steel, polyvinyl chloride (PVC), and chlorinated polyvinyl chloride (CPVC). Today there are various insulation materials that are used directly over the exterior of the tire before the finish is applied.

  3. June 20th, 2015 at 17:54 | #3

    As a homeowner, you will have maintenance and other related costs to keep your drain system running efficiently. Pipes and fittings are often made of the same base material such as copper, steel, polyvinyl chloride (PVC), and chlorinated polyvinyl chloride (CPVC). The most popular types and styles of these include the following:.

  4. June 20th, 2015 at 19:21 | #4

    After looking at a handful of the articles on your site, I really like your way of writing a blog. I saved it to my bookmark site list and will be checking back soon. Please visit my website too and tell me what you think.

  5. June 22nd, 2015 at 17:25 | #5

    Hi there very cool web site!! Man .. Beautiful .. Wonderful .. I will bookmark your web site and take the feeds also? I’m satisfied to find numerous useful information here in the publish, we want work out extra techniques on this regard, thanks for sharing. . . . . .

  6. June 24th, 2015 at 19:10 | #6

    You can certainly see your skills within the article you write.

    The arena hopes for even more passionate writers like you who are not afraid to mention how they believe.

    At all times follow your heart.

  7. July 6th, 2015 at 06:25 | #7

    Many consumers are also under the impression that supplements are inherently ‘natural’ and safe, but the lack of regulation surrounding the testing, manufacturing, and labeling of supplements can lead to potentially harmful products. Sometimes eating of bad calories result in making the dieter overweight which is really a matter of concern because it directly targets the working efficiency of its user. What’s more, the global supplement market is worth close to $70 billion according to research from Euromonitor.

  8. July 7th, 2015 at 18:13 | #8

    “The old saying used to be diagnosed and adios, and now that spectrum has completely changed. In fact the height of a teenager increases dramatically. What’s more, the global supplement market is worth close to $70 billion according to research from Euromonitor.

  9. July 9th, 2015 at 03:55 | #9

    Hello, myy name is Gonzalo. I am from Venezuela. I`ve bought some medicines on this online pharma.

    It is really good guys, nnot expensive and fast delivery! Advice to everybody!!! :)

    My weblog; Buy cheap Zyrtec online; [www.rapidpharma.biz](http://www.rapidpharma.biz “www.rapidpharma.biz”),

  10. July 11th, 2015 at 02:52 | #10

    What’s up colleagues, pleasant post and nice urging commented at this place, I am in fact enjoying by these.

  11. July 17th, 2015 at 15:17 | #11

    Simply desire to say your article is as surprising. The clearness in your post is simply cool and i could assume you are an expert on this subject. Well with your permission allow me to grab your feed to keep up to date with forthcoming post. Thanks a million and please carry on the rewarding work.

  12. July 19th, 2015 at 10:20 | #12

    The website will also serve as a portal for all things convention related, from a countdown clock to information on logistics, credentials, media walkthroughs and housing. This shows you how user friendly these templates are. You can manipulate movie clips to animate your buttons to make them perform different functions.

  13. July 20th, 2015 at 03:14 | #13

    Another 10% do it kind of well although not at a professional stage. This shows you how user friendly these templates are.

    You can manipulate movie clips to animate your buttons to make them perform different functions.

  14. July 21st, 2015 at 01:23 | #14

    In just a few days, you can have your shop up and running with Zen Cart. That means you literally can make money 24 hours a day. You will also find 30 free tools, and software programs to help you build a successfull Internet Business.

  15. July 27th, 2015 at 00:39 | #15

    It won’t necessarily cost less to get your site this way, but it does provide a way to spread the cost over time. No software to buy, no code to learn, all you need is to be a little Internet savvy and know how to click your mouse. The website templates are well-equipped with HTML coding which could result into better looking websites in minor budget also.

  16. July 30th, 2015 at 17:21 | #16

    This is especially useful if the person you’re inquiring about has a less than common name. Small claims court cases are much cheaper than superior court cases for both the plaintiff (the person doing the suing) and the defendant (the person being sued) because the parties are not allowed to have any attorneys represent them and other rules that simplify the lawsuit process, making the whole thing much cheaper, faster, and easier. The website templates are well-equipped with HTML coding which could result into better looking websites in minor budget also.

  17. August 3rd, 2015 at 06:37 | #17

    No excuses if you can’t get iin the gym, just do bodyweight exercises, stretching, and plyometric exercises. In plain English, this means that you shock tthe muscle and then immediately rebound. The exolosive power required for a high vertical leap is dependent on strength and speed whifh can be influenced bby training. Vertical Jump Program, acquire is the only vertical leap training progream that has been proven to maximize your vertical plummet.

Comment pages
  1. July 22nd, 2014 at 03:52 | #1

Comments parsed as Markdown.