passport-google-oauth2 icon indicating copy to clipboard operation
passport-google-oauth2 copied to clipboard

Error 400: invalid_request Missing required parameter: scope

Open farman05 opened this issue 4 years ago • 9 comments

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",

farman05 avatar Feb 09 '21 18:02 farman05

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

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

try this :)

stat1cprojectx avatar Feb 22 '21 06:02 stat1cprojectx

You need to add scope inside new GoogleStrategy

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

Saicharan0662 avatar Mar 01 '22 10:03 Saicharan0662

You need to add scope inside new GoogleStrategy

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

It's working, Thank you so much.

abhidadhaniya23 avatar May 23 '22 18:05 abhidadhaniya23

You need to add scope inside new GoogleStrategy

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

this worked for me, thanks a lot @Saicharan0662 .

marveldc08 avatar Sep 01 '22 09:09 marveldc08

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

abdullah032 avatar Sep 01 '23 22:09 abdullah032

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 }); }

eybel avatar Dec 25 '23 16:12 eybel

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?

ntk-quanxi avatar Mar 08 '24 05:03 ntk-quanxi

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);
  }
}

eybel avatar Mar 08 '24 11:03 eybel

@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", }) } }

ntk-quanxi avatar Mar 09 '24 14:03 ntk-quanxi