passport-linkedin-oauth2没有返回电子邮件地址

时间:2013-11-01 07:11:50

标签: node.js oauth-2.0 linkedin passport.js

这是我的console.log(个人资料);

{ provider: 'linkedin',
  id: 'LJitOAshpU',
  displayName: 'Monist BD',
  name: { familyName: 'BD', givenName: 'Monist' },
  emails: [ { value: undefined } ],
  _raw: '{\n  "firstName": "Monist",\n  "formattedName": "Monist BD",\n  "id": "
LJitOAshpU",\n  "lastName": "BD"\n}',
  _json:
   { firstName: 'Monist',
     formattedName: 'Monist BD',
     id: 'LJitOAshpU',
     lastName: 'BD' } }

这是我的路由代码:

 app.get('/auth/linkedin',passport.authenticate('linkedin', { scope: ['r_emailaddress', 'r_basicprofile', 'rw_nus'],state: 'DCEEFWF45453sdffef424' }));

app.get('/auth/linkedin/callback',passport.authenticate('linkedin', { failureRedirect: '/' }),users.authCallback);

这是passport.js配置:

 passport.use(new LinkedInStrategy({
    clientID: config.linkedIn.clientID,
    clientSecret: config.linkedIn.clientSecret,
    callbackURL: config.linkedIn.callbackURL,
    profileFields: ['id', 'first-name', 'last-name', 'email-address','public-profile-url'],
    passReqToCallback: true
  },
  function(req,token, refreshToken, profile, done) {

    console.log(profile);
}));

为什么我在电子邮件值中未定义?当我使用passport-linkedin

时它起作用了

2 个答案:

答案 0 :(得分:1)

根据readme,必须在scope对象中设置Strategy选项。您在passport.authenticate中将其设置为忽略它。

要解决此问题,请将您的代码更改为:

app.get('/auth/linkedin',passport.authenticate('linkedin', { state: 'DCEEFWF45453sdffef424' }));

app.get('/auth/linkedin/callback',passport.authenticate('linkedin', { failureRedirect: '/' }),users.authCallback);

...

passport.use(new LinkedInStrategy({
    clientID: config.linkedIn.clientID,
    clientSecret: config.linkedIn.clientSecret,
    callbackURL: config.linkedIn.callbackURL,
    scope: ['r_emailaddress', 'r_basicprofile', 'rw_nus'],
    profileFields: ['id', 'first-name', 'last-name', 'email-address','public-profile-url'],
    passReqToCallback: true
  },
  function(req,token, refreshToken, profile, done) {

    console.log(profile);
}));

答案 1 :(得分:0)

你需要在你的LinkedIn APP中给予许可,如下所示。

在这张图片中你可以看到我已经给了很多许可,就像在这个电子邮件地址中也检查所以我会得到那个

IN this image you can see i have given many permission like in this emailaddress also check so i will get that