亚马逊产品广告API通过Java / SOAP

我一直在玩亚马逊的产品广告API,我无法获得通过并提供数据的请求。 我一直在努力:http://docs.amazonwebservices.com/AWSECommerceService/2011-08-01/GSG/以及:亚马逊产品广告API对Java签名的请求

这是我的代码..我使用这个生成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 = <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: com.ECS.client.jax.ItemSearchResponse@7a6769ea
com.ECS.client.jax.Items@1b5ac06e
API Test stopped

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


我没有使用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文件并将其直接放入您的项目中。 此代码用于签署请求。

您还需要从下面下载Apache Commons Codec 1.3,并将其添加到您的类路径中,即将其添加到项目的库中。 请注意,这是Codec的唯一版本,可以与上面的类一起使用( 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创建所需的类来弥补。


这结束了工作(我不得不添加我的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");

    }
}

它看起来像响应对象不覆盖toString(),所以如果它包含某种错误响应,只需打印它不会告诉你错误响应是什么。 您需要查看api中响应对象返回的字段并单独打印这些字段。 要么你会得到一个明显的错误信息,否则你将不得不回到他们的文档来试图弄清楚什么是错误的。

链接地址: http://www.djcxy.com/p/38959.html

上一篇: Amazon Product Advertising API through Java/SOAP

下一篇: What is the best way to iterate over a dictionary?