'Error: Failed to find request token in session while trying to access Twitter API
Whenever I try to connect to Twitter API using Passport OAuth, I have this issue that prevents me to and redirects me to an error page that displays this message:
Error: Failed to find request token in session
at SessionStore.get (/Users/youcefchergui/Work/ESP/socialboard/server/node_modules/passport-oauth1/lib/requesttoken/session.js:13:44)
at OAuthStrategy.authenticate (/Users/youcefchergui/Work/ESP/socialboard/server/node_modules/passport-oauth1/lib/strategy.js:214:33)
at attempt (/Users/youcefchergui/Work/ESP/socialboard/server/node_modules/passport/lib/middleware/authenticate.js:366:16)
at authenticate (/Users/youcefchergui/Work/ESP/socialboard/server/node_modules/passport/lib/middleware/authenticate.js:367:7)
at Layer.handle [as handle_request] (/Users/youcefchergui/Work/ESP/socialboard/server/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/youcefchergui/Work/ESP/socialboard/server/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/youcefchergui/Work/ESP/socialboard/server/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/youcefchergui/Work/ESP/socialboard/server/node_modules/express/lib/router/layer.js:95:5)
at /Users/youcefchergui/Work/ESP/socialboard/server/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/youcefchergui/Work/ESP/socialboard/server/node_modules/express/lib/router/index.js:335:12)
at next (/Users/youcefchergui/Work/ESP/socialboard/server/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/Users/youcefchergui/Work/ESP/socialboard/server/node_modules/express/lib/router/index.js:174:3)
at router (/Users/youcefchergui/Work/ESP/socialboard/server/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/Users/youcefchergui/Work/ESP/socialboard/server/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/youcefchergui/Work/ESP/socialboard/server/node_modules/express/lib/router/index.js:317:13)
at /Users/youcefchergui/Work/ESP/socialboard/server/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/youcefchergui/Work/ESP/socialboard/server/node_modules/express/lib/router/index.js:335:12)
at next (/Users/youcefchergui/Work/ESP/socialboard/server/node_modules/express/lib/router/index.js:275:10)
at cors (/Users/youcefchergui/Work/ESP/socialboard/server/node_modules/cors/lib/index.js:188:7)
at /Users/youcefchergui/Work/ESP/socialboard/server/node_modules/cors/lib/index.js:224:17
at originCallback (/Users/youcefchergui/Work/ESP/socialboard/server/node_modules/cors/lib/index.js:214:15)
at /Users/youcefchergui/Work/ESP/socialboard/server/node_modules/cors/lib/index.js:219:13
I tried to search for an answer but all the solutions I found on the internet weren't efficient.
I tried to set the cookies mode to secure
app.use(session({
genid: function(req) {
return null; // use UUIDs for session IDs
},
secret: process.env.SESSION_SECRET,
maxAge: 86400000,
resave: false,
saveUninitialized: false,
cookie: {
secure: 'auto'
}
}));
I also tried to set the callbackUrl from
localhost
to
127.0.0.1
but Twitter Portal prevents me to do so by telling me that the callbackUrls don't match.
(I've tried all the combinations possible: localhost in Twitter Portal, 127.0.0.1 in my code, reverse too and also both localhost or 127.0.0.1 in both Portal and code)
I also tried to switch from TwitterOauthStrategy to OAuth1Strategy but wasn't successful.
Thanks for your help, I'm desesperate.
Solution 1:[1]
Specifically this issue/error you get is because for Twitter API you must use callbackURL: "http://127.0.0.1:3000/auth/twitter/callback" in yourApp.js file and also in your Twitter Dev Account at Callback URI / Redirect URL you should have http://127.0.0.1:3000/auth/twitter/callback set as well.
Now in your browser you should always access under http://127.0.0.1:3000, if you try http://localhost:3000/ you'll get this error every time Error: Failed to find request token in session.
I also used FacebookStrategy, GoogleStrategy and these work fine in both ways http://127.0.0.1:3000 and http://localhost:3000/.
Here is my session code and it works for all 3 strategies FacebookStrategy, GoogleStrategy, TwitterStrategy:
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
maxAge: 86400000,
cookie: {},
})
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Denis Schiopu |
