gpt4 book ai didi

javascript - JWPlayer 和 S3 : calculated signature does not match the provided one

转载 作者:行者123 更新时间:2023-12-02 09:34:02 36 4
gpt4 key购买 nike

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

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

没有s3,它工作得很好...从这个意义上说,文档不太清楚...从开始到结束没有一个做得很好的示例,我的意思是,整个过程的示例:身份验证、视频的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 的 Controller 方法调用,并使用以下其他方法来生成上传 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());
}

此方法生成一个随机数:

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();
}

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

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>

我收到的错误消息如下:

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

关于javascript - JWPlayer 和 S3 : calculated signature does not match the provided one,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57697391/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com