本地主机上的护照google oauth

我在使用护照进行身份validation时很新,因此大量的代码片段

我的服务器configuration为:

var router = require('./app/config/routes'); var googleStrategy = require('./app/config/passport'); var session = require("express-session"); var passport = require('passport'); app.use(session({secret : '<secret-key>'})); app.use(passport.initialize()); app.use(passport.session()); googleStrategy(passport); 

我的路线configuration为

 module.exports = function(app, passport) { app.get('/auth/google', function() { passport.authenticate('google', {scope: ['profile', 'email']}); }); app.get('/auth/google/callback', function() { passport.authenticate('google', { successRedirect: '/profile', failureRedirect: '/fail' }); }); .... ALSO configured /profile and /fail }; 

我的护照configuration为

 passport.serializeUser(function(user, callback){ console.log('serializing user.'); callback(null, user); }); passport.deserializeUser(function(user, callback){ console.log('deserialize user.'); callback(null, user); }); var processRequest = function(token, refreshToken, profile, callback){ process.nextTick(function(){ console.log('id : '+ profile.id); console.log('name :'+ profile.displayName); console.log('email :' + profile.emails); console.log('token : '+ token); }); }; passport.use(new GoogleStrategy({ clientID: 'client ID', clientSecret : 'client SECRET', callbackURL : 'http://127.0.0.1:8080/auth/google/callback', realm : 'http://127.0.0.1:8080' }, processRequest)); 

问题:去/auth/google ,我从来没有得到确认屏幕。 我应该看什么?

更新:

改变路线到下面显示的configuration使它工作。

  app.get('/auth/google', passport.authenticate('google', {scope: ['profile', 'email']}) ); app.get('/auth/google/callback', passport.authenticate('google', { successRedirect: '/profile', failureRedirect: '/fail' }) ); 

目前用于authentication和authentication的OAUTH2协议得到了google的支持,所以最好使用相同的。 这里是谷歌的文档。使用“passport-google-oauth”模块。 这里是实现。这应该是应用程序对象configuration,也可以看到oauth2strategy对象是从passport-google-oauth模块中使用的,也是在app.getpath注册中查看范围。

 var googleStrategy = require('passport-google-oauth').OAuth2Strategy; app.configure(function() { app.set('views', './views'); app.set('view engine', 'jade'); app.use(express.favicon()); app.use(express.logger('dev')); app.use(express.cookieParser()); app.use(express.bodyParser()); app.use(express.session({secret:'MySecret'})); app.use(passport.initialize()); app.use(passport.session()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static('./public')); }); app.get('/auth/google', select.passport.authenticate('google',{scope: 'https://www.googleapis.com/auth/plus.me https://www.google.com/m8/feeds https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'})); app.get('/auth/google/callback', function() { passport.authenticate('google', { successRedirect: '/profile', failureRedirect: '/fail' }); }); app.get('/logout', function (req, res) { req.logOut(); res.redirect('/'); }); 

但是在创build新策略之前,请前往Google开发者控制台并获取clientID和秘密。 这是步骤

  1. 去这个链接并创build项目,这里是相同的快照 在这里输入图像描述
  2. 给一个新的项目名称和ID,这里是快照 在这里输入图像描述
  3. 创build新项目大致需要一分钟,一旦创build了新项目,它就会将您redirect到应用程序的应用程序configuration。 在redirect的页面中selectAPIS AND AUTH – > API ,在API的页面中启用GOogle + API,这里是它的快照 在这里输入图像描述
  4. 然后去凭据(下面的API),然后点击创build新的客户端ID ,并注册您的应用程序的域名和callback(configuration域为本地主机),这里是它的快照! 在这里输入图像描述 然后你会得到你的新ID和秘密。 使用它们来创build新的策略

     passport.use(new googleStrategy({ clientID: '<TheNewclientID>', clientSecret: '<The New Secret>', callbackURL: "http://locahost:8080/auth/google/callback" }, function (accessToken, refreshToken, profile, done) { console.log(profile); //profile contains all the personal data returned done(null, profile) } )); 

现在序列化和反序列化

 passport.serializeUser(function(user, callback){ console.log('serializing user.'); callback(null, user.id); }); passport.deserializeUser(function(user, callback){ console.log('deserialize user.'); callback(null, user.id); }); 

运行服务器,并转到localhost:8080 / auth /谷歌(不使用127.0.0.1:8080而不是locahost)。这应该是得到它的工作:)

[其他有用的链接:查看kvcrawford在这个页面中的模块回购的第一个评论Passport谷歌是另一个stream行的模块,它是用来提供login使用谷歌,它的一种过时的现在,这里是链接其最近的问题]

在Web上的大多数例子中,路由代码是这样完成的:

 app.get('/auth/google', passport.authenticate('google')); 

根据快速参考 , app.get方法的callback有三个参数, requestresponse和'next'。 这意味着,上面的例子中的authenticate方法返回一个函数对象,并用三个参数requestresponse和'next'来执行。

所以,如果你想在app.get方法的callback函数中做这样的authentication:

 app.get('/auth/google', function() { passport.authenticate('google', {scope: ['profile', 'email']}); }); 

那么你应该写:

 app.get('/auth/google', function(request, response, next) { passport.authenticate('google', {scope: ['profile', 'email']})(request, response, next); });