JAVA如何对接腾讯云直播?-创新互联

这篇文章将为大家详细讲解有关JAVA 如何对接腾讯云直播?,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

创新互联公司主要从事网页设计、PC网站建设(电脑版网站建设)、wap网站建设(手机版网站建设)、响应式网站、程序开发、网站优化、微网站、微信小程序开发等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了丰富的成都网站制作、成都网站设计、网站设计、网络营销经验,集策划、开发、设计、营销、管理等多方位专业化运作于一体。

签名授权

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

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文名称:JAVA如何对接腾讯云直播?-创新互联
网站网址:http://scyanting.com/article/djpehh.html