JAVA 对接腾讯云直播的实现
程序员文章站
2023-02-20 20:50:54
签名授权public static t tecentdopostjsonv3(string url,string key,string secretid, tecentpublicparams hea...
签名授权
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 腾讯云直播内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
推荐阅读
-
JAVA 对接腾讯云直播的实现
-
Java后台实现将文件上传到阿里云的OSS
-
java实现阿里云上传图片,并解决上传图片文件的强制性下载
-
借助腾讯云的云函数实现一个极简的API网关
-
LiveNVR如何对接LiveQing云平台 - 实现监控摄像头云端直播、录像管理
-
LiveNVR如何对接LiveQing云平台 - 实现监控摄像头云端直播、录像管理
-
使用腾讯云容器服务(TKE)是如何实现应用跨可用区高可用部署的? TKE高可用
-
PHP对接阿里云虚拟号的实现(号码隐私保护)
-
php和腾讯直播的实现代码
-
沁园下的一缕残云 留——痕 通过java反射实现简单的关于MongoDB的对象关系映射(ORM).