说明
请求方通过控制台申请的appKey和appCert生成token用于鉴权,生成的token通过标准http header携带。
header | 值 |
---|---|
X-Auth-Token | Qtt1001bd8a2a727524c07875476ccd7800de0CESlT5ttRucoJ2+ZWNmQZOFeZq3SLRPlt68DAj6ArCmvXQOVr9rgmv////////9/f5LwcxQW |
使用
QttAudio自定义算法生成的token,目前仅支持Java。
可以参考Java项目:https://github.com/qttaudio/token-builder/blob/master/src/test/java/com/qttaudio/token/TokenBuilderHelperTest.java#L20package com.qttaudio.token;
import org.junit.Assert;
import org.junit.Test;
public class TokenBuilderHelperTest {
public void build() throws Exception {
String appKey = "27539780c4f5be815027bf9c864679b4";
String appCert = "031a4f0c1b63e5cffef53e572650a991";
String channel = "88888";
Long uid = 123456L;
// token won't expire
String token = TokenBuilderHelper.build(appKey, appCert, channel, uid);
Assert.assertTrue(TokenVerifier.verify(token, appKey, appCert, channel, uid));
// token with expire time
token = TokenBuilderHelper.build(appKey, appCert, channel, uid, 1L);
Assert.assertTrue(TokenVerifier.verify(token, appKey, appCert, channel, uid));
Thread.sleep(1000);
Assert.assertFalse(TokenVerifier.verify(token, appKey, appCert, channel, uid));
}
}
通过调用TokenBuilderHelper.build方法创建一个token,其函数签名如下:/**
* build builds a token from the specified parameters.
*
* @param appKey The app key assigned by qtt.
* @param appCert The app certificate assigned by qtt.
* @param channelName The channel name to be joined in.
* @param uid The user id. A positive unique integer.
* @param ttl The ttl, e.g. the time to live of the token.
* If it's 0, it won't timeout;
* if it's positive, it lives up to the time in seconds from now on;
* it's not valid for negatives.
* @return The generated token in string.
* @throws Exception
*/
public static String build(String appKey, String appCert, String channelName, Long uid, Long ttl) throws Exception {
checkParams(appKey, appCert, channelName, uid);
Builder builder = TokenBuilderFactory.getBuilder(builderType);
return builder.build(appKey, appCert, channelName, uid, new Strategy(ttl));
}
各参数的含义分别为:
参数名称 | 类型 | 描述 |
---|---|---|
appKey | String | 应用对应的AppKey |
appCert | String | 应用对应的Cert |
channelName | String | 房间名称 |
uid | Long | 用户uid |
ttl | Long | token的超时时间,单位秒,通常这里设置60S即可 |
注意事项
如果调用的API中有明确的uid参数,则生成token时需用明确的uid,若uid不明确,则应将uid设置为0