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.
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
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.
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.
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.
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);
}
});
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With