Search code examples
node.jsexpressoauth-2.0passport.jsgoogle-signin

Passport google login throwing error InternalOAuthError: Failed to obtain access token


I am trying to implement google login using the passport-google-oauth package and getting the below error.

InternalOAuthError: Failed to obtain access token
at Strategy.OAuth2Strategy._createOAuthError (/Users/rishiraj/Documents/Projects/admin-panel/server/node_modules/passport-oauth2/lib/strategy.js:423:17)
at /Users/rishiraj/Documents/Projects/admin-panel/server/node_modules/passport-oauth2/lib/strategy.js:177:45
at /Users/rishiraj/Documents/Projects/admin-panel/server/node_modules/oauth/lib/oauth2.js:191:18
at ClientRequest.<anonymous> (/Users/rishiraj/Documents/Projects/admin-panel/server/node_modules/oauth/lib/oauth2.js:162:5)
at ClientRequest.emit (node:events:527:28)
at ClientRequest.emit (node:domain:475:12)
at TLSSocket.socketErrorListener (node:_http_client:454:9)
at TLSSocket.emit (node:events:527:28)
at TLSSocket.emit (node:domain:475:12)
at emitErrorNT (node:internal/streams/destroy:157:8)

Getting a google profile object. but after this /auth/google/callback URL got called and it's throwing an error.

Server.js

require("dotenv").config();
// express initialized
const app = express();

app.set("trust proxy", true);
// mongo connected
app.use(cookieParser(config.cookieSecret));
// Cookies parser
connection(app).catch((error) => {
  if (error) throw error;
});

app.disable("x-powered-by");

app.use(express.text({ type: "application/graphql" }));

session(app);

app.use(cors());

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

// Started passport fun...
passport.serializeUser((user, done) => {
 setTimeout(() => {
   done(null, user);
  });
});
passport.deserializeUser((id, done) => {
 setTimeout(async () => {
  try {
    const token = await controller.AccessToken.findById(id);
    done(null, token);
  } catch (error) {
    done(error, false);
  }
 });
});

app.get(
"/auth/google",
passport.authenticate("google", {
  scope: ["profile", "email"],
  session: false,
 })
);
  app.get(
"/auth/google/callback",
passport.authenticate("google", {
  failureRedirect: "http://localhost:3000",
  successRedirect: "http://localhost:3000",
}),
(req, res) => {
  const { type, redirectUrl } = req.session;
  console.log("when successfully login");
  res.redirect("http://localhost:3000");
  }
);
  passport.use(
new OAuth2Strategy(
  {
    clientID: process.env.GOOGLE_CLIENT_ID,
    clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    callbackURL: "http://localhost:3002/auth/google/callback",
  },
  function (accessToken, refreshToken, profile, done) {
    console.log("google login", accessToken, refreshToken, profile);
    done(null, profile.id);
  }
 )
);
// End passport fun...

app.use(express.json());

// schema.applyMiddleware(app);
app.get("/", (req, res) => {
  return res.send("<h1>Working on project structure</h1>");
});

async function startServer() {
 await apolloServer.start();
 apolloServer.applyMiddleware({ app });
 console.log(apolloServer.graphqlPath);
 app.listen(process.env.PORT || 3001, () => {
   consolog.serverStartup();
 });
}

startServer();

Guys, can you please help me to figure out this.

I already tried this.

Thanks in advance.


Solution

  • After struggling 2 days final I found a solution. it's a node version problem.
    I was using node-v14.19.1.pkg.
    now updated node-v16.0.0.pkg.

    Working Fine Now.