阅读 64

腾讯云直播代码 java_JAVA 对接腾讯云直播的实现

腾讯云直播代码 java_JAVA 对接腾讯云直播的实现

签名授权


public static T TecentDoPostJsonV3(String url,String key,String secretId, TecentPublicParams header, String json, OkHttpClient httpClient, Class clazz) throws Exception {

// ************* 步骤 1:拼接规范请求串 *************


String date = DateUtils.format(new Date(Long.valueOf(header.getX_TC_Timestamp() + "000")),DateUtils.FORMAT_SHORT);


String service = url.substring(0,url.indexOf(".")); //截取服务类型 云直播是live


System.out.println(json);


String canonicalRequest ="POST\n" +


"/\n" +


"\n" +


"content-type:application/json; charset=utf-8\n" +


"host:"+url+"\n" +


"\n" +


"content-type;host\n" +


sha256Hex(json);


System.out.println(canonicalRequest);


// ************* 步骤 2:拼接待签名字符串 *************


String credentialScope = date+ "/" + service + "/" + "tc3_request";


String hashedCanonicalRequest = sha256Hex(canonicalRequest);


String algorithm = "TC3-HMAC-SHA256";


String stringToSign = algorithm + "\n" + header.getX_TC_Timestamp() + "\n" + credentialScope + "\n" + hashedCanonicalRequest;


System.out.println(stringToSign);


// ************* 步骤 3:计算签名 *************


byte[] secretDate = hmac256(("TC3" + key).getBytes(CHARSET), date);


byte[] secretService = hmac256(secretDate, service);


byte[] secretSigning = hmac256(secretService, "tc3_request");


String signature = DatatypeConverter.printHexBinary(hmac256(secretSigning, stringToSign)).toLowerCase();


System.out.println(signature);


// ************* 步骤 4:拼接 Authorization *************


String authorization = algorithm + " " + "Credential=" + secretId + "/" + credentialScope + ", "


+ "SignedHeaders=" + "content-type;host" + ", " + "Signature=" + signature;


System.out.println(authorization);


header.setAuthorization(authorization);


JSONObject jsonObject = (JSONObject)JSON.toJSON(header);


RequestBody requestBody = RequestBody.create(CONTENT_TYPE, json);


Request.Builder builder = new Request.Builder();


for (Map.Entry entry:jsonObject.entrySet()) {

if(entry.getValue()==null){

continue;


}


builder.addHeader(String.valueOf(entry.getKey()).replaceAll("_","-"),String.valueOf(entry.getValue()));


}


// 用okhttp3 拼接发送请求


try {

Response response = httpClient.newCall((builder).url("https://"+url).post(requestBody).build()).execute();


String request = response.body().string();


T t = JSON.parseObject(request, clazz);


return t;


} catch (IOException var8) {

throw new ApiProcessException(ErrorCode.HTTP_REQUEST_ERROR, var8.getMessage());


}


}


public static byte[] hmac256(byte[] key, String msg) throws Exception {

Mac mac = Mac.getInstance("HmacSHA256");


SecretKeySpec secretKeySpec = new SecretKeySpec(key, mac.getAlgorithm());


mac.init(secretKeySpec);


return mac.doFinal(msg.getBytes(CHARSET));


}


公共参数实体类


public class TecentPublicParams {

private String X_TC_Action;


private String X_TC_Region;


private String X_TC_Timestamp = Long.toString(System.currentTimeMillis() / 1000);


private String X_TC_Version;


private String Authorization;


private String X_TC_Language = "zh-CN";


private String X_TC_Token;


public String getX_TC_Action() {

return X_TC_Action;


}


public void setX_TC_Action(String x_TC_Action) {

X_TC_Action = x_TC_Action;


}


public String getX_TC_Region() {

return X_TC_Region;


}


public void setX_TC_Region(String x_TC_Region) {

X_TC_Region = x_TC_Region;


}


public String getX_TC_Timestamp() {

return X_TC_Timestamp;


}


public void setX_TC_Timestamp(String x_TC_Timestamp) {

X_TC_Timestamp = x_TC_Timestamp;


}


public String getX_TC_Version() {

return X_TC_Version;


}


public void setX_TC_Version(String x_TC_Version) {

X_TC_Version = x_TC_Version;


}


public String getAuthorization() {

return Authorization;


}


public void setAuthorization(String authorization) {

Authorization = authorization;


}


public String getX_TC_Language() {

return X_TC_Language;


}


public void setX_TC_Language(String x_TC_Language) {

X_TC_Language = x_TC_Language;


}


public String getX_TC_Token() {

return X_TC_Token;


}


public void setX_TC_Token(String x_TC_Token) {

X_TC_Token = x_TC_Token;


}


}


调用方法


String tencentLiveApi = "live.tencentcloudapi.com";


String SECRET_KEY = "***"; //此处填写自己腾讯云key


String SECRET_ID = "***"; //此处填写自己腾讯云密匙


TecentPublicParams tecentPublicParams = new TecentPublicParams();


tecentPublicParams.setX_TC_Action("DescribeLiveStreamPublishedList"); //对应方法名


tecentPublicParams.setX_TC_Version("2018-08-01");


TreeMap keyValues = new TreeMap(); //参数


keyValues.put("DomainName","***");


keyValues.put("EndTime", DateUtils.format(new Date(),"yyyy-MM-dd'T'HH:mm:ss'Z'"));


keyValues.put("StartTime", DateUtils.format(DateUtils.addDay(new Date(),-30),"yyyy-MM-dd'T'HH:mm:ss'Z'"));


String back ="";


try {

back = LiveUtils.TecentDoPostJsonV3(tencentLiveApi,SECRET_KEY,SECRET_ID,tecentPublicParams,JSON.toJSONString(keyValues),httpClient,String.class);


} catch (Exception e) {

e.printStackTrace();


}


到此这篇关于JAVA 对接腾讯云直播的实现的文章就介绍到这了,更多相关JAVA 腾讯云直播内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

————————————————

版权声明:本文为CSDN博主「xinhe sun」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_34536454/article/details/114657464


文章分类
后端
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 gxwowoo@163.com 举报,一经查实,本站将立刻删除。
相关推荐