jaredhanson / passport-google-oauth

Google authentication strategies for Passport and Node.js.
https://www.passportjs.org/packages/passport-google-oauth/?utm_source=github&utm_medium=referral&utm_campaign=passport-google-oauth&utm_content=about
MIT License
776 stars 327 forks source link

InternalOAuthError: Failed to obtain access token #146

Open akakum921 opened 2 years ago

akakum921 commented 2 years ago

I am getting this error after choosing the google account for signing in InternalOAuthError: Failed to obtain access token at Strategy.OAuth2Strategy._createOAuthError (C:\Users\sairam\Desktop\nodews\codeial\node_modules\passport-oauth2\lib\strategy.js:423:17) at C:\Users\sairam\Desktop\nodews\codeial\node_modules\passport-oauth2\lib\strategy.js:177:45 at C:\Users\sairam\Desktop\nodews\codeial\node_modules\oauth\lib\oauth2.js:191:18 at ClientRequest. (C:\Users\sairam\Desktop\nodews\codeial\node_modules\oauth\lib\oauth2.js:162:5) at ClientRequest.emit (events.js:400:28) at TLSSocket.socketErrorListener (_http_client.js:475:9) at TLSSocket.emit (events.js:400:28) at emitErrorNT (internal/streams/destroy.js:106:8) at emitErrorCloseNT (internal/streams/destroy.js:74:3) at processTicksAndRejections (internal/process/task_queues.js:82:21)

This is the passport google strategy
' const passport = require('passport'); const googleStrategy = require('passport-google-oauth').OAuth2Strategy; const crypto = require('crypto');

const User = require("../models/user");

//tell passport to use a new strategy for google login

passport.use(new googleStrategy({ clientID: GOOGLE_CLIENT_ID, clientSecret: GOOGLE_SECRET, callbackURL: 'http://localhost:8000/users/auth/google/callback', }, function(accessToken, refreshToken, profile, done){ // find a user User.findOne({email: profile.emails[0].value}).exec(function(err, user) { if(err) { console.log("Error in google strategy-passport", err); return; } // console.log(accessToken); console.log(profile);

    if(user) {
        // if found,set this user as a req.user
        return done(null, user);
    } 
    else {
        // if not found, create the user and set it as req.user
        User.create({
            name: profile.displayName,
            email: profile.emails[0].value,
            password: crypto.randomBytes(20).toString('hex'),
        }, function(err, user) {

            if (err) {
                console.log("Error in creating user google strategy-passport", err); return; }

            return done(null, user);
        }); 
    }
});

}

));

module.exports.passport; '

jaredhanson commented 2 years ago

Is your app configured with the correct client ID and secret?

akakum921 commented 2 years ago

Yes, I cross checked it

iohansson commented 2 years ago

@jaredhanson seems that Google changed a lot in their auth flow https://developers.google.com/identity/protocols/oauth2/openid-connect#server-flow The token url (https://oauth2.googleapis.com/token) doesn't match the one used in this strategy.