计算得出的签名与提供的签名不匹配

计算得出的签名与提供的签名不匹配

本文介绍了JWPlayer和S3:计算得出的签名与提供的签名不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AWS S3将视频上传到JWPlayer.

I'm trying to upload videos to JWPlayer using AWS S3.

我的后端是在Spring中编码的,所以我正在使用Java.我设法请求了上传URL,但是每次尝试使用它提交POST请求时,我都会从AWS收到"SignatureDoesNotMatch"错误.

My backend is coded in Spring so I'm using Java. I manage to request the upload URL but every time I try to submit a POST request with it I get back a "SignatureDoesNotMatch" error from AWS.

没有s3,它就可以正常工作...从这个意义上说,文档不是很清楚...从头到尾都没有一个很好的例子,我的意思是,整个过程的例子:身份验证,网址请求和视频上传.因此,我在努力了解这里存在的问题.

Without s3 it works just fine... The documentation is not that clear in this sense... There isn't a well done example from the beginning to the end, I mean, an example with the whole process: authentication, url request and upload of the video. So I'm struggling trying to understand what's the problem here.

使用这种方法,我可以获得更新网址.

With this method I get the update url.

public String createVideo(String title, String description, String username) throws UnsupportedEncodingException {
        Map<String, String> params = new HashMap<>();
        String nonce = generateNonce(8);
        String timestamp = Long.toString(System.currentTimeMillis() / 1000L);
        params.put("api_format", "json");
        params.put("author", username);
        params.put("title", title);
        params.put("description", description);
        params.put("upload_method", "s3");
        params.put("api_key", jwPlayerConfig.getKey());
        params.put("api_nonce", nonce);
        params.put("api_timestamp", timestamp);
        params.put("api_signature", generateAPISignature(params, jwPlayerConfig.getSecret()));
        String urlParameters = getParamsString(params);
        String response = requestAuthenticationToken(jwPlayerConfig.getUrl() + jwPlayerConfig.getCreateVideoUrl(), urlParameters);
        JSONObject myObject = new JSONObject(response.toString());
        System.out.println(myObject);
        JSONObject link = myObject.getJSONObject("link");
        return "https://" + link.getString("address") + link.getString("path") +
                "?api_format=json" +
                "&redirect_address=" + jwPlayerConfig.getCreateVideoRedirectAddress() +
                "&key_in_path=True" +
                "&AWSAccessKeyId=" + link.getJSONObject("query").getString("AWSAccessKeyId") +
                "&Expires=" + link.getJSONObject("query").get("Expires").toString() +
                "&Signature=" + link.getJSONObject("query").getString("Signature");
    }

此方法由Spring的控制器方法调用,并且它使用以下其他方法来生成上传网址:

This method is called by a controller method of Spring and it uses these other methods in order to generate the upload url:

public String generateAPISignature(Map<String, String> params, String api_secret){
        final Map<String, String> sorted = params.entrySet()
                .stream()
                .sorted(Comparator.comparing(Map.Entry::getKey))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
        StringBuilder concat = new StringBuilder();
        for(String key: sorted.keySet()){
            concat.append(key);
            concat.append("=");
            concat.append(sorted.get(key));
            concat.append("&");
        }
        concat.delete(concat.length() - 1, concat.length());
        concat.append(api_secret);
        return DigestUtils.sha1Hex(concat.toString());
    }

此方法会产生一个随机数:

This method generates a nonce:

public static String generateNonce(int length){
        Random rnd = new Random();
        StringBuilder nonce = new StringBuilder();
        do{
            nonce.append(rnd.nextInt(10));
        }while(nonce.length() < length);
        return nonce.toString();
    }

另一个从参数Map构建参数字符串:

And this other one builds the parameters string from the parameters Map:

public String getParamsString(Map<String, String> params) throws UnsupportedEncodingException {
        StringBuilder result = new StringBuilder();

        for (Map.Entry<String, String> entry : params.entrySet()) {
            result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
            result.append("&");
        }

        String resultString = result.toString();

        return resultString.length() > 0
                ? resultString.substring(0, resultString.length() - 1)
                : resultString;
    }

html表单如下:

<form method="POST" action="url">
    <input type="file" name="file" id="file"/>
    <button type="submit">Upload video</button>
</form>

我收到的错误消息如下:

The error message I get is the following:

推荐答案

尝试使用PUT,而不是POST.我们在 https://developer中提供了相关文档. jwplayer.com/jw-platform/reference/v1/s3_uploads.html#uploading-file

Try with a PUT, not a POST. We have documentation for this at https://developer.jwplayer.com/jw-platform/reference/v1/s3_uploads.html#uploading-file

这篇关于JWPlayer和S3:计算得出的签名与提供的签名不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 19:22