AWS SignatureDoesNotMatch

接收:

SignatureDoesNotMatch我们计算的请求签名与您提供的签名不匹配。 检查您的AWS秘密访问密钥和签名方法。 详细信息请参阅服务文档。

有以下几点:

String associateTag = "example-20";
String awsAccessKeyId = "accessKeyId";
String awsSecretKey = "secretKey";
String endpoint = "webservices.amazon.com";
String uri = "/onca/xml";
String charset = "UTF8";

private String buildQueryString(String keywords) {
    Map<String,String> params = new ArrayMap<>();
    List<String> pairs = new ArrayList<>();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

    params.put("Service","AWSECommerceService");
    params.put("Operation","ItemSearch");
    params.put("AWSAccessKeyId",awsAccessKeyId);
    params.put("AssociateTag",associateTag);
    params.put("SearchIndex","All");
    params.put("ResponseGroup","Images,ItemAttributes");
    params.put("Timestamp",sdf.format(new Date()));
    params.put("Keywords", keywords);

    Map<String, String> treeMap = new TreeMap<>(params);
    try {
        for (Map.Entry<String, String> param : treeMap.entrySet()) {
            pairs.add(URLEncoder.encode(param.getKey(), charset) + "=" + URLEncoder.encode(param.getValue(), charset));
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    String queryString = "";
    for (int i = 0; i < pairs.size(); i++) {
        if (i != 0) {
            queryString += "&";
        }

        queryString += pairs.get(i);
    }

    Log.d(TAG, "queryString: " + queryString);

    return queryString;
}

private String buildSignature(String queryString) {
    String hash = "";

    try {
        String message = "GETn" + endpoint + "n" + uri + "n" + queryString;
        Log.d(TAG, "message: " + message);

        Mac sha_HMAC = Mac.getInstance("HmacSHA256");

        SecretKeySpec secret_key = new SecretKeySpec(awsSecretKey.getBytes(charset), "HmacSHA256");
        sha_HMAC.init(secret_key);

        hash = Base64.encodeToString(sha_HMAC.doFinal(message.getBytes(charset)), Base64.DEFAULT);
    }
    catch (Exception e){
        System.out.println("Error");
    }

    return hash;
}

public void searchProducts(String keywords) {
    String requestUrl = "";
    String queryString = buildQueryString(keywords);
    String signature = buildSignature(queryString);

    Log.d(TAG, "signature: " + signature);

    try {
        requestUrl = "http://" + endpoint + uri + "?" + queryString + "&Signature=" + URLEncoder.encode(signature, charset);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    Log.d(TAG, "requestUrl: " + requestUrl);

        Ion.with(context)
            .load(requestUrl)
            .asString()
            .setCallback(new FutureCallback<String>() {
                @Override
                public void onCompleted(Exception e, String result) {
                    Log.d(TAG, "searchProducts result: " + result);
                }
            });
}

可能是什么问题呢?


确保你的系统时钟是正确的。 使用NTP同步它是个好主意。 在过去,当时间不同步时,我看到了签名错误。


我之前见过的是,这通常取决于权限。 检查访问和密钥是否正确,并且您有足够的权限。


更改:

Base64.DEFAULT;

至:

Base64.NO_WRAP;
链接地址: http://www.djcxy.com/p/38997.html

上一篇: AWS SignatureDoesNotMatch

下一篇: Not able to send Email using Amazon SES in Java