任何人都可以帮助我解决链接GitHub中以下代码的错误 带有passport-oauth2消费者的oauth2-provider服务器
在我使用http://localhost:8082
登录并到达我的回调网址后:
http://localhost:8081/auth/provider/callback
,它会抛出错误
var express = require('express')
, passport = require('passport')
, util = require('util')
, TwitterStrategy = require('passport-twitter').Strategy;
var TWITTER_CONSUMER_KEY = "--insert-twitter-consumer-key-here--";
var TWITTER_CONSUMER_SECRET = "--insert-twitter-consumer-secret-here--";
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
passport.use(new TwitterStrategy({
consumerKey: TWITTER_CONSUMER_KEY,
consumerSecret: TWITTER_CONSUMER_SECRET,
callbackURL: "http://127.0.0.1:3000/auth/twitter/callback"
},
function(token, tokenSecret, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
return done(null, profile);
});
}
));
var app = express.createServer();
// configure Express
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.logger());
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.get('/', function(req, res){
res.render('index', { user: req.user });
});
app.get('/account', ensureAuthenticated, function(req, res){
res.render('account', { user: req.user });
});
app.get('/login', function(req, res){
res.render('login', { user: req.user });
});
app.get('/auth/twitter',
passport.authenticate('twitter'),
function(req, res){
// The request will be redirected to Twitter for authentication, so this
// function will not be called.
});
app.get('/auth/twitter/callback',
passport.authenticate('twitter', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
app.listen(3000);
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login')
}
InternalOAuthError:无法获取访问令牌
如何解决此问题?
答案 0 :(得分:7)
答案 1 :(得分:4)
我遇到了类似的问题,试图让passport-oauth2正常工作。正如您所观察到的,错误消息不太有用:
InternalOAuthError: Failed to obtain access token
at OAuth2Strategy._createOAuthError (node_modules/passport-oauth2/lib/strategy.js:382:17)
at node_modules/passport-oauth2/lib/strategy.js:168:36
at node_modules/oauth/lib/oauth2.js:191:18
at ClientRequest.<anonymous> (node_modules/oauth/lib/oauth2.js:162:5)
at emitOne (events.js:116:13)
at ClientRequest.emit (events.js:211:7)
at TLSSocket.socketErrorListener (_http_client.js:387:9)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
我发现a suggestion对passport-oauth2进行了一些小改动:
--- a/lib/strategy.js
+++ b/lib/strategy.js
@@ -163,7 +163,10 @@ OAuth2Strategy.prototype.authenticate = function(req, options) {
self._oauth2.getOAuthAccessToken(code, params,
function(err, accessToken, refreshToken, params) {
- if (err) { return self.error(self._createOAuthError('Failed to obtain access token', err)); }
+ if (err) {
+ console.warn("Failed to obtain access token: ", err);
+ return self.error(self._createOAuthError('Failed to obtain access token', err));
+ }
一旦我这样做了,我收到了一条更有用的错误信息:
Failed to obtain access token: { Error: self signed certificate
at TLSSocket.<anonymous> (_tls_wrap.js:1103:38)
at emitNone (events.js:106:13)
at TLSSocket.emit (events.js:208:7)
at TLSSocket._finishInit (_tls_wrap.js:637:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:467:38) code: 'DEPTH_ZERO_SELF_SIGNED_CERT' }
在我的情况下,我认为根本原因是我正在测试的授权服务器使用的是自签名SSL证书,我可以通过添加此行来解决这个问题:
require('https').globalAgent.options.rejectUnauthorized = false;
答案 2 :(得分:0)
我认为您需要先获取TWITTER_CONSUMER_KEY和TWITTER_CONSUMER_SECRET。就是这样。
How to obtain Twitter Consumer Key
然后将其插入您的代码中。
答案 3 :(得分:0)
我也遇到过同样的问题,在我的情况下,我使用cookie会话是因为我在回调中错误地返回了一个未定义的对象。
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: "http://localhost:5000/auth/google/callback"
},
async function (accessToken, refreshToken, profile, done) {
const googleId = profile.id;
const name = profile.displayName;
const email = profile.emails[0].value;
const existingUser = await User.findOne({googleId});
if(existingUser){
//as u can notice i should return existingUser instaded of user
done(null, user); // <------- i was returning undefined user here.
}else{
const user = await User.create({ googleId, name, email });
done(null, user);
}
}
));