我在Node.js web服务项目中使用过Passport-Google-OAuth。我正在使用OAuth2Strategy。
我使用的过程是调用web服务方法来验证Gmail帐户中的用户。最初,我提供的是通过调用Passport-google-OAuth获得的原始HTMl。它工作得很好。
然后,我使用有效的Gmail帐户登录。一旦google调用了回调Url,服务器就会进入无限循环,并在固定的时间间隔后一次又一次地调用回调url。
我对Google的Passport策略配置如下:
// Use the GoogleStrategy within Passport.
// Strategies in Passport require a `verify` function, which accept
// credentials (in this case, an accessToken, refreshToken, and Google
// profile), and invoke a callback with a user object.
passport.use(new GoogleStrategy({
clientID : "948630708036-2t6mestiv81gtv0s9n6iptoava4o1cpa.apps.googleusercontent.com",
clientSecret : "omugRnr7nad2yMmefiZdBaLL",
callbackURL : "http://localhost:4000/api/auth/google/callback"
},
function(token, refreshToken, profile, done) {
console.log('Inside global callback.');
// make the code asynchronous
// User.findOne won't fire until we have all our data back from Google
process.nextTick(function() {
// try to find the user based on their google id
User.findOne({ 'google.id' : profile.id }, function(err, user) {
if (err)
return done(err);
if (user) {
// if a user is found, log them in
return done(null, user);
} else {
// if the user isnt in our database, create a new user
var newUser = new User();
// set all of the relevant information
newUser.google.id = profile.id;
newUser.google.token = token;
newUser.google.name = profile.displayName;
newUser.google.email = profile.emails[0].value; // pull the first email
return done(null, newUser);
}
});
});
}));
然后,我从服务项目中的端点调用Passport:
passport.authenticate('google', { session:false,scope : ['profile', 'email'] });
回调URL包含以下代码,我将在其中以JSON格式将返回的用户Google帐户详细信息发送到最初访问web服务的客户端。
function(req, res) {
console.log('Callback by Google:'+res.body+' || '+ res.headers);
console.log('Response Object:'+util.inspect(res));
passport.authenticate('google', { session : false }),function(req,res){
console.log('Callback authenticated.User: +req.user);
res.json(req.user);
}
在日志中,我得到了"Callback by Google: undefined || undefined“。
我将禁用会话,因为这将是API服务器向各种客户端提供数据。
我不知道我做错了什么。请指出在API(网络服务)服务器中使用Passport-Google-OAuth(OAuth2Strategy)的任何资源或示例。我是不是需要走别的路。提前感谢你的帮助。
发布于 2015-08-01 21:40:30
https://stackoverflow.com/questions/31729592
复制相似问题