Shabdar.org
Webshabdar.org
Implementing Amazon API Signed Requests Helper in ASP.Net and C# PDF Print E-mail
Written by Shabdar   
Thursday, 03 September 2009 10:32

Amazon Web Service Sample

Download Sample Download Sample

Introduction

As many of you know, Starting August 15, 2009, all Amazon API requests must be authenticated using signatures.There is a whole bunch of documentation on Amazon website that describes how to authenticate requests. Visit following URL for more information.

http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html?RequestAuthenticationArticle.html

But as a newbie to Amazon APIs it was lot more difficult for me to build a working sample in ASP.Net and C#. After hours of search, I came across following article that helped me build my first successful sample. This article provided me a helper class in VB that can be used to generate a signed URL. I had to convert it in C#.

http://solutions.amazonwebservices.com/connect/thread.jspa?threadID=33790

Description

Download my sample code from above link and open it with Visual Studio 2005 or 2008 as a website. I have used .Net Framework 2.0 for this sample. But it should work fine with version 3.5 as well.

Most of the sample code is self explanatory. Comments are written inline with source code which should help you get started.

Following are key notes for this sample to work.

Go to Default.aspx.cs file and change your access key and secret key in following lines

private const string MY_AWS_ACCESS_KEY_ID = "<Enter your Access Key here>";
private const string MY_AWS_SECRET_KEY = "<Enter your Secret Key here>";

If you are behind corporate firewall or proxy, you may need to authenticate HTTP requests. For that go to Web.config file and enter your windows user name and password in app.Settings.

<appSettings>
<add key="WindowsUserName" value=""/>
<add key="WindowsPassword" value=""/>
</appSettings>

Source Code Explanation

Most important part in this sample are following lines which are used to generate signed URL.

//Use SignedRequesthelper class to generate signed request.

SignedRequestHelper helper = new SignedRequestHelper(MY_AWS_ACCESS_KEY_ID, MY_AWS_SECRET_KEY, DESTINATION);

IDictionary<string, string> requestParams = new Dictionary<string, String>();

requestParams["Service"] = "AWSECommerceService";

//Leave following line commented if you want to use latest version of Amazon API. You can uncomment this line to use a specific version.
//requestParams["Version"] = "2009-03-31";

requestParams["Operation"] = "ItemSearch";
requestParams["SearchIndex"] = selectCategories.Value;
requestParams["Keywords"] = txtSearch.Text;

//Get signed URL in a variable
string requestUrl = helper.Sign(requestParams);

As you can see requestParams is an array which holds all the parameters to be passed along with request url.

Once you have signed URL ready, you can use WebRequest class to get response from Amazon web service.

DataSet GetData(string signedurl)
{
   try
   {
   //Create a request object using signed URL.
   WebRequest request = HttpWebRequest.Create(signedurl);
   //Get response in a stream
   Stream responseStream = request.GetResponse().GetResponseStream();

   DataSet DS = new DataSet();
   //Read returned resonpse stream into a dataset.
   //Note: You can also use XMLDocument element here to read response.
   DS.ReadXml(responseStream);
   responseStream.Close();

   return DS;
}

As you can see in above code, we get response in XML format which is then read into a Dataset. Dataset stores it as Tables.

Note that I am not referencing Amazon Web Service in this project. It could have been lot more easier to just use web methods to get Items. But unfotunately I could not find any way of signing a request with webservice.

Once we have returned data into dataset we can just bind these tables with Grid View or whatever way you want to use it.

Final Notes

In my sample I have just implemented search service from amazon. You can also implement Item Lookup in similar way. You just need to change request parameters in my sample. Feel free to modify it as you desire.

Comments
Add New Search
+/-
Write comment
Name:
Email:
 
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
Please input the anti-spam code that you can read in the image.
Milan Arizankoski |77.28.6.xxx |2009-09-27 16:39:29
Tell me how you pull aditional information about the searched item from the
dataset.
Example if you search book, how you pull the image of the book, price, overview
and etc...
bcrawl |70.179.168.xxx |2009-12-27 08:25:20
Thanks a lot for your excellent post!!!!
Dileep Kumar  - small bug |121.247.145.xxx |2010-01-06 00:28:48
There is an erro in your code. I spent few hours to spot the error. Your code
does not accepts space. Before submit to search you need to replace the white
space with plus sign ('+').



requestParams["keywords"] = txtSearch.Text.Replace("
","+");



Rest of the code is fine.



Thanks,



Dileep
Yatin |202.179.81.xxx |2010-03-31 03:49:10
How can i get the price of item?
parminder  - how to search by ASIN |59.161.124.xxx |2010-04-08 01:37:41
Hi,
The article is really very good. but how to search data using ASIN. can you
help.

Regards
Parmnder
yatin  - Search data using ASIN |202.179.81.xxx |2010-04-14 02:28:11
Parmnder,

by using itemlookup, set parameter like requestParams["ItemId"] =
"B0028JT0H0";
requestParams["IdType"] = "ASIN";

Last Updated on Wednesday, 24 March 2010 10:43