Search amazon example with new amazon service

与世无争的帅哥 提交于 2019-12-30 10:45:03

问题


I can not find a working example of the new amazon service (or at least, within the last couple of years). The closest working example just comes back with a null item no matter what I put in the title. The code is:

        // Amazon ProductAdvertisingAPI client
        AWSECommerceServicePortTypeClient amazonClient = new AWSECommerceServicePortTypeClient();

        // prepare an ItemSearch request
        ItemSearchRequest request = new ItemSearchRequest();
        request.SearchIndex = "Books";
        request.Title = "C#";
        request.Condition = Condition.All;
        //request.ResponseGroup = new string[] { "Small" };

        ItemSearch itemSearch = new ItemSearch();
        itemSearch.Request = new ItemSearchRequest[] { request };
        itemSearch.AWSAccessKeyId = ConfigurationManager.AppSettings["accessKeyId"];

        // send the ItemSearch request
        ItemSearchResponse response = amazonClient.ItemSearch(itemSearch);

        // write out the results from the ItemSearch request
        foreach (var itemLst in response.Items)
        {
            if (itemLst.Item != null)
            {
                foreach (var item in response.Items[0].Item)
                {
                    Console.WriteLine(item.ItemAttributes.Title);
                }
            }
            else
                Console.WriteLine("No item info was found for this response list item.");
        }
        Console.WriteLine("<Done...press enter to continue>");
        Console.ReadLine();

What am I doing wrong?


回答1:


I'm assuming that you've downloaded the code from here. If this is correct then you need to replace this line:

AWSECommerceServicePortTypeClient amazonClient = new AWSECommerceServicePortTypeClient();

With these lines:

BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
binding.MaxReceivedMessageSize = int.MaxValue;

AWSECommerceServicePortTypeClient amazonClient = new AWSECommerceServicePortTypeClient(
            binding,
            new EndpointAddress("https://webservices.amazon.com/onca/soap?Service=AWSECommerceService"));  

// add authentication to the ECS client
amazonClient.ChannelFactory.Endpoint.Behaviors.Add(new AmazonSigningEndpointBehavior(accessKeyId, secretKey));

The problem is two fold:

  1. You are not binding the amazonClient to an HttpBinding
  2. You are not signing the request

If my assumption is incorrect then you should download the code from the above link as it is a working example of how to call the Amazon Product API.




回答2:


I believe your problem may be lack of an Associate Tag. As of November, 2011, this is required for all requests and I noticed early on in my testing that I got null responses back (with an error code) when I didn't include it. I'm not sure if that's still the behavior but I'd definitely assume that if you aren't adding it (which I don't see in your code) that's a likely suspect.

Look at top change note here

If you don't have an Associate ID you will need to apply for one.



来源:https://stackoverflow.com/questions/8142909/search-amazon-example-with-new-amazon-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!