百家云直播录播代码实例
作者:互联网
https://dev.baijiayun.com/wiki/detail/4#-h5-28 官方文档路径
最近在做百家云直播和录播相关的内容,发现官网并没有java代码的实例,在小二的探索下,奉上以下讲解和代码
排序代码实例
public String doPostBy(Map<String, Object> map) throws Exception {
// 获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 参数
StringBuffer params = new StringBuffer();
try {
System.out.println("排序前:" + map);
map = SortUtil.sortByKey(map);
StringBuffer param = new StringBuffer();
Set<Map.Entry<String, Object>> entries = map.entrySet();
for (Map.Entry entry : entries) {
param.append(entry.getKey() + "=" + entry.getValue() + "&");
}
param.substring(0, param.length() - 1);
String sign = getSign(param, partnerKey);
System.out.println(sign);
map.put("sign", sign);
System.out.println("排序前:" + map);
map = SortUtil.sortByKey(map);
System.out.println("排序后" + map);
Set<Map.Entry<String, Object>> entrie = map.entrySet();
for (Map.Entry entry : entrie) {
params.append(entry.getKey() + "=" + URLEncoder.encode(String.valueOf(entry.getValue())) + "&");
}
String data = params.substring(0, params.length() - 1);
System.out.println(data);
return data;
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
return null;
}
如图所示,调用百家云接口文档需按照官网接口文档要求的参数进行拼接,必填的参数需要都有。
调用第三方请求代码如下
public static String doPost(RestTemplate restTemplate, String url, String params, String header) {
String body = null;
try {
HttpHeaders headers = new HttpHeaders();
if (StringUtils.isEmpty(header)){
headers.set("Content-Type","multipart/form-data");
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
}else {
headers.set("Content-Type",header);
headers.setContentType(MediaType.APPLICATION_JSON);
}
HttpEntity<String> httpEntity = new HttpEntity<>(params,headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url,httpEntity,String.class);
body = responseEntity.getBody();
log.info("http请求响应参数{}",body);
} catch (Exception e) {
log.info("http 请求发生异常",e);
}
return body;
}
代码示例:
/**
* 获取转码后视频/音频地址
*
* @param getStatusVO
*/
@Override
public JSONObject getUrl(GetStatusVO getStatusVO) throws Exception {
Map<String, Object> hashMap = new HashMap<>();
hashMap.put("partner_id", partnerId);
hashMap.put("video_id", getStatusVO.getVideoId());
if (StringUtils.isNotBlank(getStatusVO.getFormat())) {
hashMap.put("format", getStatusVO.getFormat());
}
hashMap.put("timestamp", LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
//进行排序加签名
String stringBuffer = doPostBy(hashMap);
//调用第三方接口
String postData = HttpUtil.doPost(restTemplate, getSectionUrl, stringBuffer, null);
System.out.println(postData);
JSONObject json_test = JSONObject.parseObject(postData);
System.out.println(json_test);
return json_test;
}
此接口是获取百家云视频上传后的转码地址,前端拿着这个地址去访问视频
标签:map,String,System,百家,录播,直播,param,println,out 来源: https://www.cnblogs.com/h617019/p/15502710.html