0

我对 Java 比较陌生,并且已经被困了几天,试图让我的应用程序“发布”一个 HTTP 请求到亚马逊的 MWS API。我不断收到“SignatureDoesNotMatch”回复,但我不知道为什么。我已经广泛使用了亚马逊的 MWS 暂存器,使用它我在获得成功响应方面没有任何问题,所以我的密钥等都可以。暂存器生成的内容参数和 base64 签名与我的应用程序生成的相匹配,因此我确信我的应用程序正确编译了参数和签名。当我将暂存器生成的内容参数硬编码到我的应用程序中时,我收到“SignatureDoesNotMatch”错误响应。

我的代码的相关部分是:

/*
 * get amazon timestamp
 */
GetAmazonTimestamp timestampObj = new GetAmazonTimestamp();
String amazonTimestamp = null;
try {
    amazonTimestamp = timestampObj.getTimestamp();
    amazonTimestamp.replace(".000Z", "Z");
} catch (IOException e) {
    e.printStackTrace();
}

/*
 *  create http parameters and initialise the signature value
 */ 
String URLendpoint =  "https://mws.amazonservices.co.uk/orders/2011-01-01";         

String param1 = "AWSAccessKeyId";       String value1 = "AKIAIZXBKLVSGBBQQL2A";
String param2 = "Action";               String value2 = "ListOrders"; 
String param3 = "LastUpdatedAfter";     String value3 = "2013-02-01T00:00:00Z";
String param4 = "MarketplaceId.Id.1";   String value4 = "A1F83G8C2ARO7P";
String param5 = "SellerId";             String value5 = "A3A2272JFHXROO";
String param6 = "SignatureMethod";      String value6 = "HmacSHA256";
String param7 = "SignatureVersion";     String value7 = "2";
String param8 = "Timestamp";            String value8 = amazonTimestamp;
String param9 = "Version";              String value9 = "2011-01-01";
String param10 = "Signature";           String value10 = null;

/* 
 *  build sections of URL components for signature      
 */ 
String URLforSignature = "POST\n" + "mws.amazonservices.co.uk\n" + "/Orders/2011-01-01\n" +
    URLEncoder.encode(param1,"UTF-8") + "=" +
    URLEncoder.encode(value1,"UTF-8") + "&" + 
    URLEncoder.encode(param2,"UTF-8") + "=" +
    URLEncoder.encode(value2,"UTF-8") + "&" + 
    URLEncoder.encode(param3,"UTF-8") + "=" +
    URLEncoder.encode(value3,"UTF-8") + "&" + 
    URLEncoder.encode(param4,"UTF-8") + "=" +
    URLEncoder.encode(value4,"UTF-8") + "&" + 
    URLEncoder.encode(param5,"UTF-8") + "=" +
    URLEncoder.encode(value5,"UTF-8") + "&" + 
    URLEncoder.encode(param6,"UTF-8") + "=" +
    URLEncoder.encode(value6,"UTF-8") + "&" + 
    URLEncoder.encode(param7,"UTF-8") + "=" +
    URLEncoder.encode(value7,"UTF-8") + "&" + 
    URLEncoder.encode(param8,"UTF-8") + "=" +
    URLEncoder.encode(value8,"UTF-8") + "&" + 
    URLEncoder.encode(param9,"UTF-8") + "=" +
    URLEncoder.encode(value9,"UTF-8");

/*
 * hash and base64 encode the signature using the URLforSignature
 */
GetAmazonSignature signatureObj = new GetAmazonSignature();                 
value10 = signatureObj.getSignature(URLforSignature);

/*
 * create the http post
 */
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(URLendpoint);

String line = null;

try {
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    post.addHeader("Content-Type", "application/x-www-form-urlencoded");
    nameValuePairs.add(new BasicNameValuePair(param1, value1));
    nameValuePairs.add(new BasicNameValuePair(param2, value2));
    nameValuePairs.add(new BasicNameValuePair(param3, value3));
    nameValuePairs.add(new BasicNameValuePair(param4, value4));
    nameValuePairs.add(new BasicNameValuePair(param5, value5));
    nameValuePairs.add(new BasicNameValuePair(param6, value6));
    nameValuePairs.add(new BasicNameValuePair(param7, value7));
    nameValuePairs.add(new BasicNameValuePair(param8, value8));
    nameValuePairs.add(new BasicNameValuePair(param9, value9));
    nameValuePairs.add(new BasicNameValuePair(param10, value10));

    post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));

    HttpResponse response = client.execute(post);
    BufferedReader rd = new BufferedReader(new InputStreamReader(
            response.getEntity().getContent()));
    line = "";

    while ((line = rd.readLine()) != null) {
        System.out.println(line);
    }
}

谢谢你的帮助。

4

3 回答 3

1

感谢那些帮助我的人,但我最终发现“ https://mws.amazonservices.co.uk/orders/2011-01-01 ”在订单中需要一个大写“O”。亚马逊的开发支持人员同意“SignatureDoesNotMatch”错误消息在这种情况下不是很有帮助,并表示他们会对此进行调查。

再次感谢

于 2013-02-25T13:13:01.080 回答
0

为什么不使用亚马逊为 MWS 提供的 Java 客户端库?

转到MWS并单击您感兴趣的 API。您将看到一个 Java 客户端库链接,您可以从中获取所需的文件。它们包含示例并将处理 URL 签名、解析和其他您通常必须自己完成的工作。

于 2013-02-12T03:39:17.400 回答
0

您构建的方式URLforSignature似乎还可以。

但是,您没有显示您的代码GetAmazonSignature。我怀疑这个缺陷在那里。您可能忘记对结果进行 base64 编码了吗?请注意,MWS 暂存器在“请求详细信息”页面上显示了十六进制签名以及 base64 编码的签名。您应该能够通过将这些细节与您的函数结果进行比较来找到缺陷。

于 2013-02-24T19:00:06.663 回答