What to do if the SDK's connect method returns an invalid access token

Cause 1: App Key and token mismatch in your project

Your token doesn’t match the App Key in the SDK. Verify that the locally configured App Key matches the one used to generate the token on the App Server side. Check the token’s validity in the console-nexconn.

Cause 2: Token expired

Set the token’s validity period in the Console. If it’s not set to permanent, request a new token from Chat Server via your app’s server when you get a Token error (31004). Then, reconnect with the new token.

Cause 3: App Key mismatch between project and App Server

The App Key in your project doesn’t match the one on the App Server, causing the token to mismatch the client’s App Key.

Cause 4: App Secret refreshed

Refreshing the App Secret in the Console invalidates all existing tokens. Each user must request a new token to connect.

Pseudocode example

When connect()’s onResult() returns 31004, fetch a new token from your server and reconnect. Here’s the pseudocode:


// With timeout
ConnectParams params = new ConnectParams("user-token");
params.setTimeout(10);  // 10-second timeout
NCEngine.connect(params, new ConnectHandler() {
    @Override
    public void onResult(String userId, NCError error) {
        if (error == null) {
            Log.d("Connect", "Connected: " + userId);
        } else if (error.getCode() == 31004) {
            // Fetch a new token from the App Server and reconnect (pseudocode)
        } else {
            showTip("Connection failed: " + error.getMessage());
        }
    }
});