On this page

Using Token Authentication

2026-07-07

Introduction

Authentication is the process of verifying whether a user has permission to access the system, to avoid security risks caused by missing or improper access control. ZEGO authenticates users through Token (including Basic Token and Privilege Token).

Authentication MethodDescriptionUse Cases
Basic TokenDevelopers must include a Token parameter when logging into a room to verify user legitimacy.Basic Token is the fundamental capability of Token, used for simple permission verification scenarios. In most cases, generating this Token is sufficient.
Privilege TokenTo further improve security, Room ID and Stream ID privilege bits are exposed, allowing verification of the Room ID for login and Stream ID for publishing.Common use cases for Room ID and Stream ID privilege bits include:
  • Distinguishing between regular and premium rooms, requiring control over non-premium users entering premium rooms.
  • In voice chat rooms or live streaming shows, ensuring that publishing users match on-mic users to prevent "ghost mic" phenomenon, where users hear audio from someone not on-mic.
  • In speaking games like Werewolf, preventing the app from being cracked by hackers who could log in with other user IDs to the same room and access game information for cheating, affecting normal users' experience.

Prerequisites

Warning
  • Only ZEGO Express SDK version 2.17.0 and later supports Token authentication as described in this document.
  • If you have integrated a ZEGO Express SDK version earlier than 2.17.0 (using AppSign authentication) and want to upgrade to version 2.17.0 with Token authentication, please refer to the How to upgrade from AppSign authentication to Token authentication document for more information about AppSign and Token authentication.

Process Overview

Scenario 1: Login Room Scenario

When using Token authentication, developers need to generate a Token first, then carry the Token to log in to a room. The ZEGO server verifies users with the Token.

The following describes the process using Token to determine whether a user can log in to a room:

  1. The client initiates a Token request.
  2. The Token is generated on the developer's server and returned to the client.
  3. The client carries the obtained Token along with userID and roomID to log in to the corresponding room.
  4. The ZEGO SDK automatically sends the Token to the ZEGO server for verification.
  5. The ZEGO server returns the verification result to the ZEGO SDK.
  6. The ZEGO SDK returns the verification result directly to the client. Clients without permission will fail to log in.

Scenario 2: Login to a Large-Scale Audio/Video Range Scene

When using Token authentication, developers need to generate a Token first, then carry the Token to log in to a scene. The ZEGO server verifies users with the Token.

The following describes the process using Token to determine whether a user can log in to a scene:

  1. The client initiates a Token request.
  2. The Token is generated on the developer's server and returned to the client.
  3. The client carries the obtained Token along with userID and sceneID to log in to the corresponding scene.
  4. The ZEGO SDK automatically sends the Token to the ZEGO server for verification.
  5. The ZEGO server returns the verification result to the ZEGO SDK.
  6. The ZEGO SDK returns the verification result directly to the client. Clients without permission will fail to log in.

Generate and Use Token

This section describes in detail how developers can generate Tokens on the server, how to use Tokens, and how to handle Token expiration.

1 Get AppID and ServerSecret

To generate a Token, you need the unique identifier AppID and the ServerSecret of your project. Please obtain them from the "Project Information" section in Console - Project Management.

After obtaining the AppID and ServerSecret, developers can generate Tokens on their server according to their business needs. The client sends a Token request to the developer's server, and the developer's server generates the Token and returns it to the client.

2 Generate Token on the Server

Warning
  • For your convenience during development and debugging, we provide the ZEGO Token Assistant to generate temporary Tokens.
  • For business security, you must generate Tokens on your app server; Otherwise, there is a risk of ServerSecret being stolen.
  • After generating a Token on your own server, if you need to verify its validity, you can also use the ZEGO Token Assistant for verification.

ZEGO Token Assistant

This tool runs locally in browser and does not send data to server. Do not expose ServerSecret in production, use for debugging only.

ZEGO provides an open-source zego_server_assistant plugin on GitHub/Gitee. Please use the "token04" version in the plugin to generate Tokens. The plugin supports Go, C++, Java, Objective-C, Python, PHP, .NET, and Node.js:

LanguageSupported versionCore functionCode baseSample code
User identity TokenUser privilege Token
GoGo 1.14.15 or laterGenerateToken04
C++C++ 11  or laterGenerateToken04
JavaJava 1.8  or latergenerateToken04
PythonPython 3.6.8  or latergenerate_token04
PHPPHP 7.1 or latergenerateToken04
.NET.NET Framework 3.5  or laterGenerateToken04
Node.jsNode.js 8  or latergenerateToken04

Taking Go as an example, developers can follow these steps to use zego_server_assistant to generate a Token:

  1. Use the command git clone https://github.com/zegoim/zego_server_assistant to obtain the dependency package.
  2. In your code, import the plugin via import "github.com/zegoim/zego_server_assistant/token/go/src/token04".
  3. Call the GenerateToken04 method provided by the plugin to generate a Token.
Warning

The Range Scene module does not yet support Privilege Token.

Warning

When running the Token generation Java source code, if you encounter a "java.security.InvalidKeyException

Key Size" exception, please refer to the related FAQ document for a solution.

3 Use Token

Scenario 1: Login Room Scenario

The user carries the obtained Token along with user and roomID information to log in to the corresponding room through the loginRoom interface.

Warning

The userID used when calling the loginRoom interface to log in must be the same as the userID used when "Generating Token on the Server".

String roomID = "xxx" // The ID of the room to log in to
ZegoUser user = new ZegoUser("xxxx");
ZegoRoomConfig config = new ZegoRoomConfig();
config.token = "xxxxxxxxxx"; // Obtain from the developer's server
engine.loginRoom(roomID, user, config);

If developers need to modify privilege bits after logging in to a room, they can also call the renewToken interface to update the Token. After the update, it will affect the permission for the next room login and stream publishing. Previously successful room logins and stream publishing will not be affected.

String token = getToken(); // Re-obtain Token from the developer's server
engine.renewToken(roomID, token);

Scenario 2: Login to a Large-Scale Audio/Video Range Scene

The user carries the obtained Token along with user and sceneID information to log in to the corresponding scene.

Warning

The userID used when calling the loginScene interface to log in must be the same as the userID used when "Generating Token on the Server".

long sceneID = 123L; // The ID of the scene to log in to
ZegoUser user = new ZegoUser("xxxx");
ZegoSceneParam config = new ZegoSceneParam();
param.sceneID = sceneID;
param.user = user;
param.token = @"xxxxxxxx"; // Obtain from the developer's server

rangeScene.loginScene(param, new IZegoRangeSceneLoginSceneCallback() {
    @Override
    public void onLoginSceneCallback(int errorCode, ZegoSceneConfig config) {
    }
});

4 Token Expiration Handling

Warning

Token expiration may cause issues such as abnormal publishing and playing. Please strictly follow the instructions below to handle expired Tokens promptly.

Scenario 1: Login Room Scenario

30 seconds before Token expiration, the SDK will notify through the onRoomTokenWillExpire callback. After Token expiration, attempting to log in to a room will result in error code 1002078 (Token expired) received through onDebugError or onRoomStateChanged.

After receiving the Token-about-to-expire callback or the Token expired error code, developers need to obtain a new valid Token from their server and call the SDK's renewToken interface to update the Token.

If you have integrated ZEGO Express SDK version 2.17.0 or later, and do not call the renewToken interface to update the Token after it expires, the following behavior will occur when the permission expires:

  • Users who have already logged in will not be kicked out of the room.
  • Currently successful publishing and playing will not be affected. However, after stopping publishing, you will not be able to publish again unless you update the Token.
Note

ZEGO also provides another Token expiration handling mode, which can be configured by contacting ZEGO technical support:

  • Users who have already logged in will be kicked out of the room, and they can only log in again after updating the Token.
  • Currently successful publishing will be stopped.
@Override
public void onRoomTokenWillExpire(String roomID, int remainTimeInSecond){
    String token = getToken(); // Re-obtain Token from the developer's server
    engine.renewToken(roomID, token);
}

Scenario 2: Range Scene Module

30 seconds before Token expiration, the SDK will notify through the onSceneTokenWillExpire callback. After Token expiration, attempting to log in to a room will result in error code 1002078 (Token expired) received through onDebugError or onRoomStateChanged.

After receiving the Token-about-to-expire callback or the Token expired error code, developers need to obtain a new valid Token from their server and call the SDK's renewToken interface to update the Token. If not handled, the Token expiration mechanism is as follows:

  • Users who have already logged in will not be kicked out of the scene.
  • Currently successful publishing and playing will not be affected, but the user's next publishing or playing operation will be affected.
public void onSceneTokenWillExpire(ZegoRangeScene rangeScene, int remainTimeInSecond) {
    super.onSceneTokenWillExpire(rangeScene, remainTimeInSecond);
    String token = getToken(); // Re-obtain Token from the developer's server
    // Switch threads before calling renewToken
    rangeScene.renewToken(token);
}

Other

If you are unable to deliver Tokens from the server during development, you can temporarily generate Tokens using client-side code, and complete the integration with the server later.

Warning
  • Do not generate Tokens on the client when your app goes live, otherwise your ServerSecret will be exposed to risk.
  • For security, it is strongly recommended to generate Tokens on the server, otherwise there is a risk of ServerSecret being stolen.

The following table lists the language-specific reference information for generating Tokens on the client using the zego_server_assistant plugin:

LanguageSupported versionCore functionDescription
C++C++ 11 or laterGenerateToken04
JavaJava 1.8 or latergenerateToken04

To generate Tokens on the client, please refer to Use Token.

If the Token expires, please refer to Token Expiration Handling.

API Reference

MethodDescription
loginRoomLogin to a room
renewTokenUpdate Token
onRoomTokenWillExpireToken expiration callback

How to prevent ghost mic or room bombing in audio/video interactions?

Previous

Scenario-based Audio and Video Configuration

Next

Call Quality Monitoring