passport-twitter: Error: failed to find request token in session

Here is my error. Some of the users experience this others not, or sometime they do, some times not, and that’s quite frustrating since I generally loose 30/40 users per day 😃

2012-06-18T19:40:01+00:00 app[web.1]: Error: failed to find request token in session 2012-06-18T19:40:01+00:00 app[web.1]: at Strategy.<anonymous> (/app/node_modules/passport-twitter/node_modules/passport-oauth/lib/passport-oauth/strategies/oauth.js:120:54) 2012-06-18T19:40:01+00:00 app[web.1]: at Strategy.authenticate (/app/node_modules/passport-twitter/lib/passport-twitter/strategy.js:82:40) 2012-06-18T19:40:01+00:00 app[web.1]: at callbacks (/app/node_modules/express/lib/router/index.js:272:11) 2012-06-18T19:40:01+00:00 app[web.1]: at Passport.authenticate (/app/node_modules/passport/lib/passport/middleware/authenticate.js:153:14) 2012-06-18T19:40:01+00:00 app[web.1]: at param (/app/node_modules/express/lib/router/index.js:246:11) 2012-06-18T19:40:01+00:00 app[web.1]: at pass (/app/node_modules/express/lib/router/index.js:253:5) 2012-06-18T19:40:01+00:00 app[web.1]: at Context.next (/app/node_modules/express/node_modules/connect/lib/http.js:204:15) 2012-06-18T19:40:01+00:00 app[web.1]: at Router._dispatch (/app/node_modules/express/lib/router/index.js:280:4) 2012-06-18T19:40:01+00:00 app[web.1]: at Object.handle (/app/node_modules/express/lib/router/index.js:45:10) 2012-06-18T19:40:01+00:00 app[web.1]: at Context.<anonymous> (/app/node_modules/passport/lib/passport/context/http/actions.js:64:8)

Here is part of my code

  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', maxAge: new Date(Date.now() + 7200000), store: new RedisStore({client: store}) }));
  app.use(passport.initialize());
  app.use(passport.session());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

[...]

passport.use(new TwitterStrategy({ consumerKey: TWITTER_CONSUMER_KEY, consumerSecret: TWITTER_CONSUMER_SECRET, callbackURL: "http://www.commentaindiretta.it/auth/twitter/callback"},
function(token, tokenSecret, profile, done) {
    profile.token = token;
    profile.tokenSecret = tokenSecret;
    process.nextTick(function () {
        // To keep the example simple, the user's Facebook profile is returned to // represent the logged-in user.  In a typical application, you would want // to associate the Facebook account with a user record in your database, // and return that user instead.
        return done(null, profile);
    });
}));```

Any help or hint would be appreciated,
n

About this issue

  • Original URL
  • State: closed
  • Created 12 years ago
  • Reactions: 2
  • Comments: 59 (7 by maintainers)

Most upvoted comments

Hi, twitter won’t let you register localhost as the callback url, so you’ll have it set to 127.0.0.1:3000. The error will raise if you try to browse to localhost:3000, but it won’t if you point your browser to 127.0.0.1:3000 instead. It worked for me (while using code based on https://github.com/jaredhanson/passport-twitter/blob/master/examples/signin/app.js).

Hope it helps.

Regards.

Maybe my previous comment wasn’t clear. As @aeosynth said, set your auth callback on twitter, and since it does not allow localhost, use loopback IP instead:

Assuming your auth callback is http://localhost:3000/auth/twitter/callback, go to twitter, and change it to http://127.0.0.1:3000/auth/twitter/callback.

Then, instead of trying to visit http://localhost:3000/auth/twitter (or the url you have set as an entry point to your login), point your browser to http://127.0.0.1:3000/auth/twitter instead, and it should work.

That being said, it only applies to localhost. The original question by @nicolagreco was using a public hostname, and his auth url is working now, so he may be able to help those of you still having issues.

Hey if someone is still having the issue I have another solution…

add this code : app.use(passport.session({ secret: 'Shhh.. This is a secret', cookie: { secure: true } })); just add cookie: { secure: true } and it will work just fine…

I too had this issue and above technique helped me solve this.

This is a very late answer, but I just figured another reason this can happen. When the guys who made express-session said that MemoryStore is not meant for production, they really meant it.

If you’re using clustering, (pm2 or forever or running on Heroku), then memory based cookie storages have their own set of problems. You’ll often loose cookies or corrupt them (because there are two or more separate processes on server side, not sharing common memory).

If you want to run your Node app with clusters, you need to use Redis or some DB-backed cookie storage

In my case, the problem was simple: I had configured my callback URL using 127.0.0.1, as various folks have indicated is required. But, I kept trying to go to http://localhost:3000/, rather than http://127.0.0.1:3000/, because the habit is deeply ingrained. Sure enough, I get this error if I do that, and it is cleared up by going to http://127.0.0.1:3000/ intead.

And as others have pointed out, in production it will be a nonissue because my callback URL will be a real domain name.

Hi Jared,

Not sure when to do this… However, this is what I’m seeing…when initially home page is loaded (without login)

home: {“cookie”:{“originalMaxAge”:1800,“expires”:“2013-10-03T17:12:57.954Z”,“httpOnly”:true,“path”:“/”},“passport”:{}}

Is this what you wanted to see?

How or where to (intercept) get the dump during sign-in with twitter?

Thanks-

Make sure you’re using the right domain. You may have set the callbackURL to something that isn’t your development URL, in which case you’ve got two different sets of cookies/sessions.

Also, it’s nice to set DEBUG=* environment variable when running the server. Helps with debugging this stuff.