Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / C# 4.0

"Hello Amazon!" - Making a first request to the Amazon Product API

Rate me:
Please Sign up or sign in to vote.
4.90/5 (3 votes)
24 Jun 2009CPOL1 min read 52.2K   2.3K   20   3
This demo allows you to verify that you have a valid key and can format a request..

Image 1

Introduction

This article is a simple "Hello"-type program to instantiate and interpret a simple request to the Amazon ECommerce Services.

Background

In order to make a request, you simply must supply your own AWSAccessKeyID. You can get one for free at http://aws.amazon.com/. Amazon does supply some code to perform a similar test at http://docs.amazonwebservices.com/AWSECommerceService/2009-01-06/GSG/index.html?ImplementinganA2SRequest.html, but it doesn't compile as is, and doesn't show the nice features available for using the information in the response without having to process the raw XML.

Using the code

In order to make use of the product API, you need to add it as a service reference to your project. In Solution Explorer, right-click on References and select "Add Service Reference...". Use http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl as the address. Add a using reference anyplace you want to access the full set of objects and methods in the service. Web Services are wonderful - two steps, and you can use intellisense to sort through the functionality.

To get information from the API, you need to first create a request. That request is attached to a search which is just a wrapper for one or more searches that also holds the AWSAccessKeyID. Finally, a port is opened and an ItemSearch is invoked with the search as a parameter.

C#
// In order to find information about an item we need at least one search request
// This search request (and any others) is attached to a search
// finally the search is submitted on a port and returns a response

// Create the request object
ItemSearchRequest request = new ItemSearchRequest();
            
// Fill request object with request parameters
request.ResponseGroup = new string[] { "ItemAttributes" };

// Set SearchIndex to All and use the scanned EAN
// as the keyword, this should generate a single response 
request.SearchIndex = "All";
request.Keywords = txtLookupEAN.Text;

// Make the item search 
ItemSearch search = new ItemSearch();

// It is ABSOLUTELY CRITICAL that you change
// the AWSAccessKeyID to YOUR uniqe value
// Signup for an account (and AccessKeyID) at http://aws.amazon.com/ 
search.AWSAccessKeyId = "[INSERT YOUR ACCESS ID HERE]";

// Set the request on the search wrapper - multiple requests
// can be submitted on one search
search.Request = new ItemSearchRequest[] { request };

// Make the port
AWSECommerceServicePortTypeClient port = 
             new AWSECommerceServicePortTypeClient();

//Send the request, store the response and display some of the results
ItemSearchResponse response = port.ItemSearch(search);

Points of Interest

One small difficulty in searching for information is that while Amazon continues to rename the technology for ECommerce Services, the original name is still in widespread use. Originally, it was called ECS (ECommerce Services), then it was named "Amazon Associates Web Service", and now it is officially "Product Advertising API". However, the base WSDL document is still named AWSECommerceService.wsdl.

History

None so far.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Kanasz Robert27-Sep-12 8:31
professionalKanasz Robert27-Sep-12 8:31 
QuestionOutdated Pin
cipherwar4-Apr-12 8:59
cipherwar4-Apr-12 8:59 
GeneralYou also need to sign the requests now Pin
Rei Miyasaka11-Jun-10 9:10
Rei Miyasaka11-Jun-10 9:10 
More info here: http://flyingpies.wordpress.com/2009/08/01/17/[^]

I've also written up all of that sample code in F#, if anyone's interested:

F#
open System
open System.ServiceModel
open System.Runtime.Serialization
open System.ServiceModel.Description
open System.ServiceModel.Dispatcher
open System.Text
open System.Text.RegularExpressions
open System.Security.Cryptography
open System.ServiceModel.Channels

let signingBehavior keyID secretKey = { new IEndpointBehavior with
    member x.Validate e = ()
    member x.AddBindingParameters (_,_) = ()
    member x.ApplyDispatchBehavior (_,_) = ()
    member x.ApplyClientBehavior (e, r) =
        { new IClientMessageInspector with
            member x.AfterReceiveReply (_,_) = ()
            member x.BeforeSendRequest (r, c) =            
                let ts = DateTime.UtcNow.ToString "yyyy-MM-ddTHH:mm:ssZ"
                let signature = 
                    let op = Regex.Match(r.Headers.Action, "[^/]+$").ToString()
                    let sha = new HMACSHA256(Encoding.UTF8.GetBytes(secretKey:string))
                    (op + ts)
                    |> Encoding.UTF8.GetBytes
                    |> sha.ComputeHash
                    |> Convert.ToBase64String
                    
                let header n v = { new MessageHeader() with
                    member x.Name = n
                    member x.Namespace = "http://security.amazonaws.com/doc/2007-01-01/"
                    member x.OnWriteHeaderContents(w, _) =
                        w.WriteString (v:string)
                }
                
                r.Headers.Add (header "AWSAccessKeyId" keyID)
                r.Headers.Add (header "Timestamp" ts)
                r.Headers.Add (header "Signature" signature)
                null
        }
        |> r.MessageInspectors.Add
    }

let ac = new AWSECommerceServicePortTypeClient(
            BasicHttpBinding(BasicHttpSecurityMode.Transport),
            EndpointAddress("https://ecs.amazonaws.com/onca/soap"))
ac.Endpoint.Behaviors.Add(SigningBehavior.signingBehavior AccessKeyID SecretAccessKey)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.