Node.js "passport-google-oauth2" поставляет ошибку "не удалось получить профиль пользователя" в приложении "Экспресс"
При разработке последнего примера node.js вводный book (express.js с использованием стратегии аутентификации от Google OpenID), заменив пакет passport-google
(который устарел 20 апреля 2015 г.) с пакетом passport-google-oauth2
(стратегия аутентификации от 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 ^^ *****/
Приложение имеет следующие зависимости:
[email protected]
├─┬ [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └─┬ [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ └─┬ [email protected]
│ └── [email protected]
├─┬ [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ └─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └── [email protected]
├── [email protected]
├── [email protected]
└─┬ [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ └─┬ [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]
Ответы
Ответ 1
Я, к счастью, нашел аналогичную проблему в
jaredhanson/passport-google-oauth, который дал мне идею перейти в консоль Google и просто включить Google+ API
, который был отключен (о, я, наивный разработчик своего первого приложения, основанного на Google+). Это и есть корень проблемы. Я попытался снова, и oauth2
начал правильно получать профили.
Ответ 2
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' ]
}));