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

I am creating a google sign stream using response-native using this lib https://github.com/devfd/react-native-google-signin/ .

The library works fine, I can log in with Google successfully, but we need to request offline access to the api, there we use this stream for the web application. https://developers.google.com/identity/sign-in/web/server-side-flow .

And to work on the Internet is great, but when we try to do the same in a native application, we use the configuration for this in the interactive 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 the correct answer from lib, which includes:

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

But when we try to exchange this 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);
        }
      });
  });
}

redirect_uri_mismatch invalid_grant.

, . , - , .

+6
1

, . .

ServerAuthCode, , URI , .

, getToken. !

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

Source: https://habr.com/ru/post/1016883/


All Articles