Google OpenId - redirect_uri/callback not happening. Is the issue localhost?

1.1k Views Asked by At

Following the the Google OpenID doc, I am attempting to send an authentication request to Google.

When the request goes out to https://accounts.google.com/o/oauth2/v2/auth, the request is not redirected back to my redirect_uri http://localhost:5901. I also am not getting the google login screen(not sure if I am supposed to get a login screen).

I checked the CLIENT_ID and it is correct.

Is the issue here localhost?

enter image description here

const URL = 'https://accounts.google.com/o/oauth2/v2/auth';

// initial login; redirect to get permissions
const constructCodeURI = () => {
  const queryString =
    `client_id=${CLIENT_ID}`
    + `&scope=openidemail`
    + '&state=019146662993491234567890'
    + '&[email protected]'
    + '&nonce=03852-31485-24958'
    + `&redirect_uri=http://${HOST}/auth/google/callback`
    + '&response_type=code';

  return `${URL}?${queryString}`;
};

// user gives permissions and you get user code
const redirectToGoogleLogin = (req, res) => {
  const url = constructCodeURI();
  console.log('aaa');
  console.log(url);
  res.redirect(url);
};  


const callbackStack = (req, res) => {
  console.log('This is not being hit');
  if (Object.keys(req.query).length > 0) {
    getTokenAndProfileStack(req, res, code, 'google')
      .then(e => res.redirect('http://localhost:5000/forms/ui/list_forms'))
      .catch(e => console.log('google login call back err ' + e))
  }
};

export default callbackStack;
// google login
app.route('/auth/google/login')
  .get(redirectToGoogleLogin);

app.route('/auth/google/callback')
  .get(callbackStack);

Request that goes out (modified to remove sensitive info)

https://accounts.google.com/o/oauth2/v2/auth?client_id=20oogleusercontent.com&scope=openidemail&state=034912340&[email protected]&nonce=02-31905-8&redirect_uri=http://127.0.0.1:5901/auth/google/callback&response_type=code
1

There are 1 best solutions below

0
Itai On

Answering from my own experience:

  1. Add http://localhost (without a port) to the authorized redirect URIs list while keeping the other options you have setup.
  2. Within constructCodeURI make sure to replace 127.0.0.1 with localhost.