2011-12-01 159 views
8

我一直在玩亞馬遜的產品廣告API,並且我無法獲得通過並提供數據的請求。我一直關閉的這一點:http://docs.amazonwebservices.com/AWSECommerceService/2011-08-01/GSG/這:Amazon Product Advertising API signed request with Java通過Java/SOAP的亞馬遜產品廣告API

這裏是我的代碼..我用這個生成的SOAP綁定:http://docs.amazonwebservices.com/AWSECommerceService/2011-08-01/GSG/YourDevelopmentEnvironment.html#Java

在類路徑中,我只有:commons-codec.1.5.jar

import com.ECS.client.jax.AWSECommerceService; 
import com.ECS.client.jax.AWSECommerceServicePortType; 
import com.ECS.client.jax.Item; 
import com.ECS.client.jax.ItemLookup; 
import com.ECS.client.jax.ItemLookupRequest; 
import com.ECS.client.jax.ItemLookupResponse; 
import com.ECS.client.jax.ItemSearchResponse; 
import com.ECS.client.jax.Items; 

public class Client { 

    public static void main(String[] args) { 

     String secretKey = <my-secret-key>; 
     String awsKey = <my-aws-key>; 

     System.out.println("API Test started"); 

     AWSECommerceService service = new AWSECommerceService(); 
     service.setHandlerResolver(new AwsHandlerResolver(
       secretKey)); // important 
     AWSECommerceServicePortType port = service.getAWSECommerceServicePort(); 

     // Get the operation object: 
     com.ECS.client.jax.ItemSearchRequest itemRequest = new com.ECS.client.jax.ItemSearchRequest(); 

     // Fill in the request object: 
     itemRequest.setSearchIndex("Books"); 
     itemRequest.setKeywords("Star Wars"); 
     // itemRequest.setVersion("2011-08-01"); 
     com.ECS.client.jax.ItemSearch ItemElement = new com.ECS.client.jax.ItemSearch(); 
     ItemElement.setAWSAccessKeyId(awsKey); 
     ItemElement.getRequest().add(itemRequest); 

     // Call the Web service operation and store the response 
     // in the response object: 
     com.ECS.client.jax.ItemSearchResponse response = port 
       .itemSearch(ItemElement); 

     String r = response.toString(); 
     System.out.println("response: " + r); 

     for (Items itemList : response.getItems()) { 
      System.out.println(itemList); 
      for (Item item : itemList.getItem()) { 
       System.out.println(item); 
      } 
     } 

     System.out.println("API Test stopped"); 

    } 
} 

這裏是我回來。我希望能看到一些在亞馬遜提供星球大戰書籍甩了我的控制檯: - /:

API Test started 
response: [email protected] 
[email protected] 
API Test stopped 

我在做什麼錯誤(請注意,第二個for循環中沒有「item」正在打印出來,因爲它是空的)?我如何解決這個問題或獲取相關的錯誤信息?

回答

4

這結束了工作(我有我的associateTag添加到請求):

public class Client { 

    public static void main(String[] args) { 

     String secretKey = "<MY_SECRET_KEY>"; 
     String awsKey = "<MY AWS KEY>"; 

     System.out.println("API Test started"); 


     AWSECommerceService service = new AWSECommerceService(); 
     service.setHandlerResolver(new AwsHandlerResolver(secretKey)); // important 
     AWSECommerceServicePortType port = service.getAWSECommerceServicePort(); 

     // Get the operation object: 
     com.ECS.client.jax.ItemSearchRequest itemRequest = new com.ECS.client.jax.ItemSearchRequest(); 

     // Fill in the request object: 
     itemRequest.setSearchIndex("Books"); 
     itemRequest.setKeywords("Star Wars"); 
     itemRequest.getResponseGroup().add("Large"); 
//  itemRequest.getResponseGroup().add("Images"); 
     // itemRequest.setVersion("2011-08-01"); 
     com.ECS.client.jax.ItemSearch ItemElement = new com.ECS.client.jax.ItemSearch(); 
     ItemElement.setAWSAccessKeyId(awsKey); 
     ItemElement.setAssociateTag("th0426-20"); 
     ItemElement.getRequest().add(itemRequest); 

     // Call the Web service operation and store the response 
     // in the response object: 
     com.ECS.client.jax.ItemSearchResponse response = port 
       .itemSearch(ItemElement); 

     String r = response.toString(); 
     System.out.println("response: " + r); 

     for (Items itemList : response.getItems()) { 
      System.out.println(itemList); 

      for (Item itemObj : itemList.getItem()) { 

       System.out.println(itemObj.getItemAttributes().getTitle()); // Title 
       System.out.println(itemObj.getDetailPageURL()); // Amazon URL 
      } 
     } 

     System.out.println("API Test stopped"); 

    } 
} 
1

它看起來像響應對象不覆蓋toString(),所以如果它包含某種錯誤響應,只需打印它不會告訴你錯誤響應是什麼。您需要查看api中響應對象返回的字段並單獨打印這些字段。要麼你會得到一個明顯的錯誤信息,否則你將不得不回到他們的文檔來試圖弄清楚什麼是錯誤的。

+0

是的,看看你能不能用TCPMON或類似的檢查,從亞馬遜的XML響應。然後,您將能夠在其XML中看到完整的錯誤消息。 – davidfrancis

1

你需要調用Item對象的get方法來檢索其詳細信息,如:

for (Item item : itemList.getItem()) { 
    System.out.println(item.getItemAttributes().getTitle()); //Title of item 
    System.out.println(item.getDetailPageURL()); // Amazon URL 
    //etc 
} 

如果有任何錯誤,你可以通過調用getErrors()

if (response.getOperationRequest().getErrors() != null) { 
    System.out.println(response.getOperationRequest().getErrors().getError().get(0).getMessage()); 
} 
+0

感謝您的輸入,但是「response.getOperationRequest()」和「itemList.getItem()」都是空的,所以沒有任何東西被打印出來......: - \ – systemoutprintln

+0

這可能是因爲您還沒有指定響應組與請求[(請參閱Amazon產品API文檔)](http://docs.amazonwebservices.com/AWSECommerceService/2011-08-01/DG/index.html?CHAP_ResponseGroupsList.html)。您可以在請求中添加回復組: 'itemRequest.getResponseGroup()。add(「Large」); (「圖片」);' –

10

讓他們我沒有使用SOAP API,但是您的賞金需求沒有聲明它必須只使用SOAP,而您想給亞馬遜打電話並獲得結果。因此,我將使用REST API將至少滿足你們所要求的發佈此工作示例:

我想擊中亞馬遜服務器並返回結果

你一些工作示例代碼「會需要下載下面以滿足簽名要求:

http://associates-amazon.s3.amazonaws.com/signed-requests/samples/amazon-product-advt-api-sample-java-query.zip

將它解壓縮並抓住com.amazon.advertising.api.sample.SignedRequestsHelper.java文件,並把它直接進入你r項目。此代碼用於簽署請求。

您還需要從下面下載Apache Commons Codec 1.3,並將其添加到您的類路徑中,即將其添加到項目的庫中。請注意,這是編解碼器的唯一版本,將與上述類工作(SignedRequestsHelper

http://archive.apache.org/dist/commons/codec/binaries/commons-codec-1.3.zip

現在您可以複製並粘貼以下確保使用正確的軟件包名稱,以取代your.pkg.here和更換SECRETKEY屬性:

package your.pkg.here; 

import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.StringWriter; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.Properties; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 
import javax.xml.transform.OutputKeys; 
import javax.xml.transform.Transformer; 
import javax.xml.transform.TransformerException; 
import javax.xml.transform.TransformerFactory; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult; 
import org.w3c.dom.Document; 
import org.xml.sax.SAXException; 

public class Main { 

    private static final String SECRET_KEY = "<YOUR_SECRET_KEY>"; 
    private static final String AWS_KEY = "<YOUR_KEY>"; 

    public static void main(String[] args) { 
     SignedRequestsHelper helper = SignedRequestsHelper.getInstance("ecs.amazonaws.com", AWS_KEY, SECRET_KEY); 

     Map<String, String> params = new HashMap<String, String>(); 
     params.put("Service", "AWSECommerceService"); 
     params.put("Version", "2009-03-31"); 
     params.put("Operation", "ItemLookup"); 
     params.put("ItemId", "1451648537"); 
     params.put("ResponseGroup", "Large"); 

     String url = helper.sign(params); 
     try { 
      Document response = getResponse(url); 
      printResponse(response); 
     } catch (Exception ex) { 
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    private static Document getResponse(String url) throws ParserConfigurationException, IOException, SAXException { 
     DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
     Document doc = builder.parse(url); 
     return doc; 
    } 

    private static void printResponse(Document doc) throws TransformerException, FileNotFoundException { 
     Transformer trans = TransformerFactory.newInstance().newTransformer(); 
     Properties props = new Properties(); 
     props.put(OutputKeys.INDENT, "yes"); 
     trans.setOutputProperties(props); 
     StreamResult res = new StreamResult(new StringWriter()); 
     DOMSource src = new DOMSource(doc); 
     trans.transform(src, res); 
     String toString = res.getWriter().toString(); 
     System.out.println(toString); 
    } 
} 

正如你可以看到這是更簡單的安裝和比SOAP API使用。如果您沒有使用SOAP API的特定要求,那麼我強烈建議您改用REST API。

使用REST API的缺點之一是不會將結果解組到對象中。這可以通過基於wsdl創建所需的類來彌補。

+1

我不得不添加params.put(「AssociateTag」,「th0426-20」)才能使它工作,但是謝謝..絕對有一些進展。 – systemoutprintln

+0

不完全是我想要的,但我會給你點,因爲它使我在正確的方向。 – systemoutprintln

+0

我忘記了新的Associate標籤要求。很高興我能幫忙。 –

相關問題