嗨,我在一个项目上工作,其中用户通过谷歌帐户登录。(本地主机)我已经实现了谷歌注册。一旦我从我的帐户登录,我就会得到下面的错误。
TokenError: Code was already redeemed.
at Strategy.OAuth2Strategy.parseErrorResponse (c:\Projects\Internship_rideshare\node_modules\passport-google-oauth\node_modules\passport-oauth\node_modules\passport-oauth2\lib\strategy.js:298:12)
at Strategy.OAuth2Strategy._createOAuthError (c:\Projects\Internship_rideshare\node_modules\passport-google-oauth\node_modules\passport-oauth\node_modules\passport-oauth2\lib\strategy.js:345:16)
at c:\Projects\Internship_rideshare\node_modules\passport-google-oauth\node_modules\passport-oauth\node_modules\passport-oauth2\lib\strategy.js:171:43
at c:\Projects\Internship_rideshare\node_modules\passport-google-oauth\node_modules\passport-oauth\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:176:18
at passBackControl (c:\Projects\Internship_rideshare\node_modules\passport-google-oauth\node_modules\passport-oauth\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:123:9)
at IncomingMessage.<anonymous> (c:\Projects\Internship_rideshare\node_modules\passport-google-oauth\node_modules\passport-oauth\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:142:7)
at IncomingMessage.emit (events.js:129:20)
at _stream_readable.js:908:16
at process._tickCallback (node.js:355:11)我的代码如下( google登录的代码片段):
passport.use(new GoogleStrategy(google, function(req, accessToken, refreshToken, profile, done) {
if (req.user) {
User.findOne({ google: profile.id }, function(err, existingUser) {
if (existingUser) {
console.log('There is already a Google+ account that belongs to you. Sign in with that account or delete it, then link it with your current account.' );
done(err);
} else {
User.findById(req.user.id, function(err, user) {
user.google = profile.id;
user.tokens.push({ kind: 'google', accessToken: accessToken });
user.profile.displayName = user.profile.displayName || profile.displayName;
user.profile.gender = user.profile.gender || profile._json.gender;
//user.profile.picture = user.profile.picture || 'https://graph.facebook.com/' + profile.id + '/picture?type=large';
user.save(function(err) {
console.log('Google account has been linked.');
done(err, user);
});
});
}
});
} else {
User.findOne({ google: profile.id }, function(err, existingUser) {
if (existingUser) return done(null, existingUser);
User.findOne({ email: profile._json.email }, function(err, existingEmailUser) {
if (existingEmailUser) {
console.log('There is already an account using this email address. Sign in to that account and link it with Google manually from Account Settings.' );
done(err);
} else {
var user = new User();
user.email = profile._json.email;
user.google = profile.id;
user.tokens.push({ kind: 'google', accessToken: accessToken });
user.profile.displayName = profile.displayName;
user.profile.gender = profile._json.gender;
//user.profile.picture = 'https://graph.facebook.com/' + profile.id + '/picture?type=large';
user.profile.location = (profile._json.location) ? profile._json.location.name : '';
user.save(function(err) {
done(err, user);
});
}
});
});
}
}));我被困在it.Please help me out..thanks上
发布于 2015-08-13 00:56:34
问题不在你的“代码片段”中,看看路由。它应该是谷歌重定向的绝对路径。
router.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '#/signIn' }),
function(req, res) {
// absolute path
res.redirect('http://localhost:8888/#/home');
});这是一个已知问题,请访问其他变通方法https://github.com/jaredhanson/passport-google-oauth/issues/82的链接
发布于 2018-04-29 11:52:58
我遇到过这个问题。确切的问题是你的路线。
app.get('/auth/google/callback', passport.authenticate('google'), (req, res) => {
res.send('get the data');
});在这一点上,app已经获得了用户权限,google向这个url发送了一个代码。现在passport在这里做的是,它提取代码,向谷歌请求用户详细信息,并从谷歌获得它。现在我们必须对这个细节做些什么,否则你会得到你已经得到的错误。
现在我们可以使用passport的serialiseUser和deserialiseUser将详细信息保存在cookie中,并编辑上面的一行代码以转到像这样的url。
app.get('/auth/google/callback', passport.authenticate('google'), (req, res) => {
res.redirect('/servey'); // just a url to go somewhere
});发布于 2018-06-26 00:54:18
这几天我也遇到了同样的问题。我想的是,你只需要完成这个过程。到目前为止,您只检查了用户是否存在于数据库中。如果不是,则将用户保存到数据库中。
然而,在此之后,当谷歌尝试重定向用户时,google+应用程序接口发送的代码已被使用,或者说它不再可用。因此,当你在数据库中检查用户时,你需要序列化用户,即将代码存储到浏览器中的cookie中,以便当google重定向用户时,它知道用户是谁。这可以通过添加下面给出的代码来完成。
//add this in current snippet
passport.serializeUser(function(user,done){
done(null,user.id);
});要使用此cookie,您需要反序列化用户。要反序列化,请使用下面给出的代码。
//add this in current snippet
passport.deserializeUser(function(id,done){
User.findById(id).then(function(user){
done(null, user);
});
});此外,您还需要启动cookie会话,您可以通过在主app.js文件中添加以下代码来实现这一点。
const cookieSession = require('cookie-session');
app.use(cookieSession({
maxAge: 24*60*60*1000, // age of cookie, the value is always given in milliseconds
keys:[keys.session.cookiekey]
}));
//initialize passport
app.use(passport.initialize());
app.use(passport.session());请注意,您需要cookie-session包。使用以下命令安装它
npm install cookie-session此外,您还需要在您的谷歌策略中的callbackURL属性中编写绝对URI。
https://stackoverflow.com/questions/31167336
复制相似问题