Amazon Product Advertising API通过Java/SOAP

mai*_*rgs 8 java soap amazon jax-ws amazon-product-api

我一直在玩亚马逊的产品广告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 = <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");

    }
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的东西..我希望看到亚马逊上的一些星球大战书籍被丢弃到我的控制台: - /:

API Test started
response: com.ECS.client.jax.ItemSearchResponse@7a6769ea
com.ECS.client.jax.Items@1b5ac06e
API Test stopped
Run Code Online (Sandbox Code Playgroud)

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

Jon*_*ner 10

我不使用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 = "<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);
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,这比SOAP API更易于设置和使用.如果您没有使用SOAP API的特定要求,那么我强烈建议您使用REST API.

使用REST API的一个缺点是结果不会为您解组到对象中.这可以通过基于wsdl创建所需的类来解决.


mai*_*rgs 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");

    }
}
Run Code Online (Sandbox Code Playgroud)