Using Oauth to receive a token

Trying to setup a query to PureCloud api using OAuth. Can't figure out how to turn this nodejs tutorial (below) into something NodeRed can understand and use. Any advice?

var http = require("http");

var express = require('express');
var app = express();
var uuidv4 = require('uuid/v4');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var request = require('request');

var client_id = 'efb6698a-5f76-4f90-bf29-9a69a3555602';
var client_secret = '3pDfUuU1h8nK5XZigBp2ogc1GZkII4KvJKBmKqEGnt0';

var authvalidation = function(req, res, next) {
    console.log('\n['+req.method+' '+req.url+']');
    //if we don't have a session then redirect them to the login page
    if((req.cookies && !(req.cookies.session && sessionMap[req.cookies.session])) &&
            req.url.indexOf("oauth") == -1){
        //redirect the user to authorize with purecloud
        var redirectUri = "" +
                    "response_type=code" +
                    "&client_id=" + client_id +

        console.log("redirecting to " + redirectUri);


    //if we do have a session, just pass along to the next http handler
    console.log("have session")


var sessionMap ={};

app.get("/", function(req, res){

//this route handles the oauth callback
app.get("/oauth2/callback", function(req,res){
    //the authorization page has called this callback and now we need to get the bearer token
    console.log("oauth callback")
    var authCode = req.query.code;

    var tokenFormData = {
        grant_type: "authorization_code",
        code: authCode, //from the query string parameters sent to this url
        redirect_uri : "http://localhost:8085/oauth2/callback"

    var postData = {
        form: tokenFormData,
        auth: { //basic auth here
            user: client_id,
            pass: client_secret

    //post back to /oauth/token with the client id and secret as well as the auth code that was sent to us., function(err,httpResponse,body){
        console.log("got token data back: ")

        var tokenResponse = JSON.parse(body);

        var sessionId = uuidv4();

        //store the session id as a key in the session map, the value is the bearer token for purecloud.
        //we want to keep that secure so won't send that back to the client
        sessionMap[sessionId] = tokenResponse.access_token;

        //send the session id back as a cookie
        res.cookie('session', sessionId);


//wrap up the api/v2/users/me call inside a /me route
app.get("/me", function(req, res){
    //get the session from map using the cookie
    var oauthId = sessionMap[req.cookies.session];

    var getData = {
        auth: {
            bearer: oauthId

    request.get(getData, function (e, r, user) {
        console.log("Got response for /users/me");

var httpServer = http.createServer(app);

The best way to go about something like this is to first analyze the JavaScript. Try to see the flow in it and try to understand what is basically happening. Once you get that, it shouldn't be too difficult to translate it into a node red equivalent.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.