Amazon API ItemSearch returns (400) Bad Request

2019-07-20 12:29发布

问题:

I'm using a simple example from Amazon documentation for ItemSearch and I get a strange error: "The remote server returned an unexpected response: (400) Bad Request."

This is the code:

public static void Main()
        {
            //Remember to create an instance of the amazon service, including you Access ID. 

            AWSECommerceServicePortTypeClient service = new AWSECommerceServicePortTypeClient(new BasicHttpBinding(),
                                                                                              new EndpointAddress(
                                                                                                "http://webservices.amazon.com/onca/soap?Service=AWSECommerceService"));


            AWSECommerceServicePortTypeClient client = new AWSECommerceServicePortTypeClient(
             new BasicHttpBinding(),
             new EndpointAddress("http://webservices.amazon.com/onca/soap?Service=AWSECommerceService"));

            // prepare an ItemSearch request  
            ItemSearchRequest request = new ItemSearchRequest();
            request.SearchIndex = "Books";
            request.Title = "Harry+Potter";
            request.ResponseGroup = new string[] { "Small" };
            ItemSearch itemSearch = new ItemSearch();
            itemSearch.Request = new ItemSearchRequest[] { request };
            itemSearch.AWSAccessKeyId = accessKeyId;

            // issue the ItemSearch request
            try
            {
                ItemSearchResponse response = client.ItemSearch(itemSearch);
                // write out the results  

                foreach (var item in response.Items[0].Item)
                {
                    Console.WriteLine(item.ItemAttributes.Title);
                }
            }
            catch(Exception e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(e.Message);
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Press any key to quit...");
                Clipboard.SetText(e.Message);
            }
            Console.ReadKey();

What is wrong?

回答1:

You receive this message because your request is not signed. Starting from August 2009 all requests must be signed.

Here you can see at the example about how to sign amazon requests: http://developer.amazonwebservices.com/connect/entry.jspa?externalID=2480&categoryID=14