Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Goole Sign for iOS or Android with offline access, server always get redirect_uri_mismatch

I'm building a google sign flow with react-native using this lib https://github.com/devfd/react-native-google-signin/.

The lib works great, i can login with google successfully, but we need to ask for offline access to the api, there for the web app we use this flow. https://developers.google.com/identity/sign-in/web/server-side-flow.

And for the web works perfect, but when we try to do the same on the native app, we use the configuration for that in the react-native lib.

GoogleSignin.configure({
      webClientId: 'the client id of the backend server',
      iosClientId: 'the client id of the application',
      offlineAccess: true,
      forceConsentPrompt: true,
      scopes: [
        'https://www.googleapis.com/auth/userinfo.email',
        'https://www.googleapis.com/auth/plus.me',
        'https://www.googleapis.com/auth/gmail.readonly',
        'https://www.googleapis.com/auth/pubsub'
      ]
    })

From this we get a proper response from the lib, that include the:

serverAuthCode: <one-time token to access Google API from the backend on behalf of the user>

But when we try to exchange that code:

const google = require('googleapis');
const OAuth2 = google.auth.OAuth2;

const oauth2Client = new OAuth2(
  process.env.GOOGLE_CLIENT_ID,
  process.env.GOOGLE_CLIENT_SECRET,
  'postmessage'
);

export function getToken (code: string): Promise<any> {
  return new Promise((resolve, reject) => {
     oauth2Client.getToken(code, (err, tokens) => {
        if (!err) {
          resolve(tokens);
        } else {
          reject(err);
        }
      });
  });
}

I Always get and error of redirect_uri_mismatch or invalid_grant.

At this point i don't know what else i need to change. Maybe somebody know what is going on in here.

like image 767
Highercomve Avatar asked Apr 29 '17 14:04

Highercomve


People also ask

How to solve mismatch in redirect Uri in Google Developer Console?

Hence, there was mismatch in redirect URI. I solved it by updating Authorized Redirect URIs in Google Developer Console to www URL. Other common URI mismatch are: Using http:// in Authorized Redirect URIs and https:// as actual URL, or vice-versa

How to enable server side access to the Google APIs?

Enabling Server-Side Access With the earlier Add Sign-Inprocedure, your app authenticates the user on the client side only; in that case, you can access the Google APIs only while the user is actively using your app.

Why am I getting an API error when redirecting to another URL?

The redirect URI (where the response is returned to) has to be registered in the APIs console, and the error is indicating that you haven't done that, or haven't done it correctly. Go to the console for your project and look under API Access.

How to fix Google not communicating with server error?

Go back to Settings –> Account, and sign in to your Google account again. You should be able to log in to use any Google services now without that problem communicating with Google servers error popping up.


1 Answers

Ok, i found the solution. And is pretty easy.

When you use your ServerAuthCode in order to exchanged for a user token, you need to set your return URI on the backend equal to null.

This is what i did at the end with my getToken method. Now everything works like a charm!

export function getToken (code, typeOf = 'web') {
  const redirectUri = (typeOf === 'movil') ? null : 'postmessage';
  const oauth2Client = new OAuth2(
    process.env.GOOGLE_CLIENT_ID,
    process.env.GOOGLE_CLIENT_SECRET,
    redirectUri
  );
  return new Promise((resolve, reject) => {
     oauth2Client.getToken(code, (err, tokens) => {
        if (!err) {
          resolve(tokens);
        } else {
          reject(err);
        }
      });
  });
}
like image 52
Highercomve Avatar answered Sep 28 '22 02:09

Highercomve