亚马逊 mws对接系统时SignatureDoesNotMatch报错

错误内容:
<?xml version="1.0"?>


Sender
SignatureDoesNotMatch
The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.

23142876-a0db-42f5-9ba9-527313c7bb78

我的代码:
public class TestUtl {
private static final String CHARACTER_ENCODING = "UTF-8";
final static String ALGORITHM = "HmacSHA256";

public static void main(String[] args) throws Exception {
    String timestamp = AmazonTimeUtil.getAmaZonTime();
    String secretKey = "iXvQ+bHDVSkaMBaXXXXXXXXXXXXXXXX";
    // Use the endpoint for your marketplace
    String serviceUrl = "https://mws.amazonservices.com/";
    // Create set of parameters needed and store in a map
    HashMap<String, String> parameters = new HashMap<String, String>();
    // Add required parameters. Change these as needed.

    parameters.put("AWSAccessKeyId", urlEncode("AKIAJXAAD6Y2XXXXXXXX"));
    parameters.put("SignatureMethod", urlEncode("HmacSHA256"));
    parameters.put("SignatureVersion", urlEncode("2"));
       // parameters.put("SubmittedFromDate",urlEncode(submittedFromDate));
      //  parameters.put("SubmittedFromDate",getFormattedTimestamp());
    parameters.put("Timestamp", urlEncode(timestamp));
    parameters.put("Version", urlEncode("2013-09-01"));
    parameters.put("Action", urlEncode("ListOrders"));
    parameters.put("MarketplaceId",urlEncode("ATVPDKIKX0DER"));
    parameters.put("CreatedAfter",  urlEncode("2016-05-25T10:56:00Z"));
    parameters.put("SellerId", urlEncode("A1VGN18HFINRY8"));
    parameters.put("MWSAuthToken", urlEncode("amzn.mws.b2a759cd-f24d-80f6-3cce-b543342ddd7a"));


    String formattedParameters = calculateStringToSignV2(parameters, serviceUrl);

    String signature = sign(formattedParameters, secretKey);

    Map<String, String> sorted = new TreeMap<String, String>();
    sorted.putAll(parameters);
    // Add signature to the parameters and display final results
    parameters.put("Signature", urlEncode(signature));

    System.out.println(calculateStringToSignV2(parameters, serviceUrl));

    String pa = HttpUtils.prepareParam(sorted);
    System.out.println("url的值为:"+"https://mws.amazonservices.com/Orders/2013-09-01/?"+pa+"&Signature="+urlEncode(signature)+"\n");
    try {
        URL url = new URL("https://mws.amazonservices.com/Orders/2013-09-01/?"+pa+"&Signature="+urlEncode(signature));

        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setConnectTimeout(50000);

        BufferedReader br = null;
        StringBuffer sbOrderResponse = new StringBuffer();
        String order = "";
        String str = "";

        InputStream inputstream = null;

        if (conn.getResponseCode() != 200) {
            inputstream = conn.getErrorStream();
        } else {
            inputstream = conn.getInputStream();
        }

        br = new BufferedReader(new InputStreamReader((inputstream)));
        StringBuffer buffer = new StringBuffer();

        while ((order = br.readLine()) != null) {
            sbOrderResponse.append(order);
            str = str + order + "\n";
        }
        System.out.println(conn.getResponseCode() + "    " + conn.getResponseMessage());
        System.out.println(str);

    } catch (Exception e) {
        System.out.println("Error   " + e);
    }
}

private static String calculateStringToSignV2(Map<String, String> parameters, String serviceUrl)
        throws SignatureException, URISyntaxException {
    // Sort the parameters alphabetically by storing
    // in TreeMap structure
    Map<String, String> sorted = new TreeMap<String, String>();
    sorted.putAll(parameters);

    // Set endpoint value
    URI endpoint = new URI(serviceUrl.toLowerCase());
    // Create flattened (String) representation
    StringBuilder data = new StringBuilder();
    data.append("GET\n");
    data.append(endpoint.getHost());
    data.append("\n/Orders/2013-09-01"); // /Orders/2013-09-01
    data.append("\n");
    Iterator<Entry<String, String>> pairs = sorted.entrySet().iterator();

    while (pairs.hasNext()) {
        Map.Entry<String, String> pair = pairs.next();
        if (pair.getValue() != null) {
            data.append(pair.getKey() + "=" + pair.getValue());
        } else {
            data.append(pair.getKey() + "=");
        }
        // Delimit parameters with ampersand (&)
        if (pairs.hasNext()) {
            data.append("&");
        }
    }
    return data.toString();
}

/*
 * Sign the text with the given secret key and convert to base64
 */
private static String sign(String data, String secretKey)
        throws NoSuchAlgorithmException, InvalidKeyException, IllegalStateException, UnsupportedEncodingException {

    Mac mac = Mac.getInstance(ALGORITHM);
    mac.init(new SecretKeySpec(secretKey.getBytes(CHARACTER_ENCODING), ALGORITHM));
    byte[] signature = mac.doFinal(data.getBytes(CHARACTER_ENCODING));
    String signatureBase64 = new String(Base64.encodeBase64(signature), CHARACTER_ENCODING);

    return new String(signatureBase64);
}

private static String urlEncode(String rawValue) {
    String value = (rawValue == null) ? "" : rawValue;
    String encoded = null;
    try {
        encoded = URLEncoder.encode(value, CHARACTER_ENCODING).replace("+", "%20").replace("*", "%2A")
                .replace("%7E", "~");
    } catch (UnsupportedEncodingException e) {
        System.err.println("Unknown encoding: " + CHARACTER_ENCODING);
        e.printStackTrace();
    }
    return encoded;
}

}

1个回答

iXvQ+bHDVSkaMBaXXXXXXXXXXXXXXXX";
// Use the endpoint for your marketplace
String serviceUrl = "https://mws.amazonservices.com/";
// Create set of parameters needed and store in a map
HashMap parameters = new HashMap();
// Add required parameters. Change these as needed.

parameters.put("AWSAccessKeyId", urlEncode("AKIAJXAAD6Y2XXXXXXXX"));
parameters.put("SignatureMethod", urlEncode("HmacSHA256"));
parameters.put("SignatureVersion", urlEncode("2"));
   // parameters.put("SubmittedFromDate",urlEncode(submittedFromDate));
  //  parameters.put("SubmittedFromDate",getFormattedTimestamp());
parameters.put("Timestamp", urlEncode(timestamp));
parameters.put("Version", urlEncode("2013-09-01"));
parameters.put("Action", urlEncode("ListOrders"));
parameters.put("MarketplaceId",urlEncode("ATVPDKIKX0DER"));
parameters.put("CreatedAfter",  urlEncode("2016-05-25T10:56:00Z"));
parameters.put("SellerId", urlEncode("A1VGN18HFINRY8"));
parameters.put("MWSAuthToken", urlEncode("amzn.mws.b2a759cd-f24d-80f6-3cce-b543342ddd7a"));


String formattedParameters = calculateStringToSignV2(parameters, serviceUrl);

String signature = sign(formattedParameters, secretKey);

Map<String, String> sorted = new TreeMap<String, String>();
sorted.putAll(parameters);
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐