jaredhanson / passport-google-oauth2

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

Error 400: invalid_request Missing required parameter: scope #76

Open farman05 opened 3 years ago

farman05 commented 3 years ago

I am stuck with the Missing required parameter: scope, its work fine on local environment but on production its throwing the said error

I am passing the scope like below

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

And using the google strategy like below

passport.use(
    new GoogleStrategy({
        //options for strategy
        callbackURL: `${baseUrl}/auth/google/redirect`,
        clientID: process.env.PASSPORT_CLIENT_ID,
        clientSecret: process.env.PASSPORT_CLIENTSECRET,
    }, (accessToken, refreshToken, profile, done) => {
        //passport callback
       })

passport google auth version : "@types/passport-google-oauth20": "^2.0.4",

stat1cprojectx commented 3 years ago

It may be because of the problem of passing the scope:

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

try this :)

Saicharan0662 commented 2 years ago

You need to add scope inside new GoogleStrategy

scope: ['profile', 'email'],

abhidadhaniya23 commented 2 years ago

You need to add scope inside new GoogleStrategy

scope: ['profile', 'email'],

It's working, Thank you so much.

marveldc08 commented 2 years ago

You need to add scope inside new GoogleStrategy

scope: ['profile', 'email'],

this worked for me, thanks a lot @Saicharan0662 .

abdullah032 commented 1 year ago

I am also facing this error I tried all the solutions provided here, but they did not work.

eybel commented 8 months ago

same here, I had the scope parameter and I still get the same error "Missing required parameter: scope". What could be the reason?

@Injectable() export class GoogleStrategy extends PassportStrategy(Strategy, 'google') { constructor() { super({ clientID: process.env.GOOGLE_CLIENT_ID, clientSecret: process.env.GOOGLE_CLIENT_SECRET, callbackURL: process.env.GOOGLE_CALLBACK_URL, scope: ['email', 'profile'], passReqToCallback: true, // Include request object in callback }); }

ntk-quanxi commented 6 months ago

same here, I had the scope parameter and I still get the same error "Missing required parameter: scope". What could be the reason?

@Injectable() export class GoogleStrategy extends PassportStrategy(Strategy, 'google') { constructor() { super({ clientID: process.env.GOOGLE_CLIENT_ID, clientSecret: process.env.GOOGLE_CLIENT_SECRET, callbackURL: process.env.GOOGLE_CALLBACK_URL, scope: ['email', 'profile'], passReqToCallback: true, // Include request object in callback }); }

I have the same issue in nestjs, you know how to solve that?

eybel commented 6 months ago

same here, I had the scope parameter and I still get the same error "Missing required parameter: scope". What could be the reason? @Injectable() export class GoogleStrategy extends PassportStrategy(Strategy, 'google') { constructor() { super({ clientID: process.env.GOOGLE_CLIENT_ID, clientSecret: process.env.GOOGLE_CLIENT_SECRET, callbackURL: process.env.GOOGLE_CALLBACK_URL, scope: ['email', 'profile'], passReqToCallback: true, // Include request object in callback }); }

I have the same issue in nestjs, you know how to solve that?

I can't remember exactly how I solved it but I will share the final code that I used. I hope it helps:


import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy, Profile, VerifyCallback } from 'passport-google-oauth20';

@Injectable()
export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
  constructor() {
    super({
      clientID: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
      callbackURL: process.env.GOOGLE_CALLBACK_URL,
      passReqToCallback: true,
      scope: ['email', 'profile'],
    });
  }

  async validate(
    req: any,
    accessToken: string,
    refreshToken: string,
    profile: Profile,
    done: VerifyCallback,
  ): Promise<any> {
    // Validate or create user based on Google profile
    const userProfile = {
      email: profile._json.email,
      firstname: profile._json.given_name,
      lastname: profile._json.family_name,
      picture: profile._json.picture,
    };

    // You can access the request object using req.user if needed
    return done(null, userProfile);
  }
}
ntk-quanxi commented 6 months ago

@eybel Thank for your response. I fixed this error. Declare scope in the guard file (like a middlewares in express) instead of in the strategy file. @Injectable() export class GoogleOAuthGuard extends AuthGuard("google") { constructor() { super({ scope: ["profile", "email"], accessType: "offline", }) } }