I'm having following error while authenticating LinkedIn in Passport JS.
{"message":"LinkedIn authentication failed","error":
"Scope "r_emailaddress" is not authorized for your application"}
Here is my code:
passport.use(
new LinkedInStrategy(
{
clientID: process.env.LINKEDIN_CLIENT_ID,
clientSecret: process.env.LINKEDIN_CLIENT_SECRET,
callbackURL: '',
scope: ['r_emailaddress', 'r_liteprofile']
},
async (accessToken, refreshToken, profile, done) => {
try {
let user = await User.findOne({ linkedinId: profile.id });
if (user) {
return done(null, user);
}
user = await User.findOne({ email: profile.emails[0].value });
if (user) {
if (!user.linkedinId) {
user.linkedinId = profile.id;
user.verify = true;
await user.save();
}
return done(null, user);
}
const newUser = new User({
linkedinId: profile.id,
email: profile.emails[0].value,
verify: true,
});
await newUser.save();
done(null, newUser);
} catch (error) {
done(error, null);
}
}
)
);
exports.linkedinAuth = passport.authenticate('linkedin', { scope: ['r_emailaddress', 'r_liteprofile'] });
exports.linkedinAuthCallback = (req, res, next) => {
passport.authenticate('linkedin', { session: false }, async (error, user, info) => {
console.log('user:', user);
if (error) {
console.error('LinkedIn authentication error:', error);
return res.status(400).json({ message: 'LinkedIn authentication failed error', error: error.message });
}
if (!user) {
const message = info ? info.message : 'User already registered with LinkedIn';
return res.redirect(`=${encodeURIComponent(message)}`);
}
try {
const token = jwt.sign({ userId: user._id }, secretKey, { expiresIn: '1h' });
const redirectUrl = `/recommendation?token=${token}&userId=${user._id}`;
res.redirect(redirectUrl);
} catch (jwtError) {
console.error('JWT generation error:', jwtError);
res.status(500).json({ message: 'Internal server error during JWT generation' });
}
})(req, res, next);
};
I have also added product 'Sign In with LinkedIn using OpenID Connect'
in 'My Apps' tab of LinkedIn developer Console. But I'm unable to see permissions tab as the error describes that 'r_emailaddress' isn't authorized. I'm not getting what really the authorization is in this scenario and how to grant it.