腾讯云视频接口restTemplate签名错误 4100
调用腾讯云视频接口,生成的rul为: https://vod.api.qcloud.com/v2/index.php?Action=GetVideoInfo&Nonce=26745403&Region=bj&SecretId=xxxZT6SXLs&Signature=SKKFCf3HIOOHP%2BbaaP1Bsie2LBw%3D&Timestamp=1626782337&fileId=3701925921231716349
生成的链接用通过浏览器 postman可以请求,通过restTemplate请求会告诉你签名校验失败。 腾讯云文档:https://cloud.tencent.com/document/product/213/11652 坑就在下面框框里面的。 解决办法: 一、不要对url编码
for (String k : params.keySet()) {
url.append(k).append("=").append(params.get(k).toString()).append("&");
}
上面的办法是不要对url编码,虽然能通,但是很不稳定,一会儿能通,一个又签名校验不通过。可使用方法二。
二、还是按文档对url编码,但是不要用restTemplate请求,自定义http请求
public static String sendGetTentcent(String url)
{
StringBuilder result = new StringBuilder();
BufferedReader in = null;
try
{
URL realUrl = new URL(url);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
connection.setRequestProperty("accept", "zh-CN,zh;q=0.9");
connection.setRequestProperty("Host", "vod.api.qcloud.com");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36");
connection.setRequestProperty("Charset", "utf-8");
connection.connect();
in = new BufferedReader(new InputStreamReader(new GZIPInputStream(
connection.getInputStream()), StandardCharsets.UTF_8));
String line;
while ((line = in.readLine()) != null)
{
result.append(line);
}
log.info("recv - {}", result);
}
catch (ConnectException e)
{
// log.error("调用HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e);
}
catch (SocketTimeoutException e)
{
// log.error("调用HttpUtils.sendGet SocketTimeoutException, url=" + url + ",param=" + param, e);
}
catch (IOException e)
{
//log.error("调用HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e);
}
catch (Exception e)
{
// log.error("调用HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e);
}
finally
{
try
{
if (in != null)
{
in.close();
}
}
catch (Exception ex)
{
// log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
}
}
return result.toString();
}
|