Passport-facebook FacebookTokenError: This authorization code has been used
Passport-facebook FacebookTokenError: This authorization code has been used
这是我的passport.js
var FacebookStrategy = require('passport-facebook').Strategy;
var User = require('./app/models/users');
var config = require('./config');
module.exports = function (passport) {
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
passport.use(new FacebookStrategy({
clientID: config.facebook.appId,
clientSecret: config.facebook.appSecret,
callbackURL: config.facebook.redirectUrl,
profileFields: ['id', 'name', 'displayName', 'emails', 'photos']
},
function (accessToken, refreshToken, profile, done) {
process.nextTick(function () {
User.findOne({'facebook.id': profile.id}, function (err, user) {
console.log(profile);
if (err) {
// console.log("tick errr");
return done(err)
}
;
if (user)
return done(null, user);
else {
var newUser = new User();
newUser.facebook.id = profile.id;
newUser.facebook.token = accessToken;
newUser.facebook.name = profile.displayName;
newUser.facebook.email = profile.emails[0].value;
newUser.facebook.picurl = profile.photos[0].value;
// console.log("access to ken is "+accessToken);
//console.log(newUser.facebook.id);
newUser.save(function (err) {
if (err)
console.log(err);
return done(null, newUser);
})
}
});
});
// done(null, profile);
}
));
api.js
api.get('/auth/facebook', function (req, res, next) {
passport.authenticate('facebook', {scope: ['email']})(req, res, next);
});
api.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect: '#/home',
failureRedirect: '#/login'
})
);
};
我在使用 Facebook 登录时遇到问题。
我收到以下错误:
FacebookTokenError: This authorization code has been used.
at Strategy.parseErrorResponse (c:\Users\chetan kanjani\WebstormProjects\letsgo\node_modules\passport-facebook\lib\strategy.js:199:12)
at Strategy.OAuth2Strategy._createOAuthError (c:\Users\chetan kanjani\WebstormProjects\letsgo\node_modules\passport-facebook\node_modules\passport-oauth2\lib\strategy.js:345:16)
at c:\Users\chetan kanjani\WebstormProjects\letsgo\node_modules\passport-facebook\node_modules\passport-oauth2\lib\strategy.js:171:43
at c:\Users\chetan kanjani\WebstormProjects\letsgo\node_modules\passport-facebook\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:177:18
at passBackControl (c:\Users\chetan kanjani\WebstormProjects\letsgo\node_modules\passport-facebook\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:123:9)
at IncomingMessage.<anonymous> (c:\Users\chetan kanjani\WebstormProjects\letsgo\node_modules\passport-facebook\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:143:7)
at IncomingMessage.emit (events.js:129:20)
at _stream_readable.js:908:16
at process._tickDomainCallback (node.js:381:11)
我可以登录 Facebook 页面,然后出现此错误。
我从 Facebook 获取的详细信息仍然成功地存储在我的数据库中;
我认为这与 auth/facebook/callback
有关
users.js 文件:
var UserSchema = mongoose.Schema({
local: {
name: String,
username: {type: String, index: {unique: true}},
password: {type: String, select: false}
},
facebook: {
id: String,
token: String,
email: String,
name: String,
picurl: String,
},
favouriteid: [{eventid: String}]
});
UserSchema.pre('save', function(next) {
var user = this;
if (!user.isModified('local.password')) return next();
bcrypt.hash(user.local.password, null, null, function (err, hash) {
if(err) return next(err);
user.local.password = hash;
next();
});
});
UserSchema.methods.comparePassword = function(password) {
var user = this;
var a = bcrypt.compareSync(password, user.local.password);
if (a == true)
return true;
else {
console.log('error in compare password');
return false;
}
}
当您的 Facebook 帐户中保留应用程序登录数据时,会发生此错误。
删除您的登录数据以解决此错误。
- 登录 Facebook
- 转到设置
- 转到应用程序
- 搜索您应用的登录数据。
- 按下 "X" 按钮
完成上述步骤后,尝试登录Facebook Login。
这是我的passport.js
var FacebookStrategy = require('passport-facebook').Strategy;
var User = require('./app/models/users');
var config = require('./config');
module.exports = function (passport) {
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
passport.use(new FacebookStrategy({
clientID: config.facebook.appId,
clientSecret: config.facebook.appSecret,
callbackURL: config.facebook.redirectUrl,
profileFields: ['id', 'name', 'displayName', 'emails', 'photos']
},
function (accessToken, refreshToken, profile, done) {
process.nextTick(function () {
User.findOne({'facebook.id': profile.id}, function (err, user) {
console.log(profile);
if (err) {
// console.log("tick errr");
return done(err)
}
;
if (user)
return done(null, user);
else {
var newUser = new User();
newUser.facebook.id = profile.id;
newUser.facebook.token = accessToken;
newUser.facebook.name = profile.displayName;
newUser.facebook.email = profile.emails[0].value;
newUser.facebook.picurl = profile.photos[0].value;
// console.log("access to ken is "+accessToken);
//console.log(newUser.facebook.id);
newUser.save(function (err) {
if (err)
console.log(err);
return done(null, newUser);
})
}
});
});
// done(null, profile);
}
));
api.js
api.get('/auth/facebook', function (req, res, next) {
passport.authenticate('facebook', {scope: ['email']})(req, res, next);
});
api.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect: '#/home',
failureRedirect: '#/login'
})
);
};
我在使用 Facebook 登录时遇到问题。 我收到以下错误:
FacebookTokenError: This authorization code has been used.
at Strategy.parseErrorResponse (c:\Users\chetan kanjani\WebstormProjects\letsgo\node_modules\passport-facebook\lib\strategy.js:199:12)
at Strategy.OAuth2Strategy._createOAuthError (c:\Users\chetan kanjani\WebstormProjects\letsgo\node_modules\passport-facebook\node_modules\passport-oauth2\lib\strategy.js:345:16)
at c:\Users\chetan kanjani\WebstormProjects\letsgo\node_modules\passport-facebook\node_modules\passport-oauth2\lib\strategy.js:171:43
at c:\Users\chetan kanjani\WebstormProjects\letsgo\node_modules\passport-facebook\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:177:18
at passBackControl (c:\Users\chetan kanjani\WebstormProjects\letsgo\node_modules\passport-facebook\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:123:9)
at IncomingMessage.<anonymous> (c:\Users\chetan kanjani\WebstormProjects\letsgo\node_modules\passport-facebook\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:143:7)
at IncomingMessage.emit (events.js:129:20)
at _stream_readable.js:908:16
at process._tickDomainCallback (node.js:381:11)
我可以登录 Facebook 页面,然后出现此错误。 我从 Facebook 获取的详细信息仍然成功地存储在我的数据库中; 我认为这与 auth/facebook/callback
有关users.js 文件:
var UserSchema = mongoose.Schema({
local: {
name: String,
username: {type: String, index: {unique: true}},
password: {type: String, select: false}
},
facebook: {
id: String,
token: String,
email: String,
name: String,
picurl: String,
},
favouriteid: [{eventid: String}]
});
UserSchema.pre('save', function(next) {
var user = this;
if (!user.isModified('local.password')) return next();
bcrypt.hash(user.local.password, null, null, function (err, hash) {
if(err) return next(err);
user.local.password = hash;
next();
});
});
UserSchema.methods.comparePassword = function(password) {
var user = this;
var a = bcrypt.compareSync(password, user.local.password);
if (a == true)
return true;
else {
console.log('error in compare password');
return false;
}
}
当您的 Facebook 帐户中保留应用程序登录数据时,会发生此错误。 删除您的登录数据以解决此错误。
- 登录 Facebook
- 转到设置
- 转到应用程序
- 搜索您应用的登录数据。
- 按下 "X" 按钮
完成上述步骤后,尝试登录Facebook Login。