This page provides Agora RTC SDK v2.1+, Agora Web SDK v2.4+, Agora Recording SDK v2.1+, and Agora RTSA SDK users with a quick guide on generating a sample token using the RtcTokenBuilderSample demos we provide, as well as token-generating API references in Go.
Your token needs to be generated on your own server, hence you are required to first deploy a token generator on the server. In our GitHub Repository, we provide source codes and token generator demos in C++, Java, Python, PHP, Ruby, Node.js, Go, C# and other programming languages.
The ./<language>/src folder of each language holds source codes for generating different types of dynamic keys and tokens. Note that both AccessToken and SimpleTokenBuilder can generate a token for the following SDKs:
However, we recommend using RtcTokenBuilder instead of AccessToken. AccessToken implements all the core algorithms for generating a token, while RtcTokenBuilder is a wrapper of AccessToken and provides much more simplified Interfaces.
The ./<language>/sample folder of each language holds token generator demos we create for demonstration purposes. RtcTokenBuilderSample is a demo for generating a token for the Agora RTC SDK, Agora Web SDK, Agora Recording SDK or Agora RTSA SDK. You can customize it based on your real business needs.
We take RtcTokenBuilderSample.go as an example:
Our demo provides sample-App ID, appCertificate, channelName, uid, and userAccount for demonstration purposes.
result, err = rtctokenbuilder.BuildTokenWithUserAccount(appID, appCertificate, channelName, uidStr, rtctokenbuilder.RoleAttendee, expireTimestamp)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("Token with userAccount: %s\n", result)
}
result, err := rtctokenbuilder.BuildTokenWithUID(appID, appCertificate, channelName, uid, rtctokenbuilder.RoleAttendee, expireTimestamp)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("Token with uid: %s\n", result)
}
Skip this step if you just want to take a quick look at how a token is generated.
go build
Source code: ../go/src/RtcTokenBuilder/RtcTokenBuilder.go
You can create your own token generator using the public methods that RtcTokenBuilder.go provides. Note that RtcTokenBuilder.go supports both int uid and string userAccount. Ensure that you choose the right method.
func BuildTokenWithUID(appID string, appCertificate string, channelName string, uid uint32, role Role, privilegeExpiredTs uint32) (string, error)
This method builds a token with your int uid.
Parameter | Description |
---|---|
appID |
The App ID issued to you by Agora. Apply for a new App ID from Agora Console if it is missing from your kit. See Get an App ID. |
appCertificate |
Certificate of the application that you registered in the Agora Console. See Get an App Certificate. |
channelName |
Unique channel name for the AgoraRTC session in the string format. The string length must be less than 64 bytes. Supported character scopes are: |
uid |
User ID. A 32-bit unsigned integer with a value ranging from 1 to (232-1). Must be unique. |
role |
The user role. Both roles enjoy the same privilege. Role_Publisher = 1 : (Recommended) A host.Role_Subscriber = 2 : An audience. |
privilegeExpiredTs |
Expiration time of the token. A Unix timestamp, represented by the number of seconds elapsed since 1/1/1970. For example, if you set this parameter as the current timestamp + 600 (seconds), the token expires 10 minutes after it is generated. A token is valid for 24 hours at most. If you set this parameter as 0 or a period longer than 24 hours, the token is valid for 24 hours. |
func BuildTokenWithUserAccount(appID string, appCertificate string, channelName string, userAccount string, role Role, privilegeExpiredTs uint32) (string, error)
This method builds a token with your string userAccount.
Parameter | Description |
---|---|
appID |
The App ID issued to you by Agora. Apply for a new App ID from Agora Console if it is missing from your kit. See Get an App ID. |
appCertificate |
Certificate of the application that you registered in the Agora Console. See Get an App Certificate. |
channelName |
Unique channel name for the AgoraRTC session in the string format. The string length must be less than 64 bytes. Supported character scopes are: |
userAccount |
The user account. The maximum length of this parameter is 255 bytes. Ensure that you set this parameter and do not set it as null. Supported character scopes are: |
role |
The user role. Both roles enjoy the same privilege. Role_Publisher = 1 : (Recommended) A host.Role_Subscriber = 2 : An audience. |
privilegeExpiredTs |
Expiration time of the token. A Unix timestamp, represented by the number of seconds elapsed since 1/1/1970. For example, if you set this parameter as the current timestamp + 600 (seconds), the token expires 10 minutes after it is generated. A token is valid for 24 hours at most. If you set this parameter as 0 or a period longer than 24 hours, the token is valid for 24 hours. |
When generating a token, you can also refer to the following articles: