2011-12-01 9 views
8

私はAmazonのProduct Advertising APIを使って遊んでいます。 http://docs.amazonwebservices.com/AWSECommerceService/2011-08-01/GSG/この:私はこれのオフに取り組んできましたAmazon Product Advertising API signed request with JavaここJava/SOAPによるAmazon Product Advertising 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 

私は間違っています(2番目の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

あなたがアマゾンからのXMLレスポンスをチェックするのtcpmonまたは類似を使用することができるかどうかはい、ご覧ください。その後、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 "); itemRequest.getResponseGroup()。add( "Images"); ' –

10

を呼び出すことによって、それらを得ることができます私はSOAP APIを使用していませんが、あなたのBountyの要件は、Amazonを呼び出して結果を得るだけの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を次からダウンロードしてクラスパスに追加する必要があります。つまり、プロジェクトのライブラリに追加する必要があります。これは今、あなたは適切なパッケージ名でyour.pkg.hereを交換し、SECRETを交換するには、次の作ってくださいをコピーして貼り付けることができます上記のクラスで動作しますコーデックの唯一のバージョン(SignedRequestsHelper

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

であることに注意してくださいそしてKEYプロパティ:あなたは、これがSOAP APIをよりセットアップと使用にはるかに簡単です

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を使用するための特定の要件がない場合は、代わりにREST APIを使用することを強くお勧めします。

REST APIを使用する際の欠点の1つは、結果がオブジェクトにアンマーシャルされていないことです。これは、wsdlに基づいて必要なクラスを作成することで解決できます。

+1

params.put(" AssociateTag "、" th0426-20 ")を追加して動作させる必要がありましたが、ありがとうございました。 – systemoutprintln

+0

正確には私が望んでいたものではありませんでしたが、私は正しい方向に私を設定して以来、あなたにポイントを与えます。 – systemoutprintln

+0

私は新しいアソシエイトタグ要件について忘れてしまいました。私は助けることができてうれしい。 –

関連する問題