jessiepinkman
jessiepinkman

Reputation: 153

React Google OAuth 2.0: Some requested scopes were invalid

I am trying to use Google OAuth 2.0 to sign in to my web application. I am requesting the following scopes:

When I try to authorize my app, I get the following error message:

Some requested scopes were invalid. {invalid=[https://www.googleapis.com/auth/userinfo.email,https://www.go... I have also enabled the scopes in the Google Cloud Console. However, I am still getting this error message.

const scope = [
  'https://www.googleapis.com/auth/userinfo.email',
  'https://www.googleapis.com/auth/userinfo.profile'
].join(' ');

const params = {
    response_type: 'code',
    client_id: googleClientId,
    redirect_uri: `${apiBaseURL}/${redirectUri}`,
    prompt:'select_account',
    access_type:'offline',
    scope
};

const urlParams = new URLSearchParams(params).toString();
window.location = `${googleAuthUrl}?${urlParams}`;
};

Upvotes: 2

Views: 3844

Answers (2)

Muhammad Muneeb Waqas
Muhammad Muneeb Waqas

Reputation: 207

I was facing the same issue while using Google API for authentication. The issue was in the scope code. So I check the required scopes in Google Developers Console. And made the following changes in my Express.js Code. I added spaces between different scopes as seen in the code.

const authorizeUrl = oauthClient.generateAuthUrl({
    access_type: 'offline',
    scope: 'https://www.googleapis.com/auth/userinfo.profile openid https://www.googleapis.com/auth/userinfo.email',
    prompt: 'consent'
  });

It works for me.

Upvotes: 0

Bob
Bob

Reputation: 191

Try it with plus sign

{
  ..., 
  scope: 'https://www.googleapis.com/auth/userinfo.profile+openid+https://www.googleapis.com/auth/userinfo.email",
}

Scopes defines with string divided by space character or plus sign

Upvotes: 3

Related Questions