[英]TokenError: Bad Request; Google OAuth2; Passport.js on Node.js; Able to console.log data, however delivers error
[英]Node.js "passport-google-oauth2" delivers "failed to fetch user profile" error in Express application
在開發node.js的介紹性書籍的最后一個示例(使用 Google OpenID 的身份驗證策略的express.js應用程序)時,將passport-google
package (已於 2015 年 4 月 20 日過時)替換為passport-google-oauth2
package (Google OAuth 2.0 的身份驗證策略)並遵循其文檔頁面上的指示和此處的示例; 選擇我的 Google+ 帳戶后出現以下錯誤,這是由oath2.js
模塊拋出的,具體調用this._oauth2.get("https://www.googleapis.com/plus/v1/people/me",...)
在userProfile(accessToken, done)
方法中。 相關源碼和模塊依賴如下。
問題的根源可能是什么?
具體錯誤是:
InternalOAuthError: failed to fetch user profile
at <...>\web-app\b4\node_modules\passport-google-oauth2\lib\oauth2.js:92:28
at passBackControl (<...>\web-app\b4\node_modules\passport-google-oauth2\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:124:9)
at IncomingMessage.<anonymous> (<...>\web-app\b4\node_modules\passport-google-oauth2\node_modules\passport-oauth2\node_modules\oauth\lib\oauth2.js:143:7)
at IncomingMessage.emit (events.js:129:20)
at _stream_readable.js:908:16
at process._tickCallback (node.js:355:11)
相關應用程序的代碼是:
passport = require('passport'),
//...
GoogleStrategy = require('passport-google-oauth2').Strategy; // #passport-google-oauth2
//...
/***** #passport-google-oauth2 vv *****/
passport.use(new GoogleStrategy({
clientID: "a_specific_value",
clientSecret: "another_specific_value",
callbackURL: "http://127.0.0.1:3000/auth/google/callback",
passReqToCallback:true
},
function(request, accessToken, refreshToken, profile, done) {
profile.identifier=profile.id;
return done(null, profile);
}
));
/***** #passport-google-oauth2 ^^ *****/
//...
/***** #passport-google-oauth2 vv *****/
app.get('/auth/google',
passport.authenticate('google', { successRedirect: '/',scope:
[ 'https://www.googleapis.com/auth/userinfo.email']})
);
app.get( '/auth/google/callback',
passport.authenticate( 'google', {
successRedirect: '/',
failureRedirect: '/'
}));
/***** #passport-google-oauth2 ^^ *****/
該應用程序具有以下依賴項:
b4@0.0.1 ├─┬ connect-redis@1.4.7 │ ├─┬ debug@2.1.3 │ │ └── ms@0.7.0 │ └── redis@0.10.3 ├─┬ cookie-parser@1.3.4 │ ├── cookie@0.1.2 │ └── cookie-signature@1.0.6 ├─┬ express@3.3.8 │ ├── buffer-crc32@0.2.1 │ ├─┬ commander@1.2.0 │ │ └── keypress@0.1.0 │ ├─┬ connect@2.8.8 │ │ ├── bytes@0.2.0 │ │ ├── formidable@1.0.14 │ │ ├── pause@0.0.1 │ │ ├── qs@0.6.5 │ │ └── uid2@0.0.2 │ ├── cookie@0.1.0 │ ├── cookie-signature@1.0.1 │ ├─┬ debug@2.1.3 │ │ └── ms@0.7.0 │ ├── fresh@0.2.0 │ ├── methods@0.0.1 │ ├── mkdirp@0.3.5 │ ├── range-parser@0.0.4 │ └─┬ send@0.1.4 │ └── mime@1.2.11 ├─┬ express-session@1.11.1 │ ├── cookie@0.1.2 │ ├── cookie-signature@1.0.6 │ ├── crc@3.2.1 │ ├─┬ debug@2.1.3 │ │ └── ms@0.7.0 │ ├── depd@1.0.1 │ ├── on-headers@1.0.0 │ ├── parseurl@1.3.0 │ ├─┬ uid-safe@1.1.0 │ │ ├── base64-url@1.2.1 │ │ └── native-or-bluebird@1.1.2 │ └── utils-merge@1.0.0 ├─┬ morgan@1.5.2 │ ├── basic-auth@1.0.0 │ ├─┬ debug@2.1.3 │ │ └── ms@0.7.0 │ ├── depd@1.0.1 │ └─┬ on-finished@2.2.0 │ └── ee-first@1.1.0 ├─┬ npmlog@0.0.4 │ └── ansi@0.1.2 ├─┬ passport@0.2.1 │ ├── passport-strategy@1.0.0 │ └── pause@0.0.1 ├─┬ passport-google-oauth2@0.1.6 │ └─┬ passport-oauth2@1.1.2 │ ├── oauth@0.9.12 │ ├── passport-strategy@1.0.0 │ └── uid2@0.0.3 ├── q@0.9.7 ├── redis@0.8.6 └─┬ request@2.27.0 ├── aws-sign@0.3.0 ├── cookie-jar@0.3.0 ├── forever-agent@0.5.2 ├─┬ form-data@0.1.4 │ ├── async@0.9.0 │ └─┬ combined-stream@0.0.7 │ └── delayed-stream@0.0.5 ├─┬ hawk@1.0.0 │ ├── boom@0.4.2 │ ├── cryptiles@0.2.2 │ ├── hoek@0.9.1 │ └── sntp@0.2.4 ├─┬ http-signature@0.10.1 │ ├── asn1@0.1.11 │ ├── assert-plus@0.1.5 │ └── ctype@0.5.3 ├── json-stringify-safe@5.0.0 ├── mime@1.2.11 ├── node-uuid@1.4.3 ├── oauth-sign@0.3.0 ├── qs@0.6.6 └── tunnel-agent@0.3.0
我只是幸運地在jaredhanson/passport-google-oauth發現了一個類似的問題,這讓我想到去谷歌的項目控制台並簡單地啟用Google+ API
,它被“關閉”(哦,我!!,天真的開發者他的第一個基於 Google+ 的應用程序)。 這就是問題的根源。 我再次嘗試, oauth2
開始正確接收配置文件。
您正在使用的scope
現在已棄用:
passport.authenticate('google', { successRedirect: '/',scope:
[ 'https://www.googleapis.com/auth/userinfo.email']})
);
相反,我們必須使用這個:
passport.authenticate('google', { successRedirect: '/',scope:
['email']
}));
您還可以獲取profile scope
:
passport.authenticate('google', { successRedirect: '/',scope:
[ 'email', 'profile' ]
}));
我正在使用 Google OAuth 2.0 Playground,就我而言,此錯誤的原因是我的令牌已過期。 在 Playground 中刷新它解決了這個問題。
我發現了同樣的錯誤,我解決了這個問題:
在 package.json 中,將“passport”值更改為“^0.4.0”,將“passport-google-oauth”更改為“^2.0.0”。 再次運行“npm install”。
經過一些研究,在我的案例中問題的根本原因是我最初使用
userProfileURL:"https://googleapis.com/oauth2/v3/userinfo" (WRONG)
而不是使用
userProfileURL:"https://**www**.googleapis.com/oauth2/v3/userinfo"
當我將 package 從passport-google-oauth
更改為passport-google-oauth20
時,一切都開始正常工作。
當使用不在Test Users
列表中的用戶登錄未發布的 OAuth 應用程序時也會拋出此錯誤(在Google 項目控制台的OAuth consent screen
下)。
我也面臨同樣的問題! 解決方案:內部
passport.use(new GoogleStrategy({ clientID: "a_specific_value", clientSecret: "another_specific_value", callbackURL: "http://127.0.0.1:3000/auth/google/callback", passReqToCallback:true, ****** **************************** },
在注視的行上添加此鏈接“https://www.googleapis.com/oauth2/v3/userinfo” 最終代碼如下所示:passport.use(new GoogleStrategy({ clientID: GOOGLE_CLIENT_ID, clientSecret:.GOOGLE_CLIENT_SECRET, callbackURL: "http://localhost:3000/auth/google/secrets", passReqToCallback:true, userProfileURL:"https://www.googleapis.com/oauth2/v3/userinfo" },希望能解決問題!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.