GithubHelp home page GithubHelp logo

agoraio-community / agora-node-tokenserver Goto Github PK

View Code? Open in Web Editor NEW
36.0 2.0 79.0 39 KB

A simple token server for Agora applications using NodeJS with Express, and Agora Access Token modules.

License: MIT License

JavaScript 99.37% Shell 0.63%

agora-node-tokenserver's Introduction

Agora Node Token Server

This is an example of a simple Node/Express server that generates tokens for Agora applications.

Deploy on Railway

Deploy to Render

Deploy on Heroku

Run the server

  • Install the dependencies
npm install
  • Create a copy of the .env.example file and save it as .env
  • Add your Agora App ID and App Certificate:
APP_ID=ca123xxxxxx
APP_CERTIFICATE=12za123xxxxxx

You can obtain these values by selecting your project in the Agora console projects section. Optionally, you can also define a port.

  • Start the service
npm start

Endpoints

Ping

endpoint structure

/ping

response:

{"message":"pong"} 

RTC Token

The rtc token endpoint requires a channelName, role ('publisher' or 'audience'), tokentype ('uid' || 'userAccount') and the user's uid (type varies based on tokentype (example: 1000 for uid, ekaansh for userAccount). (optional) Pass an integer to represent the token lifetime in seconds.

endpoint structure

/rtc/:channelName/:role/:tokentype/:uid/?expiry=

response:

{"rtcToken":" "} 

RTM Token

The rtm token endpoint requires the user's uid. (optional) Pass an integer to represent the privelege lifetime in seconds. endpoint structure

/rtm/:uid/?expiry=

response:

{"rtmToken":" "} 

Both Tokens

The rte token endpoint generates both the rtc and rtm tokens with a single request. (optional) Pass an integer to represent the token lifetime in seconds.

endpoint structure

/rte/:channelName/:role/:tokentype/:uid/?expiry=

response:

{
  "rtcToken":" ",
  "rtmToken":" " 
} 

agora-node-tokenserver's People

Contributors

casethree avatar digitallysavvy avatar ekaansharora avatar mgw-sbex avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

agora-node-tokenserver's Issues

Generated RTC & RTM Token not working in RTCClient

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:)

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 undefine

Cloned the repository, replaced APP_ID, and APP_CERTIFICATE with my own. The following error is shown when trying to access the /access_token endpoint: http://localhost:8080/access_token?channelName=testchannel:

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 Function.from (buffer.js:330:9)
    at AccessToken.build (/Users/<omitted>/Agora-Node-TokenServer/node_modules/agora-access-token/src/AccessToken.js:31:21)
    at Function.buildTokenWithAccount (/Users/<omitted>/Agora-Node-TokenServer/node_modules/agora-access-token/src/RtcTokenBuilder.js:70:25)
    at Function.buildTokenWithUid (/Users/<omited>/Agora-Node-TokenServer/node_modules/agora-access-token/src/RtcTokenBuilder.js:40:21)
    at generateAccessToken (/Users/<omitted>/Agora-Node-TokenServer/index.js:47:33)
    at Layer.handle [as handle_request] (/Users/<omitted>/Agora-Node-TokenServer/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/<omitted>/Agora-Node-TokenServer/node_modules/express/lib/router/route.js:137:13)
    at nocache (/Users/<omitted>/Agora-Node-TokenServer/index.js:15:3)
    at Layer.handle [as handle_request] (/Users/<omitted>/Agora-Node-TokenServer/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/<omitted>/Agora-Node-TokenServer/node_modules/express/lib/router/route.js:137:13)

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

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)

Picked up your code and ran it after adding my APP ID and APP CERTIFICATE. but this error shows up when I try to use the URLs that you have provided 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"
}

Can you please help me with this? I shall be very thankful.

RtmTokenBuilder

RtmTokenBuilder.buildToken fails with a crash in gcloud functions.
Rtc token is successfully generated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.