In this step-by-step tutorial, you are going to learn how to authenticate RingCentral platform using password flow in Node JS programming language. Click the "Start" button below to start the tutorial.Start Tutorial
Clone the project from GitHub and install RingCentral Node JS SDK and dependencies.
If you don't know how to create a RingCentral app. Click here for instructions.
Copy the Client id and Client secret and add them to the
Add the account login credentials to the
RC_USERNAME= RC_PASSWORD= RC_EXTENSION=
If you want to specify variables for your production environment, repeat this step for the
Using a RingCentral SDK is the most convenient way to authenticate and access RingCentral platform services.
Let's start by creating a file named
We read the ENVIRONMENT value from the
.env file and load the
For the sandbox environment, we save the data to the
For the production environment, we save the data to the
In your real application, you may want to secure the authentication data in a database or in a safe place as it contains the access token and the refresh token!
To make this tutorial code reusable and extensible, we define the RC_Authentication class and implement a couple utility functions.
First we implement the
get_sdk() function to instantiate the SDK and return the
To instantiate the
sdk, we pass the client id, client secret and the platform environment parameters.
platforminstance from the SDK
Now we implement the
First we call the
get_sdk() function to get the
Then we call the
sdk.platform() to retrieve the
Now we authenticate a user by calling the
platform.login() function, passing the username, extension_number and password.
Upon success, we will write the authentication data to a local file and return the
platform instance to a caller function.
Every time we call the
get_platform() function, we get the
platform instance from the
sdk, read the authentication data we saved locally and call the
platform.auth().setData(tokenObj) function, passing along the authentication data.
Then we call the
platform.loggedIn() function to check if we are still logged in or not. The SDK will automatically validate the access token's and the refresh token's expiration time and return true if the user is still authenticated. Otherwise, the function returns false and we proceed to the login procedure as discussed in the previous step.
Now let's create a file
demo.js and use the
RC_Authentication class as shown in the demo code on the right-hand side.
In this demo, we call the account extension endpoint to read extensions' information and print them out on the console.
$ node demo.js