javascript - 错误:尝试访问 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 但没有成功。
谢谢你的帮助,我很绝望。
解决方案
具体来说,您遇到的这个问题/错误是因为对于 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
。
我还使用了FacebookStrategy、GoogleStrategy,它们在两种方式http://127.0.0.1:3000
和http://localhost:3000/
.
这是我的会话代码,它适用于 FacebookStrategy、GoogleStrategy、TwitterStrategy 的所有 3 种策略:
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
maxAge: 86400000,
cookie: {},
})
);
推荐阅读
- python - 将新列添加到 Numpy 数组作为行的函数
- checkout - 仅在父字段中输入某些字母/数字后有条件地显示结帐字段?
- python - 用python完全删除excel文件中的NA行
- java - 如何让应用程序将所有整数值添加到数组列表中,以便找到每个年级的中位数?
- python - 设计师小部件阴影?
- python - 基于复制 SAS 的第一个和最后一个功能的唯一/重复使用 pandas 创建不同的数据框
- android - Hilt Field Injection 抛出“属性未初始化”错误?
- haxe - 安装库时haxelib的问题
- elasticsearch - 通过赛普拉斯测试 ElasticSearch 应用程序时出错,导致未填充结果 - “[useElasticsearch] 标头检查不正确”
- python - 使用 stack/unstack 或 groupby 在 python (pandas) 上将长数据帧转换为宽数据帧