亚马逊产品广告 API 使用 Java 签署的请求 [英] Amazon Product Advertising API signed request with Java

查看:50
本文介绍了亚马逊产品广告 API 使用 Java 签署的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过数小时的修补和阅读整个互联网数次后,我就是不知道如何签署使用产品广告 API 的请求.

after many hours of tinkering and reading the whole internet several times I just can't figure out how to sign requests for use with the Product Advertising API.

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

So far I managed to generate a client from the provided WSDL file. I used a tutorial by Amazon for this. You can find it here:

生成 Web 服务客户端的教程

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

So far no problems. To test the client I wrote a small piece of code. The code is intended to simply get some information about a product. The product is specified by its 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");
  }
}

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

As you can see there is no part where I sign the request. I have worked my way through a lot of the classes used and found no methods for signing the request.

那么,如何签署请求?

我实际上在文档中找到了一些东西:请求身份验证

I actually found something in the documentation: request authentication

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

But they don't use their own API. The proposed solutions are more or less for manual use only. So I looked in the client classes to sort out if I could get the request URL and put all the parts needed for request signing in myself. But there are no such methods.

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

I hope someone can point out what I am doing wrong.

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

This is what I did to solve the problem. All the credit goes to Jon and the guys of the Amazon forums.

在我概述我做了什么之前,这里是帮助我解决问题的帖子的链接:亚马逊论坛上的论坛帖子.

Before I outline what I did, here is a link to the post which helped me to solve the problem: Forum Post on Amazon forums.

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

I downloaded the awshandlerresolver.java which is linked in the post. Than I modified my own code so it looks like this:

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 或多或少没用.但它有效.我还使用了 WSDL Jon 链接来生成一个新的 web 服务客户端.我刚刚更改了我在问题中发布的教程中的 URL.

The println on the end are more or less useless. But it works. I also used the WSDL Jon linked to generate a new webservice client. I just changed the URLs in the tutorial I posted in my question.

推荐答案

创建服务后试试这个

service.setHandlerResolver(new AwsHandlerResolver(my_AWS_SECRET_KEY));

您需要这个 类和 this jar 文件作为 AwsHandlerResolver 使用的项目的引用添加Base64 编码.

You'll need this class and this jar file to add as a reference to your project as AwsHandlerResolver uses Base64 encoding.

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

You'll need to rename the AwsHandlerResolver file to the name of the class as the file name is all lower case.

我认为您拥有的其余代码没问题.

I think the rest of the code you have is fine.

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

这篇关于亚马逊产品广告 API 使用 Java 签署的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆