JWPlayer和S3:计算得出的签名与提供的签名不匹配 [英] JWPlayer and S3: calculated signature does not match the provided one

查看:80
本文介绍了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:

SignatureDoesNotMatch 请求 我们计算出的签名与您提供的签名不匹配. 检查您的密钥和签名方法. AKKSAJBCJBSbXC3NQ帖子 应用程序/x-www-form-urlencoded 1567098922 /jwplatform-upload/Qsafas6vB WWbjcskWBKlc/BLxbm6/RJg57u7M = 50 4f 53 54 0a 0a 61 70 70 6c 69 63 61 74 69 6f 6e 2f 31 2d 77 77 77 2d 66 6f 72 6d 2d 75 72 6c 65 6e 64 6f 64 65 64 0a 31 35 36 37 30 39 38 39 32 32 0a 2f 6a 77 70 6c 23 74 66 6f 72 6d 2d 75 70 6c 6f 61 64 2f 51 72 4e 53 33 36 76 42 8D9AA0A3719CE53F 3x6mnassasaQ2PEFVmc9GZwp0Y7yFS1FtasakDgY39EktjlwX2UsoViikqiE8bDcG6pKB4YPXvsH1Q =

SignatureDoesNotMatch The request signature we calculated does not match the signature you provided. Check your key and signing method. AKKSAJBCJBSbXC3NQ POST application/x-www-form-urlencoded 1567098922 /jwplatform-upload/Qsafas6vB WWbjcskWBKlc/BLxbm6/RJg57u7M= 50 4f 53 54 0a 0a 61 70 70 6c 69 63 61 74 69 6f 6e 2f 31 2d 77 77 77 2d 66 6f 72 6d 2d 75 72 6c 65 6e 64 6f 64 65 64 0a 31 35 36 37 30 39 38 39 32 32 0a 2f 6a 77 70 6c 23 74 66 6f 72 6d 2d 75 70 6c 6f 61 64 2f 51 72 4e 53 33 36 76 42 8D9AA0A3719CE53F 3x6mnassasaQ2PEFVmc9GZwp0Y7yFS1FtasakDgY39EktjlwX2UsoViikqiE8bDcG6pKB4YPXvsH1Q=

推荐答案

尝试使用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:计算得出的签名与提供的签名不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆