Picked up the code for NodeJS server to serve the token to my flutter app by hosting it online on Heroku, from the official link provided by Agora for supporting servers: https://github.com/AgoraIO-Community/Agora-Node-TokenServer and ran it after adding my APP ID and APP CERTIFICATE. but this error shows up when I try to use the URLs that are provided in the documentation like: http://localhost:8080/rtc/test/publisher/uid/1
Something changed after using this: http://localhost:8080/rtc/:test/:publisher/uid/1 and the out became // 20220212032023 // http://localhost:8080/rtc/:test/:publisher/uid/1
{
"error": "role is incorrect"
}
even though the role is correct. I even tried audience as the role but the output didn't change. And the ping: http://localhost:8080/ping works just fine and produces this output: // 20220212032146 // http://localhost:8080/ping
{
"message": "pong"
}
Complete error is:
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined
at new NodeError (node:internal/errors:371:5)
at Function.from (node:buffer:322:9)
at AccessToken.build (C:\Users\HP\Desktop\Agora-Node-TokenServer\node_modules\agora-access-token\src\AccessToken.js:31:21)
at Function.buildTokenWithAccount (C:\Users\HP\Desktop\Agora-Node-TokenServer\node_modules\agora-access-token\src\RtcTokenBuilder.js:70:25)
at Function.buildTokenWithUid (C:\Users\HP\Desktop\Agora-Node-TokenServer\node_modules\agora-access-token\src\RtcTokenBuilder.js:40:21)
at generateRTCToken (C:\Users\HP\Desktop\Agora-Node-TokenServer\index.js:59:29)
at Layer.handle [as handle_request] (C:\Users\HP\Desktop\Agora-Node-TokenServer\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\HP\Desktop\Agora-Node-TokenServer\node_modules\express\lib\router\route.js:137:13)
at nocache (C:\Users\HP\Desktop\Agora-Node-TokenServer\index.js:15:3)
at Layer.handle [as handle_request] (C:\Users\HP\Desktop\Agora-Node-TokenServer\node_modules\express\lib\router\layer.js:95:5)
I used the following index.js code:
const express = require('express');
const dotenv = require('dotenv');
const {RtcTokenBuilder, RtcRole, RtmTokenBuilder, RtmRole} = require('agora-access-token');
dotenv.config();
const app = express();
const PORT = process.env.PORT || 8080;
const APP_ID = process.env.APP_ID;
const APP_CERTIFICATE = process.env.APP_CERTIFICATE;
const nocache = (_, resp, next) => {
resp.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
resp.header('Expires', '-1');
resp.header('Pragma', 'no-cache');
next();
}
const ping = (req, resp) => {
resp.send({message: 'pong'});
}
const generateRTCToken = (req, resp) => {
// set response header
resp.header('Acess-Control-Allow-Origin', '*');
// get channel name
const channelName = req.params.channel;
if (!channelName) {
return resp.status(500).json({ 'error': 'channel is required' });
}
// get uid
let uid = req.params.uid;
if(!uid || uid === '') {
return resp.status(500).json({ 'error': 'uid is required' });
}
// get role
let role;
if (req.params.role === 'publisher') {
role = RtcRole.PUBLISHER;
} else if (req.params.role === 'audience') {
role = RtcRole.SUBSCRIBER
} else {
return resp.status(500).json({ 'error': 'role is incorrect' });
}
// get the expire time
let expireTime = req.query.expiry;
if (!expireTime || expireTime === '') {
expireTime = 3600;
} else {
expireTime = parseInt(expireTime, 10);
}
// calculate privilege expire time
const currentTime = Math.floor(Date.now() / 1000);
const privilegeExpireTime = currentTime + expireTime;
// build the token
let token;
if (req.params.tokentype === 'userAccount') {
token = RtcTokenBuilder.buildTokenWithAccount(APP_ID, APP_CERTIFICATE, channelName, uid, role, privilegeExpireTime);
} else if (req.params.tokentype === 'uid') {
token = RtcTokenBuilder.buildTokenWithUid(APP_ID, APP_CERTIFICATE, channelName, uid, role, privilegeExpireTime);
} else {
return resp.status(500).json({ 'error': 'token type is invalid' });
}
// return the token
return resp.json({ 'rtcToken': token });
}
const generateRTMToken = (req, resp) => {
// set response header
resp.header('Acess-Control-Allow-Origin', '*');
// get uid
let uid = req.params.uid;
if(!uid || uid === '') {
return resp.status(500).json({ 'error': 'uid is required' });
}
// get role
let role = RtmRole.Rtm_User;
// get the expire time
let expireTime = req.query.expiry;
if (!expireTime || expireTime === '') {
expireTime = 3600;
} else {
expireTime = parseInt(expireTime, 10);
}
// calculate privilege expire time
const currentTime = Math.floor(Date.now() / 1000);
const privilegeExpireTime = currentTime + expireTime;
// build the token
console.log(APP_ID, APP_CERTIFICATE, uid, role, privilegeExpireTime)
const token = RtmTokenBuilder.buildToken(APP_ID, APP_CERTIFICATE, uid, role, privilegeExpireTime);
// return the token
return resp.json({ 'rtmToken': token });
}
const generateRTEToken = (req, resp) => {
// set response header
resp.header('Acess-Control-Allow-Origin', '*');
// get channel name
const channelName = req.params.channel;
if (!channelName) {
return resp.status(500).json({ 'error': 'channel is required' });
}
// get uid
let uid = req.params.uid;
if(!uid || uid === '') {
return resp.status(500).json({ 'error': 'uid is required' });
}
// get role
let role;
if (req.params.role === 'publisher') {
role = RtcRole.PUBLISHER;
} else if (req.params.role === 'audience') {
role = RtcRole.SUBSCRIBER
} else {
return resp.status(500).json({ 'error': 'role is incorrect' });
}
// get the expire time
let expireTime = req.query.expiry;
if (!expireTime || expireTime === '') {
expireTime = 3600;
} else {
expireTime = parseInt(expireTime, 10);
}
// calculate privilege expire time
const currentTime = Math.floor(Date.now() / 1000);
const privilegeExpireTime = currentTime + expireTime;
// build the token
const rtcToken = RtcTokenBuilder.buildTokenWithUid(APP_ID, APP_CERTIFICATE, channelName, uid, role, privilegeExpireTime);
const rtmToken = RtmTokenBuilder.buildToken(APP_ID, APP_CERTIFICATE, uid, role, privilegeExpireTime);
// return the token
return resp.json({ 'rtcToken': rtcToken, 'rtmToken': rtmToken });
}
// app.get('/access_token', nocache , generateRTCToken)
app.get('/ping', nocache, ping)
app.get('/rtc/:channel/:role/:tokentype/:uid', nocache , generateRTCToken);
app.get('/rtm/:uid/', nocache , generateRTMToken);
app.get('/rte/:channel/:role/:tokentype/:uid', nocache , generateRTEToken);
app.listen(PORT, () => {
console.log(`Listening on port: ${PORT}`);
});
The format that you're following is incorrect. According to the README in the given repo, if you want to generate a RTC token this the schema:
/rtc/:channelName/:role/:tokentype/:uid/?expiry=
In the URL that you shared it seems that you're adding the UID directly after the user role.
You should try specifying the tokenType to either UID or User Account first and then enter the UID.
You can read more here: https://github.com/AgoraIO-Community/Agora-Node-TokenServer#rtc-token
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.