2

I'm trying to do twitter oauth in nodejs using passportjs but getting error

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

Here is my node js code

    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);
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
iJade
  • 23,144
  • 56
  • 154
  • 243

2 Answers2

2

EDIT There is missing / in auth/twitter/callback route definition.

Also for the routers /auth/twitter and auth/twitter/callback, passport.authenticate() as middleware will do the authentication, and you should have route handling functions.

So the definition of your routes should look something like:

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
npejo
  • 46
  • 3
  • 1
    there is missing / in the callback route definition, try with that – npejo Oct 15 '13 at 21:16
  • now its a new error Error: Failed to find request token in session – iJade Oct 15 '13 at 22:23
  • well actually it didn't work on localhost but worked when i hosted it, can u tell me the reason.Marking as answer – iJade Oct 15 '13 at 22:35
  • 2
    make sure that you are accessing the page through `http://127.0.0.1:3000` as you have specified in the `callbackURL` parameter. Using `http://localhost` can cause that problem – npejo Oct 15 '13 at 22:39
0

You don't need the empty route handler function(req, res) {} - you can just leave the argument out and express will understand you don't plan on ever using the handler

Dillon
  • 79
  • 1
  • 4