java - 使用 Java 的亚马逊产品广告 API 签名请求

标签 java web-services api amazon-web-services amazon

经过数小时的修改和多次阅读整个互联网后,我只是想不出如何签署使用产品广告 API 的请求。

到目前为止,我设法从提供的 WSDL 文件生成了一个客户端。我为此使用了亚马逊的教程。你可以在这里找到它:

Tutorial for generating the web service client

到目前为止没有问题。为了测试客户端,我写了一小段代码。该代码旨在简单地获取有关产品的一些信息。该产品由其 ASIN 指定。

代码:

package client;

import com.ECS.client.jax.AWSECommerceService;
import com.ECS.client.jax.AWSECommerceServicePortType;
import com.ECS.client.jax.ItemLookup;
import com.ECS.client.jax.ItemLookupResponse;
import com.ECS.client.jax.ItemLookupRequest;

public class Client {

  public static void main(String[] args) {
    System.out.println("API Test startet");

    AWSECommerceService service = new AWSECommerceService();
    AWSECommerceServicePortType port = service.getAWSECommerceServicePort();

    ItemLookupRequest itemLookup = new ItemLookupRequest();
    itemLookup.setIdType("ASIN");
    itemLookup.getItemId().add("B000RE216U");

    ItemLookup lookup = new ItemLookup();
    lookup.setAWSAccessKeyId("<mykeyishere>");
    lookup.getRequest().add(itemLookup);

    ItemLookupResponse response = port.itemLookup(lookup);

    String r = response.toString();
    System.out.println("response: " + r);

    System.out.println("API Test stopped");
  }
}

如您所见,没有我签署请求的部分。我已经研究了很多使用的类,但没有找到用于签署请求的方法。

那么,如何签署请求?

我实际上在文档中找到了一些东西:request authentication

但他们不使用自己的 API。建议的解决方案或多或少仅供手动使用。因此,我查看了客户端类,以确定是否可以获取请求 URL 并将请求签名所需的所有部分放入我自己。但是没有这样的方法。

我希望有人能指出我做错了什么。


这就是我为解决问题所做的。所有功劳归功于Jon和亚马逊论坛的人。

在概述我所做的之前,这里有一个帮助我解决问题的帖子的链接:Forum Post on Amazon forums .

我下载了帖子中链接的 awshandlerresolver.java。比起我修改自己的代码,它看起来像这样:

package client;

import com.ECS.client.jax.AWSECommerceService;
import com.ECS.client.jax.AWSECommerceServicePortType;
import com.ECS.client.jax.ItemLookup;
import com.ECS.client.jax.ItemLookupResponse;
import com.ECS.client.jax.ItemLookupRequest;

public class Client {

  public static void main(String[] args) {
    System.out.println("API Test startet");

    AWSECommerceService service = new AWSECommerceService();
    service.setHandlerResolver(new AwsHandlerResolver("<Secret Key>"));  // important
    AWSECommerceServicePortType port = service.getAWSECommerceServicePort();

    ItemLookupRequest itemLookup = new ItemLookupRequest();
    itemLookup.setIdType("ASIN");
    itemLookup.getItemId().add("B000RE216U");

    ItemLookup lookup = new ItemLookup();
    lookup.setAWSAccessKeyId("<Access Key>"); // important
    lookup.getRequest().add(itemLookup);

    ItemLookupResponse response = port.itemLookup(lookup);

    String r = response.toString();
    System.out.println("response: " + r);   
    System.out.println("API Test stopped");
  }
}

最后的 println 或多或少没用。但它有效。我还使用了 Jon 链接的 WSDL 来生成一个新的 web 服务客户端。我刚刚更改了我在问题中发布的教程中的 URL。

最佳答案

在创建服务后试试这个

service.setHandlerResolver(new AwsHandlerResolver(my_AWS_SECRET_KEY));

你需要 this类和 this jar 文件添加为对项目的引用,因为 AwsHandlerResolver 使用 Base64 编码。

您需要将 AwsHandlerResolver 文件重命名为类名,因为文件名全部小写。

我认为您的其余代码没问题。

WSDL 是 http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl

关于java - 使用 Java 的亚马逊产品广告 API 签名请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3189979/

相关文章:

java - 用 Python/Java 翻录音频 CD

java - 使用 JDK 10 运行 icCube

web-services - Axis 错误 : There is no SOAP service at this location

javascript - 带有 iFrame 示例的 Facebook 应用程序 JS API?

Python 2.7 - 帮助使用 API (HL7)

java - 从自定义模型生成 Java 类

java - 如何从两个具体类型的类中删除循环依赖

php - 不使用 PHP 连接 MySql 和 Android

web-services - 带有 SimpleJaxWsServiceExporter 的 Spring Webservice

java - Java 标准 API 中的内存泄漏陷阱