1 August 2009 4:05p Pacific

403 Forbidden (and 400 Bad Request) errors with Amazon Product Advertising API

by Matt Sherman

I recently experienced an issue using the Amazon Product Advertising API (formerly the Amazon Associates Web Service). I was getting a “403 Forbidden” error for what seemed like a normal request.

I am using the sample C# code provided here, along with the Amazon.ECS library (which apparently is no longer supported by Amazon).

I was doing an ItemSearchRequest, with a search term of “black swan”.

I tracked the error down to the encoding of the search terms. They were being passed with the URL encoding, so the querystring became “black+swan”. This is apparently no longer supported – you must use percent-style encoding. By using “black%20swan” instead, it worked.

This is caused by a bug in the sample code at the link above. Line 142 of SignedRequestHelper.cs reads:

str.Replace("'", "%27").Replace("(", "%28").Replace(")", "%29")
.Replace("*", "%2A").Replace("!", "%21").Replace("%7e", "~").Replace("+", "%20");

It needs to read:

str = str.Replace("'", "%27").Replace("(", "%28").Replace(")", "%29")
.Replace("*", "%2A").Replace("!", "%21").Replace("%7e", "~").Replace("+", "%20");

Someone forgot that strings are immutable!

By the way, I was making changes because Amazon has a new security requirement for this API. If you would like some help/code in adapting to the updated API, drop me a note.

--

While I’m in the neighborhood, I also had to deal with a “400 Bad Request” error.

Part of the new API authentication scheme is a timestamp. The clock on my server had fallen quite a ways behind – it hadn’t been synced in a while. This resulted in an inaccurate timestamp on the request, and Amazon rejected it.

After correcting the server’s clock, it worked fine.

Comments are closed

Tell others

TwitterTweet this page
Digg!Digg this page
TwitterAdd to Google Reader

Experimental! Let me know how it works for you.

Shorten this page's URL

Learn more about the TinyASP URL shortener

ASP.Net jQuery Controls

Implement jQuery effects using familiar ASP.Net server controls. Learn more...

Recent posts

Avoiding “magic strings” in jQuery, C# and ASP.net MVC

Alikewise learnings #1: DIY PR

Sherman’s law of prior knowledge, or, predicting the past

The busiest people at Apple right now…

When “infographics” jump the shark

HTTPS is the least of your problems

Stacking up

Beware the truth-tellers

more...  

About us

ClipperHouse.com is brought to you by Matt Sherman and Fernando Chilvarguer, among others. Contact us here.