How to Integrate Amazon Advertising API - Linq to Amazon

How to Integrate Amazon Advertising API - Linq to Amazon

May 12, 2009 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

Earlier, I did a blog post on integrating Amazon Associates API into Sitefinity. Although that widget works fine, what is you want to the product list look a certain way? What if you want to show a specific product based on an ID number, which Amazon calls an ASIN? If you answered yes to any of these questions, or if you want to show lists based on other logic here are the steps:

 

  1. Obtain an Amazon Access ID
  2. Find the ASIN number of the product that you'd like to use. This can be found in the query string. Here is an example:
ASINAmazon

 

     3. Create a user control called AmazonItemLookUp.ascx and use the code below:

 

AmazonItemLookUp.ascx

<h2>Insert ASIN</h2> 
        <asp:TextBox ID="ItemIDTextBox" runat="server" MaxLength="10"></asp:TextBox> 
        <br /> 
       <asp:Button ID="btnSubmit" runat="server" Text="Submit"  
            onclick="btnSubmit_Click" /> 
            <br /> 
            <br /> 
          <asp:Repeater ID="Repeater1" runat="server"
          <ItemTemplate> 
          <asp:Image ID="SmallImage" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "MediumProductImageURL") %>' /> 
          <br /> 
          <asp:Label ID="ASIN" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ASIN") %>' Visible="false"></asp:Label> 
          <br /> 
          <i><asp:Label ID="ProductNameLabel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ProductName") %>'></asp:Label></i
<br /> 
          <b><asp:Label ID="ProductPriceLabel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ProductPrice") %>'></asp:Label> 
</b> 
<br /> 
<asp:HyperLink ID="DetailPageURLHyperLink" runat="server" Text="Buy this Product!" Target="_blank" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "DetailPageURL") %>'></asp:HyperLink> 
          </ItemTemplate> 
          </asp:Repeater>  

 

AmazonItemLookUp.ascx.cs

protected void btnSubmit_Click(object sender, EventArgs e) 
    { 
        Amazon a = new Amazon(); 
        //Insert AwsAccessKeyId below 
        a.AwsAccessKeyId = "xxxxxxxxxxxx";  
        a.ItemId = ItemIDTextBox.Text; 
        Repeater1.DataSource = a.PopulateAmazonList(); 
        Repeater1.DataBind(); 
 
    } 
 

      4. Create a class called Amazon in your App_Code folder and insert the code below:

 

 

using System.Linq; 
using System.Collections; 
using System.Xml.Linq; 
 
/// <summary> 
/// Summary description for Amazon 
/// </summary> 
public class Amazon 
    public string GetURL() 
    { 
        return "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=" + AwsAccessKeyId + "&Version=2009-03-31&Operation=ItemLookup&ResponseGroup=Request,SalesRank,Small,Images,OfferSummary&ItemId=" + ItemId; 
 
    } 
 
    private string _AwsAccessKeyId; 
    public string AwsAccessKeyId 
    { 
        get 
        { 
            return _AwsAccessKeyId; 
        } 
        set 
        { 
            _AwsAccessKeyId = value; 
        } 
    } 
    private string _ItemId; 
    public string ItemId 
    { 
        get 
        { 
            return _ItemId; 
        } 
        set 
        { 
            _ItemId = value; 
        } 
    } 
 
    /// <summary> 
    /// Gets namespace needed for Amazon.com recommendations 
    /// </summary> 
    public static XNamespace ns 
    { 
        get 
        { 
            return "http://webservices.amazon.com/AWSECommerceService/2009-03-31"
        } 
    } 
 
    /// <summary> 
    /// Populates a list of product recommendations from Amazon.com 
    /// </summary> 
    /// <param name="list2"></param> 
    public IList PopulateAmazonList() 
    { 
        XElement tags = XElement.Load(GetURL()); 
        var groups = from book in tags.Descendants(ns + "Item"// Return all nodes with the word "Item" in it 
                     let bookAttributes = book.Element(ns + "ItemAttributes"//In each Item node, get the "ItemAttributes" node 
                     let offerAttributes = book.Element(ns + "OfferSummary"//Get the Offer Summary Node 
                     let mediumImageAttributes = book.Element(ns + "MediumImage"
                     let lowestNewPrice = offerAttributes.Element(ns + "LowestNewPrice"//Within the Offer Summary Node, get the Lowest New Price node 
 
                     select new 
                     { 
                         ASIN = ((string)book.Element(ns + "ASIN")), 
                         ProductName = ((string)bookAttributes.Element(ns + "Title")), 
                         MediumProductImageURL = ((string)mediumImageAttributes.Element(ns + "URL")), 
                         DetailPageURL = ((string)book.Element(ns + "DetailPageURL")), 
                         ProductPrice = ((string)lowestNewPrice.Element(ns + "FormattedPrice")) 
 
                     }; 
        return groups.ToList(); 
    } 

 

     5. Upload the User Control to Sitefinity

     6.  Inside the text box, insert the ASIN number of the product

     7.  Click on the link "Buy this Product" ,that appears below, and give it to your customer as shown below:

 AmazonImage

How it Works

Using a long query string, Amazon returns a large XML file with many attributes for the item. Here is a sample:

 

 AmazonXML

To view your URL, place a break point on this code in Amazon.cs:

var Groups

In the URL, you'll notice many properties in the query string and here is an explanation of a few of them:

 

Operation - This defines what you're seeking to do. In this case, I want to look up a single product. You can also look up a list of products based on other identifying information, like a search index or a tag. Amazon also has many other operations and for more info, please read this article.

 

Response Groups - These pull certain item attributes into the XML file. If attributes were removed from your query string in response groups, you'd notice that data would be missing in the XML file. For more info on response groups, please read this article.


progress-logo

The Progress Guys

View all posts from The Progress Guys on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments
Comments are disabled in preview mode.
Topics
 
 
Latest Stories in
Your Inbox
Subscribe
More From Progress
d12fcc0bdb669b804e7f71198c9619a7
5 Questions Automakers Should Ask to Improve Asset Uptime
Download Whitepaper
 
SF_MQ_WCM
2018 Gartner Magic Quadrant Web Content Management (WCM)
Download Whitepaper
 
What-Serverless-Means-For-Enterprice-Apps-Kinvey
What Serverless Means for Enterprise Apps
Watch Webinar