2011-12-01 160 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标签要求。很高兴我能帮忙。 –

相关问题