InternalOAuthError: Failed to obtain access token 0
See original GitHub issueAfter choosing the google account from the google page, the page redirect to my app but the process keep hanging then an error of " InternalOAuthError: Failed to obtain access token 0" was thrown, thus authentication process can not proceed.
1.launch the app 2.go to http://localhost:3000/login or http://localhost:3000/register route , both have the link to /auth/google/ route 3. the route trigger the passport.authenticate(“google”, { scope: [“https://www.googleapis.com/auth/userinfo.profile”] })); 4.redirect to the google page, then I choose my google account to login 5.the google page seems to try wait for response from local host, 6 an error of “InternalOAuthError: Failed to obtain access token 0” was thrown
I am just trying to login with google oauth2
Expected behavior
After choosing the google account from the google authentication page, I should be redirected to my app and an Access Token is retrieved from google
Actual behavior
After choosing the google account from google page, the process hanged then the error was thrown, it seems the Authoriazation Code is retrieved but the passport-google-oauth2 package faild to exchange the Access Token with the Authorization Code in hand, the package should exchange them automatically, but why is it not done?
Steps to reproduce
11.launch the app 2.go to http://localhost:3000/login or http://localhost:3000/register route , both have the link to /auth/google/ route 3. the route trigger the passport.authenticate(“google”, { scope: [“https://www.googleapis.com/auth/userinfo.profile”] })); 4.redirect to the google page, then I choose my google account to login 5.the google page seems to try wait for response from local host, 6 an error of “InternalOAuthError: Failed to obtain access token 0” was thrown
//jshint esversion:6
require("dotenv").config();
const express = require("express");
const ejs = require("ejs");
const mongoose = require("mongoose");
const session = require("express-session");
const passport = require("passport");
const passportLocalMongoose = require("passport-local-mongoose");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const findOrCreate = require("mongoose-findorcreate");
const app = express();
app.use(express.static("public"));
app.set("view engine", "ejs");
app.use(
express.urlencoded({
extended: true
})
);
app.use(
session({
secret: "Our little secret.",
resave: false,
saveUninitialized: false
})
);
app.use(passport.initialize());
app.use(passport.session());
const mongoDB = "...";
const mongoDBLocal = "mongodb://localhost:27017/secretsDB";
const enviornment = process.env.NODE_ENV || "developement";
const url = enviornment === "developement" ? mongoDBLocal : mongoDB;
mongoose.set("useCreateIndex", true);
mongoose.set("useUnifiedTopology", true);
mongoose.connect(url, { useNewUrlParser: true });
const userSchema = new mongoose.Schema({
email: String,
password: String
googleId: String,
secret: String
});
userSchema.plugin(passportLocalMongoose);
userSchema.plugin(findOrCreate);
const User = new mongoose.model("User", userSchema);
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
passport.use(
new GoogleStrategy(
{
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/google/secrets",
userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo"
},
function(accessToken, refreshToken, profile, cb) {
console.log(profile);
User.findOrCreate(
{
googleId: profile.id
},
function(err, user) {
return cb(err, user);
}
);
}
)
);
app
.route("/")
.get(function(req, res) {
res.render("home");
})
.post(function(req, res) {});
app.route("/auth/google").get(
passport.authenticate("google", { scope: ["https://www.googleapis.com/auth/userinfo.profile"] }));
app
.route("/auth/google/secrets")
.get(passport.authenticate("google", { failureRedirect: "/login" }), function(
req,
res
) {
res.redirect("/secrets");
});
app
.route("/login")
.get(function(req, res) {
res.render("login");
})
.post(function(req, res) {
const user = new User({
username: req.body.username,
password: req.body.password
});
req.login(user, function(err) {
if (err) {
console.log(err);
} else {
passport.authenticate("local")(req, res, function() {
res.redirect("/secrets");
});
}
});
app.route("/logout").get(function(req, res) {
req.logout();
res.redirect("/");
});
const username = req.body.username;
const password = req.body.password;
User.findOne({ email: username }, function(err, foundUser) {
if (err) {
console.log(err);
} else {
if (foundUser) {
bcrypt.compare(password, foundUser.password, function(err, result) {
if (result === true) {
res.render("secrets");
} else {
res.send("user not exsist or password incoreect.");
}
});
}
}
});
});
app.get("/secrets", function(req, res) {
if (req.isAuthenticated()) {
res.render("secrets");
} else {
res.redirect("/login");
}
});
app
.route("/register")
.get(function(req, res) {
res.render("register");
})
.post(function(req, res) {
User.register({ username: req.body.username }, req.body.password, function(
err,
user
) {
if (err) {
console.log(err);
res.redirect("/register");
} else {
passport.authenticate("local")(req, res, function() {
res.redirect("/secrets");
});
}
});
let port = process.env.PORT;
if (port == null || port == "") {
port = 3000;
}
app.listen(port, function() {
console.log(`Server online, now listening on port ${port}`);
});
// Format code using Markdown code blocks
Environment
- Operating System: windows 7 64 x86
- Node version: v10.16.0
- passport version: passport 0.4.1
- passport-google-oauth2 version: passport-google-oauth20@2.0.0
Issue Analytics
- State:
- Created 3 years ago
- Comments:17
@VoyageinStyle I had a similar issue. Found this https://github.com/nodejs/node/issues/42116 issue in the node repo, downgraded to 16.3.0 and it worked like a charm without any errors. But I guess it’ll help if you’re using ESM like I do and experiencing the issue. Of course, downgrading node is not the best option, I’ll investigate further when I have time.
For anyone still struggling with this issue, there’s a problem with the
node-oauth
package mentioned in issue #87 that causes this behaviour: Here’s a possible fix: