Get Chat Token
Last updated
Last updated
在直播管理后台,点击开发设置,在显示的页面中获取userId、appId、appSecret值。
将请求所需所有参数按照参数名字典顺序排列,连接参数名与参数值,并在首尾加上appSecret,然后计算MD5,将MD5结果转为16进制、大写字母,作为sign。
String appId = "XXXXXXXX";
String userId = "XXXXXXXX";
String appSecret = "XXXXXXXXXXXXXXXXXXXXXXXX";
long ts = System.currentTimeMillis();
// 创建参数表 (创建接口需要传递的所有参数表)
Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("appId", appId);
paramMap.put("timestamp", Long.toString(ts));
//对参数名进行字典排序
String[] keyArray = paramMap.keySet().toArray(new String[0]);
Arrays.sort(keyArray);
//拼接有序的参数串
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(appSecret);
for (String key : keyArray) {
stringBuilder.append(key).append(paramMap.get(key));
}
stringBuilder.append(appSecret);
String signSource = stringBuilder.toString();
String sign = org.apache.commons.codec.digest.DigestUtils.md5Hex(signSource).toUpperCase();
System.out.println("http://api.polyv.net/live/v1/users/" + userId + "/channels?appId=" + appId + "×tamp=" + ts + "&sign=" + sign);
Copy
1、config.php文件代码
<?php
//签名验证必需参数
$appId = "XXXXXXXXX";
$timestamp = time()*1000;
$appSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
//获取sign函数
function getSign($params){
global $appSecret;
// 1. 对加密数组进行字典排序
foreach ($params as $key=>$value){
$arr[$key] = $key;
}
sort($arr);
$str = $appSecret;
foreach ($arr as $k => $v) {
$str = $str.$arr[$k].$params[$v];
}
$restr = $str.$appSecret;
$sign = strtoupper(md5($restr));
return $sign;
}
?>
Copy
2、接口请求示例
<?php
//引用config.php
include 'config.php';
//接口需要的参数(非sign)赋值
$userId = "XXXXXXXXX";
$params = array(
'appId'=>$appId,
'timestamp'=>$timestamp
);
//生成sign
$sign = getSign($params); //详细查看config.php文件的getSign方法
//接口请求url
$url ="http://api.polyv.net/live/v1/users/".$userId."/channels?appId=".$appId."×tamp=".$timestamp."&sign=".$sign;
//输出接口请求结果
$ch = curl_init() or die ( curl_error() );
curl_setopt( $ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 360);
$response = curl_exec ( $ch );
curl_close ( $ch );
//打印获得的数据
print_r($response);
?>