Friday, October 19, 2012

Implementing Amazon API Signed Requests Helper in ASP.Net and C#

Implementing Amazon API Signed Requests Helper in ASP.Net and C#


'Amazon

'Download 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
1.private const string MY_AWS_ACCESS_KEY_ID = '<Enter />';
2.private const string MY_AWS_SECRET_KEY = '<Enter />';
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.
1.<appSettings>
2.<add key="WindowsUserName" value="''/" />
3.<add key="WindowsPassword" value="" />
4.</appSettings>

Source Code Explanation

Most important part in this sample are following lines which are used to generate signed URL.
01.//Use SignedRequesthelper class to generate signed request.
02. 
03.SignedRequestHelper helper = new SignedRequestHelper(MY_AWS_ACCESS_KEY_ID, MY_AWS_SECRET_KEY, DESTINATION);
04. 
05.IDictionary requestParams = new Dictionary();
06. 
07.requestParams['Service'] = 'AWSECommerceService';
08. 
09.//Leave following line commented if you want to use latest version of Amazon API. You can uncomment this line to use a specific version.
10.//requestParams['Version'] = '2009-03-31';
11. 
12.requestParams['Operation'] = 'ItemSearch';
13.requestParams['SearchIndex'] = selectCategories.Value;
14.requestParams['Keywords'] = txtSearch.Text;
15. 
16.//Get signed URL in a variable
17.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.
01.DataSet GetData(string signedurl)
02.{
03.try
04.{
05.//Create a request object using signed URL.
06.WebRequest request = HttpWebRequest.Create(signedurl);
07.//Get response in a stream
08.Stream responseStream = request.GetResponse().GetResponseStream();
09. 
10.DataSet DS = new DataSet();
11.//Read returned resonpse stream into a dataset.
12.//Note: You can also use XMLDocument element here to read response.
13.DS.ReadXml(responseStream);
14.responseStream.Close();
15. 
16.return DS;
17.}
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.

No comments:

Post a Comment