我对Java相对较新,并且已经停留了几天,试图让我的应用程序向亚马逊的MWS API发出“POST”请求。我一直收到'SignatureDoesNotMatch'回复,我无法弄清楚原因。我已经广泛使用了亚马逊的MWS暂存器,并且使用它我获得成功的响应没有任何问题,所以我的钥匙等都可以。暂存器生成的内容参数和base64签名与我的应用程序生成的内容相匹配,因此我确信我的应用程序正确编译了参数和签名。当我将暂存器生成的内容参数硬编码到我的应用程序中时,我得到'SignatureDoesNotMatch'错误响应。我非常感谢来自更有经验的Java开发人员或任何已经制作了像我在亚马逊MWS上工作的应用程序的人的指示。
我的代码的相关部分是:
/*
* 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);
}
}
感谢您的帮助。
答案 0 :(得分:1)
感谢那些帮助我的人,但我终于知道“https://mws.amazonservices.co.uk/orders/2011-01-01”在命令中需要一个大写“O”。亚马逊的开发支持同意在这种情况下'SignatureDoesNotMatch'错误消息并不是很有用,并且已经说他们会调查它。
再次感谢
答案 1 :(得分:0)
为什么不使用亚马逊为MWS提供的Java客户端库?
转到MWS并单击您感兴趣的API。您将看到一个Java客户端库链接,您可以在其中获取所需的文件。它们包含示例,并将处理URL签名,解析以及您通常必须自己完成的其他工作。
答案 2 :(得分:0)
你构建URLforSignature
的方式似乎没问题。
但您没有显示GetAmazonSignature
的代码。我怀疑那里有缺陷。你可能忘了对你的结果进行base64编码吗?请注意,MWS scratchpad在“请求详细信息”页面上显示十六进制签名以及base64编码。您应该能够通过将这些细节与您的功能结果进行比较来找到缺陷。