首页 > 解决方案 > 错误:尝试访问 Twitter API 时未能在会话中找到请求令牌

问题描述

每当我尝试使用 Passport OAuth 连接到 Twitter API 时,我都会遇到此问题,阻止我访问并将我重定向到显示此消息的错误页面:

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

我试图寻找答案,但我在互联网上找到的所有解决方案都没有效率。

我尝试将 cookie 模式设置为安全

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'
}

}));

我也尝试从

本地主机

127.0.0.1

但是 Twitter 门户通过告诉我 callbackUrls 不匹配来阻止我这样做。

(我已经尝试了所有可能的组合:Twitter 门户中的 localhost,我的代码中的 127.0.0.1,反向以及门户和代码中的 localhost 或 127.0.0.1)

我也尝试从 TwitterOauthStrategy 切换到 OAuth1Strategy 但没有成功。

谢谢你的帮助,我很绝望。

标签: javascriptnode.jsapitwitterpassport.js

解决方案


具体来说,您遇到的这个问题/错误是因为对于 Twitter API,您必须callbackURL: "http://127.0.0.1:3000/auth/twitter/callback"在 yourApp.js 文件以及您的 Twitter 开发帐户中Callback URI / Redirect URL使用您也应该http://127.0.0.1:3000/auth/twitter/callback设置。

现在在您的浏览器中,您应该始终在 下访问http://127.0.0.1:3000,如果您尝试http://localhost:3000/每次都会收到此错误Error: Failed to find request token in session

我还使用了FacebookStrategyGoogleStrategy,它们在两种方式http://127.0.0.1:3000http://localhost:3000/.

这是我的会话代码,它适用于 FacebookStrategy、GoogleStrategy、TwitterStrategy 的所有 3 种策略:

    app.use(
        session({
            secret: process.env.SESSION_SECRET,
            resave: false,
            saveUninitialized: false,
            maxAge: 86400000,
            cookie: {},
        })
    );

推荐阅读