Skip to main content

Token generators

Authentication is the act of validating the identity of each user before they access a system. In Agora SD-RTN™, this key level of security is implemented in the form of token authentication. Agora SD-RTN™ uses digital tokens to authenticate users and their privileges before they can access Broadcast Streaming. A token is a dynamic key that is generated from a given set of inputs. All Agora core products are protected using token security.

In order to build a seamless authentication process for your users, you need to integrate token generation into your identity management system. This guide helps you integrate Agora token generation libraries into your authentication system. The integration of Agora token generation and authentication in your security infrastructure provides an additional level of security while allowing you to retain control of the overall authentication process.

Understand the tech

When a user attempts to connect to an Agora channel, your app requests a token from your authentication system. Your authentication system uses Agora libraries to generate a token specific to the channel, then sends it to your app. The app then sends this token to Agora SD-RTN™ with the request to join a channel. Agora SD-RTN™ validates the token and adds the user to the channel. The following diagram shows the authentication call flow between your app, your authentication system and Agora SD-RTN™:

Integrated token generation


To follow this procedure you must have created:

  • Android Studio 4.1 or higher.
  • Android SDK API Level 24 or higher.
  • A mobile device that runs Android 4.1 or higher.
  • An Agora account and project.

  • A computer with Internet access.

    Ensure that no firewall is blocking your network communication.

Project setup

Open the project in which you wish to integrate token generation.

Integrate token generation into your authentication system

When a user sends an authentication request to your server, the server must check the user's credentials against your internal security and business logic. If everything is in order, the server returns an Agora authentication token to the app. The app then uses this token to join a channel.

This section shows you how to integrate Agora token generation into your authentication system code.

Handle the system logic

  1. Download the Agora tools Git repository

    To call the token generation methods, you need to add Agora token generation code to your project. Clone the Agora IO tools repository to your development device using the following command:

    git clone <your download directory>

  2. Add the Agora dynamic key code to your project

    Copy the folder Tools/DynamicKey/AgoraDynamicKey/java/src/main/java/io from the downloaded repository to the root folder of your Java project.

    cp -r <your download directory>/Tools/DynamicKey/AgoraDynamicKey/java/src/main/java/io <your project folder>

  3. Add the encoding library to your project

    For encoding tokens, Agora token builder classes use the Apache Commons Codec library. To add this library to your project, download the Apache Commons Codec binaries and add the comons-codec-x.xx.jar file to the lib folder of your project. For example:

    curl -LO && ( tar xvf commons-codec-1.15-bin.tar.gz && cp commons-codec-1.15/commons-codec-1.15.jar <your project folder>/lib/ && rm -rf commons-codec*)

Implement token generation

Agora SD-RTN™ uses integer user Ids for token generation. For smooth communication, all users in a channel must use an integer uid. To call the uid token generation method, take the following steps:

  1. Import the Agora token builder classes into your project

    Create a new file named in your project folder. Add the following import statements to the file:


  2. Generate a uid token

To generate tokens based on user ID, replace the main class of your app with the following token generation code:

public class App {
static String appId = "Your app Id";
static String appCertificate = "Your app certificate";
static String channelName = "Your channel name";
static int uid = 0; // The integer uid, required for an RTC token
static int expirationTimeInSeconds = 3600; // The time after which the token expires
public static void main(String[] args) throws Exception {
RtcTokenBuilder2 tokenBuilder = new RtcTokenBuilder2();
// Calculate the time expiry timestamp
int timestamp = (int)(System.currentTimeMillis() / 1000 + expirationTimeInSeconds);
System.out.println("UID token");
String result = tokenBuilder.buildTokenWithUid(appId, appCertificate,
channelName, uid, Role.ROLE_PUBLISHER, timestamp, timestamp);

Test your implementation

To test the validity of tokens you generate through your integrated authentication system, take the following steps:

  1. In the sample code, set the variables appId, and appCertificate to values from Agora Console.

  2. Set channelName to the name of the channel to join and expirationTimeInSeconds to the validity duration of the token in seconds.

  3. For the purpose of this test, set uid equal to zero.

  4. Build and run the project.

    The code generates a Broadcast Streaming and prints it to the terminal.

  5. Use the token to connect to a Broadcast Streaming channel.

    1. In your browser, navigate to the Agora web demo.

    2. Fill in the App ID, and Channel with the same values that you used to call the token generation method.

    3. Copy the Broadcast Streaming uid authentication token from the terminal and paste it into the Token box.

    4. Press Join to connect.

      You see the local video on the screen.

  6. Connect to the same channel from another device to confirm that everything works.


This section contains information that completes the information in this page, or points you to documentation that explains other aspects to this product.

You have seen that you generate Agora tokens with a few lines of code. Now integrate the code into your identity management system. Have a look at: