William
William

Reputation: 4588

How to authenticate LinkedIn using Node.js and simple-oauth2

I am writing a node.js app to authenticate with LinkedIn and it isn't working. The problem is that I am redirecting to (what appears to be) the correct URL, but instead of being forwarded to a page that queries the user to authorize their credentials, I get a "page not found" message.

I have created a LinkedIn "App". Below are my "authorized redirect URLs":

enter image description here

HTML

  <div id="root">
        <button id="auth-button"> Login </button>
  </div>

Client JS

function onSignInButtonClick() {
  // Open the Auth flow in a popup.
  window.open('/redirect', 'firebaseAuth', 'height=315,width=400');
};

var button = document.getElementById("auth-button");

button.addEventListener("click",function(){
    onSignInButtonClick();
});

Server code

const credentials = {
 client: {
   id: "LINKEDIN_CLIENT_ID-1-2-3-4", 
   secret: "LINKEDIN_CLIENT_SECRET-1-2-3-4", 
 },
 auth: {
   tokenHost: 'https://www.linkedin.com/oauth/v2/authorization'
 }
};


const oauth2 = require('simple-oauth2').create(credentials);

var express = require("express");
var app = express();
app.use(express.static("public"));


app.get('/', (req, res) => {

   res.sendFile('landing.html',{
      root:'public'
   })

});




app.get('/redirect', (req, res) => {

  const redirectUri = oauth2.authorizationCode.authorizeURL({
    response_type:"code",
    redirect_uri: "http://www.localhost:3000/callback",
    state: "some-cryptic-stuff-98471871987981247"
  });

  res.redirect(redirectUri);
});


app.get('/callback',(req, res) => {
  console.log("linkedin-callback route invoked");

  res.send("linked in callback working")

});


app.listen(3000, function(err) {
    console.log('Server works');
});

When the user clicks the button they are redirected to a URL that is identical in structure to the one that is given as a "sample call" (below) in the LinkedIn developer reference.

https://developer.linkedin.com/docs/oauth2#

enter image description here

However instead of seeing the prompt in the image above, my code gives them this:

enter image description here

Upvotes: 2

Views: 3172

Answers (1)

RrR-
RrR-

Reputation: 1410

The redirect_uri you have registered in LinkedIn (http://localhost:3000/callback) is different to what you are actually sending (http://www.localhost:3000/callback). This might be the issue as it causes an invalid redirect_uri error.

Upvotes: 3

Related Questions