I’m trying to implement LinkedIn authentication in my Node.js application using Passport.js and the passport-linkedin-oauth2 package. After a successful login redirect, I’m encountering the following error:
InternalOAuthError: failed to fetch user profile
at Strategy.<anonymous> (C:UsersjahedMdShayemurRahman-github02-thesisbackendnode_modulespassport-linkedin-oauth2liboauth2.js:57:19)
at passBackControl (C:UsersjahedMdShayemurRahman-github02-thesisbackendnode_modulesoauthliboauth2.js:132:9)
at IncomingMessage.<anonymous> (C:UsersjahedMdShayemurRahman-github02-thesisbackendnode_modulesoauthliboauth2.js:157:7)
at IncomingMessage.emit (node:events:526:35)
at endReadableNT (node:internal/streams/readable:1408:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
I’ve double-checked that my clientID
, clientSecret
, and callbackURL
match exactly with the ones from LinkedIn Developer Platform. I’ve also ensured the LinkedIn account has granted the necessary permissions.
ScreenShot LinkedIn Developer Platform
Here’s the relevant code snippet:
import express from 'express';
import cors from 'cors';
import passport from 'passport';
import session from 'express-session';
import { Strategy as LinkedInStrategy } from 'passport-linkedin-oauth2';
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
secure: false,
},
})
);
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
done(null, user);
});
passport.use(
new LinkedInStrategy(
{
clientID: process.env.LINKEDIN_CLIENT_ID,
clientSecret: process.env.LINKEDIN_CLIENT_SECRET,
callbackURL: process.env.LINKEDIN_CALLBACK_URL,
scope: ['openid', 'profile', 'w_member_social', 'email'],
},
(accessToken, refreshToken, profile, done) => {
return done(null, profile);
}
)
);
app.get('/', (req, res) => {
res.send(
`<center style="font-size:140%"> <p>LOGIN </p>
<img style="cursor:pointer;" onclick="window.location='/auth/linkedin'" src="https://bkpandey.com/wp-content/uploads/2017/09/linkedinlogin.png"/>
</center>
`
);
});
app.get('/auth/linkedin', passport.authenticate('linkedin'));
app.get(
'/auth/linkedin/callback',
passport.authenticate('linkedin', {
successRedirect: '/profile',
failureRedirect: '/',
})
);
// Route to handle successful authentication
app.get('/profile', (req, res) => {
res.send('You are authenticated with LinkedIn!');
});
export default app;
I’ve tried restarting my server and testing with a different network, but the issue persists. What could be causing this error, and how can I resolve it to successfully fetch the user profile information from LinkedIn?
anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.