【问题标题】:Error: Failed to find request token in session while trying to access Twitter API错误:尝试访问 Twitter API 时未能在会话中找到请求令牌
【发布时间】:2023-03-11 05:42:01
【问题描述】:

每当我尝试使用 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'
}

}));

我也尝试设置 callbackUrl from

本地主机

127.0.0.1

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

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

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

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

【问题讨论】:

    标签: javascript node.js api twitter passport.js


    【解决方案1】:

    具体来说,您遇到的这个问题/错误是因为对于 Twitter API,您必须在 yourApp.js 文件以及您的 Twitter 开发帐户Callback URI / Redirect URL 中使用 callbackURL: "http://127.0.0.1:3000/auth/twitter/callback",您还应该设置 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: {},
            })
        );
    

    【讨论】:

      猜你喜欢
      • 2021-11-04
      • 2014-06-11
      • 2021-09-07
      • 2019-09-16
      • 2019-12-05
      • 2017-07-20
      • 2016-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多