I use the passport.js strategy passport-linkedin-oauth2 to make users of my system log in via linkedin.
They do get to the "sign in" page, where they fill in their email address and password, and "grant access" but when they do, they return to my callback page, where an error is shown: "Cannot fetch user profile".
Navigating to / or /phone works
this is my code:
const express = require('express');
const passport = require('passport');
const plinkedin = require('passport-linkedin-oauth2').Strategy;
const works = path.join(__dirname, 'works.html');
const worksnot = path.join(__dirname, 'worksnot.html');
passport.use(
new plinkedin({
clientID: clientID,
clientSecret: clientSecret,
callbackURL: "http://localhost:4000/auth/linkedin/callback",
profileFields: [
"id",
"positions"
],
scope: ['r_emailaddress', 'r_liteprofile', 'w_member_social'],
passReqToCallback: true
}, function(req, accessToken, refreshToken, profile, done) {
console.log('a');
process.nextTick(function() {
console.log('something is working'); //is not logged
});
}));
const server = express()
.get('/auth/linkedin', passport.authenticate('linkedin', {
state: 'gekkeSTATE'
}), function(req, res) {
console.log('authenticate1')
}) //is not logged
.get('/auth/linkedin/callback', passport.authenticate('linkedin', {
successRedirect: '/works',
failureRedirect: '/worksnot'
}, console.log('authenticate2'))) //is logged first
.get('/works', (req, res) => res.sendFile(works))
.get('/worksnot', (req, res) => res.sendFile(worksnot))
.listen(4000);
console.log(4000) //is logged 2nd
I expected the user being directed to either /works or /worksnot, but instead, the user is directed to http://localhost:4000/auth/linkedin/callback?code=AQSIbPg5hakpi_HnHl1azUGnTASZkLoqUwAP9cL4zqPwKnGNT792KCC9Q52HLbGOAqKSwE20moJy9poijiMLtIpPCEQkN-ld53aigyHb5dUXr3ef3PGjLVsk6nxAqzcOYKt3HN95uJXahiCAXLSWIRO_BecK-EwwY4bOsWm6emLipn4muDnwtGPTfDI1Sg&state=gekkeSTATE, where they see "InternalOAuthError: failed to fetch user profile".
The console output:
authenticate2
4000
InternalOAuthError: failed to fetch user profile
at _oauth2.get (C:\Users\Maarten\iCloudDrive\node_modules\passport-linkedin-oauth2\lib\oauth2.js:62:21)
at passBackControl (C:\Users\Maarten\iCloudDrive\node_modules\oauth\lib\oauth2.js:132:9)
at IncomingMessage.<anonymous> (C:\Users\Maarten\iCloudDrive\node_modules\oauth\lib\oauth2.js:157:7)
at IncomingMessage.emit (events.js:187:15)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
This module is not updated on npm after the Linkedin API had been changed. Its latest version is 3.0.0 that you can grab from Github.
Download the latest version of the library from Github https://github.com/auth0/passport-linkedin-oauth2/releases/tag/v3.0.0 Copy the dev dependencies from its package.json.
{
"ejs": "^2.6.1",
"express": "^3.x.x",
"mocha": "*",
"morgan": "^1.9.1",
"nock": "^13.3.1",
"passport": "^0.4.0",
"should": "^13.2.3"
}
Also include passport-oauth2.
Copy the lib folder with all the files inside it.
Import it in your passport configuration file like this:
const LinkedInStrategy = require('./lib/index.js').Strategy;
Example Usage:
passport.use(new LinkedInStrategy({
clientID: process.env.LINKEDIN_CLIENT_ID,
clientSecret: process.env.LINKEDIN_CLIENT_SECRET,
callbackURL: "/auth/linkedin/callback",
scope: ['openid', 'email', 'w_member_social']
},
async (accessToken, refreshToken, profile, done) => {
// You can access the profile details here
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With