java调用腾讯云直播api_JAVA 对接腾讯云直播的实现
java调用腾讯云直播api_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();
}
————————————————
版权声明:本文为CSDN博主「傲娇燕儿」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_32884263/article/details/114309494