gpt4 book ai didi

javascript - 在 nodejs 中使用 twitter oauth 时无法获取/auth/twitter/callback

转载 作者:行者123 更新时间:2023-11-30 17:49:25 25 4
gpt4 key购买 nike

我正在尝试使用 passportjs 在 nodejs 中执行 twitter oauth 但出现错误

Cannot GET /auth/twitter/callback?oauth_token=alksdkalsjdsjd23232378skjdfjsdhf&oauth_verifier=234jjh23j4k234k23h4j2h342k34hj

这是我的 Node js代码

    var express = require('express')
, passport = require('passport')
, util = require('util')
, GoogleStrategy = require('passport-google').Strategy
, TwitterStrategy = require('passport-twitter').Strategy;


passport.serializeUser(function(user, done) {
done(null, user);
});

passport.deserializeUser(function(obj, done) {
done(null, obj);
});


passport.use(new TwitterStrategy({
consumerKey: 'xxxxxxxxxxxxxxxxxxxx',
consumerSecret: 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
callbackURL: 'http://127.0.0.1:3000/auth/twitter/callback'
},
function(token, tokenSecret, profile, done) {
process.nextTick(function () {
return done(null, profile);
});
}
));


var app = express();

// 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' }));
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
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('/login', function(req, res){
res.sendfile('./views/auth.html');
});




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

app.get('auth/twitter/callback',
passport.authenticate('twitter', { successRedirect: '/success',
failureRedirect: '/login' }));

app.get('/success', function(req, res){
res.send("success logged in");
});

app.listen(process.env.PORT || 3000);

最佳答案

编辑auth/twitter/callback 路由定义中缺少 /

同样对于路由器 /auth/twitterauth/twitter/callbackpassport.authenticate() 作为中间件将进行身份验证,你应该有路由处理功能。

所以你的路由定义应该是这样的:

app.get('/auth/twitter', 
passport.authenticate('twitter'),
function(req, res) {}); // empty route handler function, it won't be triggered
app.get('/auth/twitter/callback',
passport.authenticate('twitter', {
successRedirect: '/success',
failureRedirect: '/login' }),
function(req, res) {}); // route handler

关于javascript - 在 nodejs 中使用 twitter oauth 时无法获取/auth/twitter/callback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19390204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com