getChatToken
Updated: 2021-05-18 14:27:41
1. Obtaining userId, appId, and appSecret Values
In the live streaming management backend, click on "Development Settings". On the displayed page, obtain the values for userId, appId, and appSecret.
2. Sign Generation Rules
Arrange all required request parameters in dictionary order by parameter name. Concatenate each parameter name with its corresponding value. Prepend and append the appSecret to the resulting string. Then, compute the MD5 hash, convert the MD5 result to a hexadecimal string in uppercase letters, and use this as the sign.
3. Java Sign Generation Example (For details, see: JAVA Live Streaming API Details)
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
4. PHP Sign Generation Example
1. config.php File Code
<?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. API Request Example
<?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);
?>
