【问题标题】:Search amazon example with new amazon service使用新的亚马逊服务搜索亚马逊示例
【发布时间】:2011-12-29 21:14:04
【问题描述】:

我找不到新的亚马逊服务的工作示例(或者至少在过去几年内)。无论我在标题中添加什么内容,最接近的工作示例都会返回一个空项目。代码是:

        // 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();

我做错了什么?

【问题讨论】:

    标签: c# wcf amazon amazon-product-api


    【解决方案1】:

    我假设您已经下载了code from here。如果这是正确的,那么您需要替换此行:

    AWSECommerceServicePortTypeClient amazonClient = new AWSECommerceServicePortTypeClient();
    

    这些行:

    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));
    

    问题有两个方面:

    1. 您没有将 amazonClient 绑定到 HttpBinding
    2. 您没有签署请求

    如果我的假设不正确,那么您应该从上面的链接下载代码,因为它是如何调用亚马逊产品 API 的工作示例。

    【讨论】:

    • 我之前的代码和另一个示例中的当前代码建议在 response.Items[0].Item 为空时引发相同的错误。我查看调试器,果然是 null。
    • 恐怕不能再帮你了。此代码之外还有其他问题。也许您的其中一个键不正确?我的答案中链接中的代码适用于我的密钥。对不起。
    【解决方案2】:

    我相信您的问题可能是缺少关联标签。截至 2011 年 11 月,所有请求都需要这样做,我在测试的早期就注意到,当我没有包含它时,我得到了空响应(带有错误代码)。我不确定这是否仍然是这种行为,但我肯定会假设如果你不添加它(我在你的代码中没有看到),那很可能是嫌疑犯。

    Look at top change note here

    如果您没有 Associate ID,则需要申请一个。

    【讨论】:

      猜你喜欢
      • 2013-01-06
      • 2016-11-26
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 2022-12-03
      • 2016-01-06
      相关资源
      最近更新 更多