Не удалось получить логин профиля пользователя через Google с помощью узла и паспорта

Я пытаюсь войти в систему через Google, используя узел и пакет паспорт-google-oauth20. Но получаю ошибку - «InternalOAuthError: Failed to fetch user profile». Уже включили google plus api. Я использую свой бизнес-идентификатор Gmail для доступа к нему. Все хранится в mongodb, но в ответ не возвращается json. Вот мой код паспорта-setup.js.

const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20');
const keys = require('./keys');
const User = require('../api/models/user');
const mongoose   = require('mongoose');

passport.use(
new GoogleStrategy({
callbackURL: '/users/google/redirect',
clientID: keys.google.clientID,
clientSecret: keys.google.clientSecret
}, (accessToken, refreshToken, profile, done)=>
{

  User.findOne({googleid : profile.id})
  .exec()
  .then(doc=>{
      if(doc)
      {
        //already exist user
        console.log("User exist : "+doc)
      } else{
        const user = new User({
          _id : new mongoose.Types.ObjectId(),
          name : profile.displayName,
          googleid : profile.id
          });
          user.
          save().
          then(result=>{
              console.log("myerror1" +result);
          })
      }
  })
  .catch(err=>{
      console.log("error1" + err);
  });
  console.log(accessToken);
  console.log(profile);
  })
 )

А вот и код маршрута

router.get('/google', passport.authenticate('google', {
scope: ['profile']
}));

router.get('/google/redirect', passport.authenticate('google'), (req, res)=> 
{
res.send(req.user);
});

Я получаю ошибку при запросе google.redirect.

req.user

показывает мне InternalOAuthError


person deepak bhardwaj    schedule 18.09.2018    source источник


Ответы (2)


Проверяли ли вы эту документацию от Google? Это может помочь.

Кроме того, можете ли вы добавить .Strategy в конец const GoogleStrategy = require('passport-google-oauth20'); и посмотреть, поможет ли это (или если вы получите другое сообщение об ошибке?

const GoogleStrategy = require('passport-google-oauth20').Strategy;
person golojap    schedule 26.12.2018

Я столкнулся с той же проблемой, и я решил ее, сделав следующее:

В package.json измените значение «паспорт» на «^ 0.4.0» и «паспорт-google-oauth» на «^ 2.0.0». Снова запустите «npm install».

person Claudio Augusto Pereira Rolim    schedule 04.03.2020