Generate a Token

When accessing to real-time audio/video interaction services, you need to generate a token on the business server for authentication. This section introduces how to generate a token and provides sample codes for reference.

Note: Sample codes on this page are programmed by Golang and applicable to the token generation and verification in the “User Authentication 2.0”.

Golang Sample Codes

  • Generate Token
	appid:=int32(12345)
	uid:="1234444"
	expiresecs:=int32(46)
	tk:=sctoken.NewSCToken(appid,uid,expiresecs)
	// Set service parameters
	tk.SetParameter("pkey1","pval1")
	tk.SetParameter("pkey2","pval2")
	// Set service permissions
	tk.SetPrivilege("pri1",300)
	tk.SetPrivilege("pri2",400)
	// Generate token string
	token:=tk.BuildToken("appkey1234")
  • Verify Token
	// Parse token string to generate SCToken object
	tk,err:=sctoken.ParseToken(token,"appkey1234")
	if err!=nil {
		// print err
	} else {
		   if tk.IsValid(){
			    // token is valid
				// do something
				
			} else {
				//  token expired
			}
	}

Sample Code Downloading

Click to Download

<