2015-05-09 2 views
16

При разработке последнего пример «s вводного book приложений с использованием стратегии аутентификации с помощью Google OpenID), после того, как заменяя пакет passport-google(который устарел 20 апреля 2015 года) с пакетом passport-google-oauth2 (стратегия аутентификации от Google OAuth 2.0) и последовали показаниям its documentation's page и example here; Я получил следующую ошибку после выбора моей учетной записи Google+, которая была выбрана модулем oath2.js, конкретно вызывающим this._oauth2.get("https://www.googleapis.com/plus/v1/people/me",...) в пределах userProfile(accessToken, done). Связанные исходные коды и зависимости модулей приведены ниже.Node.js «паспорт Google-oauth2» поставляет «Не удалось получить профиль пользователя» ошибка в применении Экспресс

В чем может быть корень проблемы?

Конкретная ошибка:

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].1.0 
│ ├── [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] 

ответ

49

Я просто к счастью, нашел аналогичный выпуск на jaredhanson/passport-google-oauth, который дал мне идею , чтобы перейти к Google's project console и просто включите Google+ API, который был отключен (oh me !!, наивный разработчик его первого приложения на основе Google+). Это и есть корень проблемы. Я попытался снова, и oauth2 начал правильно получать профили.

+1

Это точно что это было для меня тоже, спасибо! – Nocturno

+1

У меня также были устаревшие области 'https: // www.googleapis.com/ auth/userinfo.profile' и' https: // www.googleapis.com/ auth/userinfo.email', измененные на 'profile' и' email 'соответственно. – Samin

+0

wow thanks потратил так много времени на это. они действительно должны вернуть какую-то ошибку! для меня обратный вызов висел –

4

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' ] 
})); 
+0

У меня такая же проблема с Github oauth. Любая идея, чего не хватает? – Sandeep