'TokenError: Bad Request; Google OAuth2; Passport.js on Node.js; Able to console.log data, however delivers error

I am attempting to use Passport.js to authorize Google OAuth2 on Node.js. I have tried all week to make it work and have no idea why it isn't, so am now resorting to stack for some potential help. I have tried all solutions to similar problems available on forums online.

Each time it sends the request it returns TokenError: Bad Request, however, it is able to console.log the required data, so this to me demonstrates that the token was in fact successful. I cannot explain why this is occurring.

I have tried being more specific in callback request e.g http://localhost:3000/auth/google/redirect. I have tried every other type of Oauth type google has Node server, web application, html ect. I have tried different ports.

AUTH ROUTES

 const router = require('express').Router();
 const passport = require('passport');

 // auth login
 router.get('/login', (req, res) => {
     res.render('login', { user: req.user });
 });

 // auth logout
 router.get('/logout', (req, res) => {
     // handle with passport
     res.send('logging out');
 });

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

 // callback route for google to redirect to
 // hand control to passport to use code to grab profile info
     router.get('/google/redirect', passport.authenticate('google'), 
   (req, 
   res) => {
      res.send('you reached the redirect URI');
   });

module.exports = router;

PASSPORT_SETUP

const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const keys = require('./keys');

passport.use(
new GoogleStrategy({
    // options for google strategy
    clientID: keys.google.clientID,
    clientSecret: keys.google.clientSecret,
    callbackURL: '/auth/google/redirect'
   }, (accessToken, refreshToken, profile, done) => {
    // passport callback function
    console.log('passport callback function fired:');
    console.log(profile);
    })
);

When submitted the process progresses through SignIn page, delivers desired result the console.log and then just sits for about 1 minute awaiting localhost.

As you can see the very thing it is trying to retrieve is already in the console.

console

It then progresses to throw and Error:

Error



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source