Getting started with Amazon AWS via C#

A while back I was putting together a website that, among other things, relied on being able to retrieve products from Amazon’s Product Advertising API. There isn’t a ton of documentation out there on how to do this, and it took me a long time to find information on how to do this in my language of choice, C#. Unfortunately, there isn’t anything as cool as the Zend Framework for .NET.

After several hours of trial and error and searching for information, I went from having no knowledge of Amazon’s API to having a pretty decent understanding. Along the way I also found a VERY helpful article here. It has great sample code that helped me get my project communicating with Amazon. All I had to do was add the library to my solution, steal the app.config from the sample project, and set the different security keys Amazon requires (the AWS ID, secret, and associate tag), and I was immediately able to use the library to start getting results out of Amazon’s API.

I also wrote a small wrapper method (shown below) that sheds some light on the usage of the service for some simple tasks. My project only required looking up certain information about books based on ISBN, but you could pass in the ASIN for just about any product and get similar results. Enjoy!

   1: // wrapper class for data returned from Amazon

   2:         public class ItemLookupInfo {

   3:             public ItemLookupInfo() {

   4:             }

   5:  

   6:             public string ISBN { get; set; }

   7:             public string Title { get; set; }

   8:             public string Binding { get; set; }

   9:             public string Edition { get; set; }

  10:             public string Authors { get; set; }

  11:             public string Manufacturer { get; set; }

  12:             public decimal? Price { get; set; }

  13:             public decimal? TradeInValue { get; set; }

  14:             public AWS.Image Image { get; set; }

  15:             public string AmazonLink { get; set; }

  16:         }

  17:  

  18:         // method to fill up my wrapper class using an ISBN

  19:         public static ItemLookupInfo GetProductByISBN(string isbn) {

  20:             Logger.WriteToLog("Entered method: GetProductByISBN");

  21:             

  22:             AWSECommerceServicePortTypeClient amazonClient = new AWSECommerceServicePortTypeClient();

  23:             ItemLookupRequest request = new ItemLookupRequest();

  24:             request.IdType = ItemLookupRequestIdType.ISBN;

  25:             request.IdTypeSpecified = true;

  26:             request.ItemId = new string[] { isbn };

  27:             request.SearchIndex = "Books";

  28:             request.ResponseGroup = new string[] { "OfferFull", "Images", "ItemAttributes" };

  29:             ItemLookup lookup = new ItemLookup();

  30:             lookup.Request = new ItemLookupRequest[] { request };

  31:             lookup.AWSAccessKeyId = ConfigurationManager.AppSettings["accessKeyId"];

  32:             lookup.AssociateTag = "your-tag-here";

  33:  

  34:             ItemLookupResponse response = amazonClient.ItemLookup(lookup);

  35:             Item myItem = response.Items[0].Item.FirstOrDefault(i => i.MediumImage != null);

  36:             ItemLookupInfo myInfo = null;

  37:             if (myItem != null) {

  38:                 myInfo = new ItemLookupInfo() {

  39:                     Authors = myItem.ItemAttributes.Author != null ? System.String.Join(", ", myItem.ItemAttributes.Author) : null,

  40:                     Binding = myItem.ItemAttributes.Binding,

  41:                     Edition = myItem.ItemAttributes.Edition,

  42:                     Image = myItem.SmallImage,

  43:                     ISBN = myItem.ItemAttributes.ISBN,

  44:                     Manufacturer = myItem.ItemAttributes.Manufacturer,

  45:                     Price = myItem.Offers.Offer != null ? decimal.Parse(myItem.Offers.Offer[0].OfferListing[0].Price.FormattedPrice.Replace("$", "")) : decimal.Parse(myItem.ItemAttributes.ListPrice.FormattedPrice.Replace("$", "")),

  46:                     Title = myItem.ItemAttributes.Title,

  47:                     TradeInValue = myItem.ItemAttributes.TradeInValue != null ? (decimal?)decimal.Parse(myItem.ItemAttributes.TradeInValue.FormattedPrice.Replace("$", "")) : null,

  48:                     AmazonLink = response.Items[0].Item[0].DetailPageURL

  49:                 };

  50:             }

  51:             else {

  52:                 myInfo = new ItemLookupInfo() {

  53:                     Authors = response.Items[0].Item[0].ItemAttributes.Author != null ? System.String.Join(", ", response.Items[0].Item[0].ItemAttributes.Author) : null,

  54:                     Binding = response.Items[0].Item[0].ItemAttributes.Binding,

  55:                     Edition = response.Items[0].Item[0].ItemAttributes.Edition,

  56:                     Image = null,

  57:                     ISBN = response.Items[0].Item[0].ItemAttributes.ISBN,

  58:                     Manufacturer = response.Items[0].Item[0].ItemAttributes.Manufacturer,

  59:                     Price = response.Items[0].Item[0].Offers.Offer != null ? (decimal?)decimal.Parse(response.Items[0].Item[0].Offers.Offer[0].OfferListing[0].Price.FormattedPrice.Replace("$", "")) : null,

  60:                     Title = response.Items[0].Item[0].ItemAttributes.Title,

  61:                     TradeInValue = response.Items[0].Item[0].ItemAttributes.TradeInValue != null ? (decimal?)decimal.Parse(response.Items[0].Item[0].ItemAttributes.TradeInValue.FormattedPrice.Replace("$", "")) : null,

  62:                     AmazonLink = response.Items[0].Item[0].DetailPageURL

  63:                 };

  64:             }

  65:             Logger.WriteToLog("Exiting method: GetProductByISBN");

  66:             return myInfo;

  67:         }

Credits: http://www.falconwebtech.com/post/2010/06/14/Using-WCF-and-SOAP-to-Send-Amazon-Product-Advertising-API-Signed-Requests.aspx

Leave a Reply