AgoraIO-Community / Agora-Node-TokenServer

A simple token server for Agora applications using NodeJS with Express, and Agora Access Token modules.
MIT License
38 stars 82 forks source link

Generated RTC & RTM Token not working in RTCClient #7

Closed GUILLALAB closed 2 years ago

GUILLALAB commented 2 years ago

Hello,

Based on your sample code, I integrated the RTC and RTM token generation in my web application which is served via express.js for server side. I successfully arrive to generate and retrieve the tokens, but when I pass the RTC or RTM in the RTCclient.join channel function, it doesn't work.

1) I used this endpoint with the function generateRTEToken. (I also tried the others functions but result is same)

app.get('/rte/:channel/:role/:tokentype/:uid/', nocache , generateRTEToken);

In my script "webvr-broadcast-client.js", I improved the function JoinChannel() with the token request.

function joinChannel() {
 fetch("https://example.com/rte/web/publisher/uid/44/").then(function(response) {
return response.json();
}).then(function(data) {
token = data.rtcToken; (RTC or RTM are well retrieved)

rtcClient.setClientRole('audience', () => {
  console.log('Client role set to audience');
}, (e) => {
  console.log('setClientRole failed', e);
});

rtcClient.join(token, channelName, 0, (uid) => { 

    console.log('User ' + uid + ' join channel successfully');
    localStreams.uid = uid;
    createBroadcaster(uid);   // Load 3D model with video texture
    createCameraStream(uid);  // Create the camera stream
    joinRTMChannel(uid);      // join the RTM channel
}, (err) => {
    console.log('[ERROR] : join channel failed', err);
});

}).catch(function() {
});
}

The error returned is : "Get server node failed [NO_AUTHORIZED]" – "https://webrtc2-ap-web-2.agoraio.cn/api/v1" – "NO_AUTHORIZED"

I confirm the code structure work, by using a Temp RTC Token it works very well. But when I pass a generated RTC or RTM token as parameter, it doesn't work.

At this stage you can reproduce the scenario by replacing the joinChannel() function and creating a "token" variable ( var token = "";) on top of the file. We will be happy to know how to resolve that:)

EkaanshArora commented 2 years ago

I think you're calling the join method wrong (assuming you're using v4 of the web SDK). This is the function signature:

join(appid: string, channel: string, token: string | null, uid?: UID): Promise<UID>

The first parameter should be your Agora App ID

GUILLALAB commented 2 years ago

I think you're calling the join method wrong (assuming you're using v4 of the web SDK). This is the function signature:

join(appid: string, channel: string, token: string | null, uid?: UID): Promise<UID>

The first parameter should be your Agora App ID

Hello, thanks you for your answer.

I'am using previous version, I confirm you the join function work because I can pass a temp Token. I finally solve the problem. Within the URL fetch request, I try to fetch the uid 44, and in the join function I request the uid 0 (mismatch).

Finally I modified and put the uid 0 in my url request, it works!

Thanks you for your help.

EkaanshArora commented 2 years ago

You're right, the UID mismatch was causing the token issues.

Using 0 as your UIDs isn't the best approach as you wouldn't be able to uniquely identify users joining your channel. This can be a potential issue if you want to kick/ban users or revoke access from your backend later. Since 0 is used as a "wildcard" token - a user with a token for UID 0 can rejoin the channel after their old UID is banned (as the SDK will automatically assign a new UID when using 0 as the UID). This would only be possible until the lifetime of the token.

I'd recommend using some part of the user data as the UID. For example an integer based primary key, if that's an option.