2011-11-07 115 views
0

我想要使用新產品亞馬遜API來搜索亞馬遜上的產品。我一直在看他們的示例代碼和其他人的例子,但我沒有收到任何結果,並想知道是否有其他人最近使用過API並可以提供一些幫助?亞馬遜API,產品廣告API,ItemSearch,C#

using System; 
using System.ServiceModel; 
using Simple.Amazon.ECS; 

namespace Simple { 
    class Program { 
     // your Amazon ID's 
     private const string accessKeyId = "*******************"; 
     private const string secretKey = "************************************"; 

     // the program starts here 
     static void Main(string[] args) { 

      // create a WCF Amazon ECS client 
      BasicHttpBinding binding  = new BasicHttpBinding(BasicHttpSecurityMode.Transport); 
      binding.MaxReceivedMessageSize = int.MaxValue; 
      AWSECommerceServicePortTypeClient client = new AWSECommerceServicePortTypeClient(
       binding, 
       new EndpointAddress("https://webservices.amazon.com/onca/soap?Service=AWSECommerceService")); 

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

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

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

      // issue the ItemSearch request 
      ItemSearchResponse response = client.ItemSearch(itemSearch); 

      // write out the results 
      foreach (var item in response.Items[0].Item) { 
       Console.WriteLine(item.ItemAttributes.Title); 
      } 
     } 
    } 
} 

所有樣品/例子是在結構上與此類似,但是當涉及到foreach循環沒有項目返回(NULL),所以我得到一個空的異常錯誤。

+1

「響應」究竟包含什麼內容? –

+0

「未將對象引用設置爲對象的實例」。當它到達foreach循環。 – Standage

+0

希望我能告訴你什麼是錯的。我運行了我從[1]下載的代碼,並按預期運行。 [1] http://flyingpies.wordpress。com/2009/08/01/17/ –

回答

1

如果上面的解決方案仍然是行不通的。

試試這個..

下載上http://www.falconwebtech.com/post/2010/06/14/Using-WCF-and-SOAP-to-Send-Amazon-Product-Advertising-API-Signed-Requests.aspx

我們需要更新服務引用,請在app.config中,Program.cs中,並reference.cs變化不大的示例代碼。

app.config: (1.) appSettings tag; 分配accessKeyId和secretKey值, 添加。 (2.)行爲標記 - > endpointBehaviors標記 - >行爲標記 - > signingBehavior標記; 分配accessKeyId和secretKey值。 (3.)綁定標籤 - > basicHttpBinding標籤; (可選) 刪除除AWSECommerceServiceBindingNoTransport 和AWSECommerceServiceBindingTransport之外的綁定標記。 (4.)客戶端標籤; 刪除AWSECommerceServiceBindingTransport以外的端點標記。

program.cs: add itemSearch.AssociateTag = ConfigurationManager.AppSettings [「associateTag」]; ItemSearchResponse響應之前= amazonClient.ItemSearch(itemSearch);

reference.cs:(在服務引用打開文件夾使用的Visual Studio) 變化私人ImageSet [] [] imageSetsField;私人ImageSet [] imageSetsField; 變化公共ImageSet [] [] ImageSets {...}公共ImageSet [] ImageSets {...}

終於可以運行我們的程序,它會工作。祝你好運..

nb:我使用微軟視覺工作室2010年。 會有1個警告(無效的子元素簽名行爲),我想我們可以忽略它,或者如果你有任何解決方案,請分享.. ^^ v ..