Amazon Product Advertising API通过Java / SOAP

我一直在玩亚马逊的产品广告API,我无法获得要求通过并向我提供数据。 我一直在努力解决这个问题: http : //docs.amazonwebservices.com/AWSECommerceService/2011-08-01/GSG/以及: 使用Java签署的Amazon Product Advertising API请求

这是我的代码..我用这个生成了SOAP绑定: http : //docs.amazonwebservices.com/AWSECommerceService/2011-08-01/GSG/YourDevelopmentEnvironment.html#Java

在Classpath上,我只有: 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 = ; String awsKey = ; 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: com.ECS.client.jax.ItemSearchResponse@7a6769ea com.ECS.client.jax.Items@1b5ac06e API Test stopped 

我做错了什么(请注意,第二个for循环中没有“item”正在打印出来,因为它是空的)? 如何解决此问题或获取相关的错误信息?

我不使用SOAP API,但是你的Bounty要求没有声明它只需要使用SOAP,你只想调用Amazon并获得结果。 所以,我将使用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文件并将其直接放入项目中。 此代码用于签署请求。

您还需要从下面下载Apache Commons Codec 1.3并将其添加到您的类路径中,即将其添加到项目的库中。 请注意,这是将与上述类( SignedRequestsHelper )一起使用的唯一版本的Codec

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 = ""; private static final String AWS_KEY = ""; public static void main(String[] args) { SignedRequestsHelper helper = SignedRequestsHelper.getInstance("ecs.amazonaws.com", AWS_KEY, SECRET_KEY); Map params = new HashMap(); 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创建所需的类来解决。

这最终工作(我不得不将我的associateTag添加到请求中):

 public class Client { public static void main(String[] args) { String secretKey = ""; String awsKey = ""; 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"); } } 

看起来响应对象不会覆盖toString(),因此如果它包含某种错误响应,只需打印它就不会告诉您错误响应是什么。 您需要查看api,了解响应对象中返回的字段并单独打印它们。 要么你会得到一个明显的错误信息,要么你必须回到他们的文档来试图找出问题所在。

您需要调用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()); } 
Interesting Posts