最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - "Unknown scope "r_email"" Error While Authenticating LinkedIn in Passport

programmeradmin1浏览0评论

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论