业务接入实时音视频互动服务需要进行Token身份验证。你需要在业务服务器自行生成token,本文介绍如何生成鉴权所需的Token,并提供相关的代码参考。
appid:=int32(12345)
uid:="1234444"
expiresecs:=int32(46)
tk:=sctoken.NewSCToken(appid,uid,expiresecs)
// 设置业务参数
tk.SetParameter("pkey1","pval1")
tk.SetParameter("pkey2","pval2")
// 设置业务权限
tk.SetPrivilege("pri1",300)
tk.SetPrivilege("pri2",400)
// 生成token 串
token:=tk.BuildToken("appkey1234")
// 解析token串,生成SCToken对象
tk,err:=sctoken.ParseToken(token,"appkey1234")
if err!=nil {
// print err
} else {
if tk.IsValid(){
// token 有效
// do something
} else {
// token 已过期
}
}
private static void genToken()
throws UnsupportedEncodingException {
/*
* Initialize tokenBuilder, passing your appKey which can be found on developer center
*/
SCTokenBuilder builder = new SCTokenBuilder( new SCTokenAppSecretProvider() {
@Override
public String getAppsecret( int appKey ) {
// app secret
return "1234";
}
} );
/*
* Parameter:
*
* version: token Version, integer
*
* appid: appKey, integer
*
* uid, business definition, string
*
* validTime, in seconds, value less then 90 will be set to TOKEN_MIN_VALID_TIME(90s)
*
*/
SCTokenPropertyProvider provider = new SCTokenPropertyProvider(2, 1234, "2483549835", 4*60*60);
// Optional Extend parameters,defined by your app
provider.getParameterProperties()
//
.addTokenExtendProperty("param1","content")
//
.addTokenExtendProperty("param2",2);
// Optional privilege properties,defined by your app
provider.getPrivilegeProperties()
//
.addTokenExtendProperty("privilege1",12345566l)
//
.addTokenExtendProperty("privilege2",12345566l);
//Generate token according to above attribute configuration
byte[] token = builder.buildBinanyToken( provider );
// In order to facilitate network transmission,
// BASE 64 encoding and url_safe mode are used
Base64 coder = new Base64( 76, new byte[] {}, true );
// The byte array is converted to a string to print
String tokenStr = coder.encodeAsString( token );
System.out.println( "gen token:"+ tokenStr );
}
private static void validToken(String tokenStr) throws UnsupportedEncodingException {
/*
* Initialize tokenBuilder, passing your appKey which can be found on develop center
*/
SCTokenBuilder builder = new SCTokenBuilder( new SCTokenAppSecretProvider() {
@Override
public String getAppsecret( int appKey ) {
// app secret
return "1234";
}
} );
Base64 coder = new Base64( 76, new byte[] {}, true );
// parse token,make sure your token is url-safed, mean that '+' and '/' MUST NOT be found in your token
if (!tokenStr.equals(URLDecoder.decode(tokenStr, "UTF-8"))) {
throw new SCTokenException("token isn't url-safed");
}
byte[] token = coder.decode(tokenStr.getBytes( "UTF-8" ) );
SCToken scToken = builder.validateTokenBytes( token );
System.out.println( "decode token: " + scToken.toString());
}
app_id = 1296325
app_secret = bytearray(b'abcdefg')
uid = "987654321"
valid_time = 100
build_timestamp = int(time.time() * 1000)
// 设置业务参数
parameter = {"pkey1": "pval1", "pkey2": "pval2"}
// 设置业务权限
privileges = {"pri1": 300, "pri2": 400}
// 生成token 串
token_str = SCToken().gen(app_id, app_secret, uid, parameter, privileges, build_timestamp, valid_time)
// 解析token串,生成SCToken对象
app_secret = bytearray(b'abcdefg')
token_str = "_2dllwAAAHMAADA5AAk5ODc2NTQzMjEAAgAFcGtleTIABXB2YWwyAAVwa2V5MQAFcHZhbDEAAgAEcHJpMQAAAAAAAAEsAARwcmkyAAAAAAAAAZAAAAFsuAVsTAAA6mDjTWxNCdjou_5GSCFCWLtGAgn9Ww"
yt, err = SCToken().parse(token_str, app_secret)
if err != None:
// print "token 解析失败"
else:
if yt.validate():
// print "token 有效"
// do something
else:
//print "token 过期"
语言 | 示例代码 |
---|---|
Golang | 点击下载 |
Java | 点击下载 |
Python | 点击下载 V2.7 点击下载 V3.0 |