簡體   English   中英

使用OAuth 2.0a API向LinkedIn進行身份驗證的護照策略在保存用戶時返回未定義的電子郵件

[英]Passport strategy for authenticating with LinkedIn using the OAuth 2.0a API return undefined email on save User

無法在LinkedIn上檢索用戶電子郵件。 我已經使用了Passport-LinkedIn和OAuth 2.0。 我可以插入用戶名和圖片。 這是我嘗試過的代碼。

var LinkedIn = require('passport-linkedin-oauth2').Strategy;

module.exports = (passport, User) => {
    passport.use( new LinkedIn({
        clientID: '86ew637ipvirsa',
        clientSecret: 'HoEMfqCBGL9SxsIt',
        callbackURL: 'http://localhost:3000/auth/linkedin/callback'
        }, (accesstoken, refreshToken, profile, done) => {
        User.findOne({'linkedin.id': profile.id}, (err, x) => {
            if (x) return done(null, x);
            var user = {
                displayName: profile.displayName,
                image: profile._json.pictureUrl,
                email: profile.emailAddress,
                linkedin: {
                    id: profile.id
                }
            };
            User.create(user);
            User.create(user, (err, x) => done(null, x));
        });
    }));
};

您使用的npm軟件包沒有正確記錄。 作者尚未明確說明如何從配置文件變量訪問電子郵件字段。

您可以通過策略傳遞作用域,並通過記錄配置文件變量來獲取電子郵件字段。

passport.use(new LinkedInStrategy({
clientID: LINKEDIN_KEY,
clientSecret: LINKEDIN_SECRET,
callbackURL: "http://127.0.0.1:3000/auth/linkedin/callback",
scope: ['r_emailaddress', 'r_basicprofile'], //pass the scope
state: true
}, function(accessToken, refreshToken, profile, done) {
// asynchronous verification, for effect... 
  process.nextTick(function () {
     console.log(profile); //logging
     return done(null, profile);
  });
}));

您還可以使用其他軟件包 您可以在此處明確定義要訪問的配置文件字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM