Using WCF and SOAP to Send Amazon Product Advertising API Signed Requests

14. June 2010 23:09 by viperguynaz in .NET, WCF, Featured  //  Tags: , , ,   //   Comments (5)

This article and the associated Windows Communication Foundation (WCF) sample (download) demonstrate how to implement and configure a WCF client using a custom message inspector to sign SOAP based Amazon Product Advertising API requests.  The sample uses the .NET Framework 4.0 to communicate with Amazon’s Product Advertising API.  The Product Advertising API allows you to:

 

  • Access Amazon Product Selection: The Product Advertising API gives you access to Amazon’s selection of millions of products in categories such as books, music, digital downloads and electronics from Amazon.com, Amazon.ca, Amazon.co.uk, Amazon.de, Amazon.fr, and Amazon.co.jp.
  • Leverage Amazon Product Discovery capabilities: The Product Advertising API lets you leverage Amazon’s customer-centric features such as Product Search, Customer Reviews, Similar Products, Accessories, Listmania Lists and more.
  • Monetize your website: The Product Advertising API allows you to include your associate tag in API requests to automatically format the URLs returned by the API to ensure that you earn referral fees when users you refer to Amazon sites buy qualifying products.

 

This sample makes use of a WCF custom MessageInspector as described in the MSDN documentation.  A message inspector is an extensibility object that can be used in the service model's client runtime and dispatch runtime programmatically or through configuration and that can inspect and alter messages after they are received or before they are sent.  The sample here is derived from Oren Trutner's article “Signing Amazon Product Advertising API – C#/WCF.”  Oren’s example is extended to allow all the configuration to occur in the application configuration (app.config) file.  All of the Amazon helper methods have been moved to a separate library that can be used/referenced in any project.  A separate console app has been included that demonstrates how to use the app.config file to configure the WCF endpoints and how to add the custom MessageInspector behavior to the endpoint.

 

The sample is self-contained, and does not require you to read the entire post to use.  Simply download the source, modify the app.config with your Amazon Product Advertising API keys, build and run.

 

The project includes the Amazon.PAAPI.WCF library of helper functions that can be used in any project to sign SOAP based Amazon Product Advertising API requests.  The AmazonSigningMessageInspector class implements IClientMessageInspector and does the work of signing the SOAP request elements IAW Amazon’s specifications.  The AmazonSigningEndpointBehavior class implements IEndpointBehavior to add the custom MessageInspector to the endpoint behavior.   Finally, the AmazonSigningBehaviorExtensionElement class implements BehaviorExtensionElement so that the endpoint behavior can be configured in the app.config file of the executing program.

 

To use the library, create an application like the AmazonSOAP project included in the example solution.  Add a reference to the Amazon.PAAPI.WCF library (copy local set to “true”).  Modify the app.config file:

 

First, in the system.serviceModel section, add a child section “extensions” and then add the custom  behaviorExtension –

 

<extensions>
  <behaviorExtensions>
    <add name="signingBehavior" type="Amazon.PAAPI.WCF.AmazonSigningBehaviorExtensionElement, Amazon.PAAPI.WCF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
  </behaviorExtensions>
</extensions>

 

Next, add an endpointBehavior in the behaviors section that makes use of the behaviorExtension just defined:

 

<behaviors>
  <endpointBehaviors>
    <behavior name="amazonEndpointBehavior">
      <signingBehavior accessKeyId="yourAmazonPublicKey" secretKey="yourAmazonSecretKey" />
    </behavior>
  </endpointBehaviors>
</behaviors>

Finally, modify the client endpoint to use the endpointBehavior by adding the behaviorConfiguration attribute - behaviorConfiguration="amazonEndpointBehavior"

 

<client>
  <endpoint address="https://ecs.amazonaws.com/onca/soap?Service=AWSECommerceService"
    binding="basicHttpBinding" bindingConfiguration="AWSECommerceServiceBindingTransport" behaviorConfiguration="amazonEndpointBehavior"
    contract="Amazon.PAAPI.AWSECommerceServicePortType" name="AWSECommerceServicePort" />
</client>

Hope this helps – good luck!

 

 

amazonProductAdvertisingAPI-SOAP-WCF.zip (1.11 mb)

Comments (5) -

Scott Wojan
3/20/2011 9:17:52 AM #

Thanks for this!  They really need to update their docs!!

Frank
7/19/2011 8:48:15 AM #

I ran the attached code.
I found the code don't even call the custom WCF behaviours,  in fact you can unload the Amazon.PAAPI.WCF project and remove the reference from the AmazonSOAP project. the code still runs.

am I missing something?

Frank
7/19/2011 9:00:05 AM #

Sorry, Your code runs fine.
the issue I got was, when I update the service reference. the (behaviorConfiguration="amazonEndpointBehavior") was removed by the automated process. which cause the program no longer call your signing code.

great sample.

DaveR
11/23/2011 2:40:17 PM #

Amazon made changes to the API effective Nov 1, 2011.  I'm hacking through trying to modify your code to work with their changes.  The simple change was to add
itemSearch.AssociateTag = ConfigurationManager.AppSettings["associateTag"];
to line 20 of Program.cs (and to add an associated tag in the app.config).

But, line 18 in app.config          
<signingBehavior accessKeyId="myKey" secretKey="mySecretKey" />
gets an error "The element 'behavior' has invalid child element 'signingBehavior' "

Can you please update your article and sample code to adjust for the new API changes?

Vga Over Cat5
12/1/2011 1:50:49 AM #

I had a plan to use dotnet and amazon integration at one of my application. Your code example becomes my medicine. But I wonder if it is rest request or soap? I have tried to use in php application when I was started it was easy to manage but after my project getting bigger than code become very complicated.



Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About the author

After 20 years flying F-16s in the USAF, I've had a ton of fun and I'm now doing something I enjoy almost as much, developing code.  I currently work as an ASP.NET/MVC developer for SpinSix Strategic Marketing Design in Scottsdale AZ.  I love developing ASP.NET MVC and eCommerce sites.

Month List

Page List