簡體   English   中英

錯誤:嘗試訪問 Twitter 時未能在 session 中找到請求令牌 API

[英]Error: Failed to find request token in session while trying to access 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

我試圖尋找答案,但我在 inte.net 上找到的所有解決方案都沒有效率。

我嘗試將 cookies 模式設置為安全

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

本地主機

127.0.0.1

但是 Twitter Portal 告訴我 callbackUrls 不匹配,阻止我這樣做。

(我已經嘗試了所有可能的組合:Twitter 門戶中的本地主機,我的代碼中的 127.0.0.1,也反向以及門戶和代碼中的本地主機或 127.0.0.1)

我也嘗試從 TwitterOauthStrategy 切換到 OAuth1Strategy 但沒有成功。

謝謝你的幫助,我很絕望。

具體來說,您遇到的這個問題/錯誤是因為對於 Twitter API,您必須在您的 App.js 文件中以及您的 Twitter 開發帳戶中使用回調 URL callbackURL: "http://127.0.0.1:3000/auth/twitter/callback"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/兩種方式下都可以正常工作。

這是我的 session 代碼,它適用於所有 3 種策略 FacebookStrategy、GoogleStrategy、TwitterStrategy:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM