![](/img/trans.png)
[英]Passport strategy for authenticating with LinkedIn using the OAuth 2.0a API return undefined email on save User
[英]Passport strategy for authenticating with LinkedIn using the OAuth 1.0a API return undefined email on save User
我是node.js開發的初學者,並嘗試基於Express框架構建應用程序。 我為用戶實現了本地護照通行證-facebook和護照通行證,以便用戶注冊並登錄到應用程序,以及在mongoDb中將多個社交帳戶鏈接到單個用戶帳戶(來自本教程 )。
我的問題是,即使我可以使用經過正確身份驗證的用戶獲取名字和姓氏,在使用通行證-linkedin時也總是會得到未定義的電子郵件字段。
passport.js
passport.use(new LinkedinStrategy({
consumerKey : configAuth.linkedinAuth.consumerKey,
consumerSecret : configAuth.linkedinAuth.consumerSecret,
callbackURL : configAuth.linkedinAuth.callbackURL,
passReqToCallback : true // allows us to pass in the req from our route (lets us check if a user is logged in or not)
},
function(req, token, tokenSecret, profile, done) {
// asynchronous
process.nextTick(function() {
// check if the user is already logged in
if (!req.user) {
User.findOne({ 'linkedin.id' : profile.id }, function(err, user) {
if (err)
return done(err);
if (user) {
// if there is a user id already but no token (user was linked at one point and then removed)
if (!user.linkedin.token) {
[...]
} else {
// if there is no user, create them
var newUser = new User();
newUser.linkedin.id = profile.id;
newUser.linkedin.token = token;
newUser.linkedin.firstname = profile.name.givenName;
newUser.linkedin.lastname = profile.name.familyName;
newUser.linkedin.email = profile.emails;
newUser.save(function(err) {
if (err)
return done(err);
return done(null, newUser);
});
}
});
} else {
// user already exists and is logged in, we have to link accounts
[...]
}
});
}));
路線/ accounts.js
// linkedin --------------------------------
// send to linkedin to do the authentication
app.get('/auth/linkedin', passport.authenticate('linkedin', { scope: ['r_basicprofile', 'r_emailaddress'] }));
// handle the callback after linkedin has authenticated the user
app.get('/auth/linkedin/callback',
passport.authenticate('linkedin', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
通過查看Passport-LinkedIn / lib / strategy.js ,我可以找到要保存到我的用戶數據庫中的名字和姓氏,然后我對profile.email進行了同樣的操作。在正確驗證用戶身份后,emails仍然保持“未定義”狀態。
預先感謝您的幫助。
編輯:來自LinkedIn開發人員常見問題解答
雖然我們尚未宣布將OAuth 1.0a與LinkedIn的API一起正式棄用,但我們不再積極鼓勵或支持其使用。
使用OAuth 1.0a構建的現有應用程序將繼續正常運行,直到我們正式宣布其停用。
強烈建議所有應用程序使用或過渡到OAuth 2.0。
前一段時間有同樣的問題,現在可以正常工作了。 您是否嘗試過使用oauth2 ? 如果您已經實現了passport-linkedin,那么這對您的password.js文件應該是很小的更改。 Linkedin不贊成使用OAuth 1.0,因此為什么它可能導致某些問題來檢索某些信息。 我猜你正面臨着同樣的情況。 試試這個,你應該很好!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.