Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android AWS签名不匹配_Android_Amazon Web Services - Fatal编程技术网

Android AWS签名不匹配

Android AWS签名不匹配,android,amazon-web-services,Android,Amazon Web Services,接收: signaturedesnotmatch我们计算的请求签名与您提供的签名不匹配。检查您的AWS秘密访问密钥和签名方法。有关详细信息,请参阅维修文档 以下是: String associateTag = "example-20"; String awsAccessKeyId = "accessKeyId"; String awsSecretKey = "secretKey"; String endpoint = "webservices.amazon.com"; String uri =

接收:

signaturedesnotmatch我们计算的请求签名与您提供的签名不匹配。检查您的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 = "GET\n" + 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);
                }
            });
}
String associateTag=“示例-20”;
字符串awsAccessKeyId=“accessKeyId”;
字符串awsSecretKey=“secretKey”;
String endpoint=“webservices.amazon.com”;
字符串uri=“/onca/xml”;
字符串charset=“UTF8”;
私有字符串buildQueryString(字符串关键字){
Map params=new ArrayMap();
列表对=新的ArrayList();
SimpleDataFormat sdf=新的SimpleDataFormat(“yyyy-MM-dd'T'HH:MM:ss.SSS'Z',Locale.US”);
sdf.setTimeZone(TimeZone.getTimeZone(“UTC”));
参数put(“服务”、“AWSECommerceService”);
参数put(“操作”、“项搜索”);
参数put(“AWSAccessKeyId”,AWSAccessKeyId);
参数put(“AssociateTag”,AssociateTag);
参数put(“搜索索引”、“全部”);
参数put(“响应组”、“图像、项属性”);
参数put(“Timestamp”,sdf.format(newdate());
参数put(“关键字”,关键字);
映射树映射=新树映射(参数);
试一试{
对于(Map.Entry参数:treeMap.entrySet()){
add(URLEncoder.encode(param.getKey(),charset)+“=”+URLEncoder.encode(param.getValue(),charset));
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
字符串queryString=“”;
对于(int i=0;i
可能是什么问题?

已更改:

Base64.DEFAULT;
致:


很难从代码中分辨出来。需要检查的几件事:

  • 确保要访问的服务使用SigV2签名(或查询字符串签名)。新服务遵循版本4签名标准
  • urlcoder.encode
    不符合AWS的编码要求。您需要对编码的字符串应用一些修复
  • 查询字符串应以不区分大小写的方式排序
  • 你的证件确实正确
看看在官方SDK中是如何实现的,这是一个好主意


PS:不使用Official SDK的原因是什么?

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

确保您的系统时钟正确。最好使用NTP同步它。在过去,当时间不同步时,我看到签名错误

在过去我遇到这个问题时,它与系统时间有关。将时间与NTP fix issue for me

同步是一些进行签名的python代码,例如。注意的查询字符串需要区分大小写。还有一些和身体有关。是实现Ficial SDK的aws规范,是用java 6编写的,不能与Android/Kotlin应用程序一起使用
Base64.NO_WRAP;