I get "ERROR GET /auth/google/callback TokenError: Unauthorized" after I try to use my gmail to authenticate

I get "ERROR GET /auth/google/callback TokenError: Unauthorized" after I try to use my gmail to authenticate

我正在尝试使用 Google 身份验证登录,但是由于某种原因,在我点击一个帐户后,在我尝试使用 Google Oauth 进行身份验证,我该如何解决这个问题?

这是我的 App.js


const GoogleStrategy = require("passport-google-oauth20").Strategy;

const User = require('./models/user.js')


mongoose
  .connect('mongodb://localhost/localpassport', { useNewUrlParser: true })
  .then(x => {
    console.log(`Connected to Mongo! Database name: "${x.connections[0].name}"`)
  })
  .catch(err => {
    console.error('Error connecting to mongo', err)
  });

const app_name = require('./package.json').name;
const debug = require('debug')(`${app_name}:${path.basename(__filename).split('.')[0]}`);

const app = express();

// Middleware Setup
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(flash());

app.use(
  session({
    secret: "our-passport-local-strategy-app",
    store: new MongoStore({ mongooseConnection: '//localhost/localpassport' }),
    resave: true,
    saveUninitialized: true
  })
);


app.use(passport.initialize());
app.use(passport.session());


//deserializer code
passport.serializeUser((user, cb) => {
  cb(null, user._id);
});
passport.deserializeUser((id, cb) => {
  User.findById(id)
    .then(user => cb(null, user))
    .catch(err => cb(err))
    ;
});

//Strategy definition

passport.use(new LocalStrategy(
  {
    usernameField: 'username', // by default
    passwordField: 'password'  // by default
  },
  (username, password, done) => {
    User.findOne({ username })
      .then(user => {
        if (!user) {
          return done(null, false, { message: "Incorrect username" });
        }

        if (!bcrypt.compareSync(password, user.password)) {
          return done(null, false, { message: "Incorrect password" });
        }

        done(null, user);
      })
      .catch(err => done(err))
      ;
  }
));


//GOOGLE strategy

passport.use(
  new GoogleStrategy(
    {
      clientID: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_ID,
      callbackURL: "http://localhost:3000/auth/google/callback"
    },
    (accessToken, refreshToken, profile, done) => {
      // to see the structure of the data in received response:
      console.log("Google account details:", profile);

      User.findOne({ googleID: profile.id })
        .then(user => {
          if (user) {
            done(null, user);
            return;
          }

          User.create({ googleID: profile.id })
            .then(newUser => {
              done(null, newUser);
            })
            .catch(err => done(err)); // closes User.create()
        })
        .catch(err => done(err)); // closes User.findOne()
    }
  )
);

// Express View engine setup

app.use(require('node-sass-middleware')({
  src: path.join(__dirname, 'public'),
  dest: path.join(__dirname, 'public'),
  sourceMap: true
}));


app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.use(express.static(path.join(__dirname, 'public')));
app.use(favicon(path.join(__dirname, 'public', 'images', 'favicon.ico')));



// default value for title local
app.locals.title = 'Express - Generated with IronGenerator';


const auth = require('./routes/auth-routes')
const index = require('./routes/index');
app.use('/', index);
app.use('/', auth)


module.exports = app;

有人可以帮帮我吗?我不知道我做错了什么。

这通常意味着客户端 ID 或客户端密码中存在拼写错误,您能否验证这两个值是否完全写入提供的值?

在定义 GoogleStrategy 时 app.js 处出现错字,clientSecret 定义不正确。已解决