Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Did anyone manage to get the id token from google sign in (Flutter)

I am trying to connect my users with my back end server , i used the example from the official google sign in plugin for flutter : https://pub.dartlang.org/packages/google_sign_in

the sign process goes fine and i get the username and email ect.. but i need the id Token to authenticate the user with my server.

Ps: Not using firebase , only google sign in.

Can anyone guide me how to get the id Token ?

like image 488
kageeker Avatar asked Sep 22 '18 17:09

kageeker


People also ask

How do I get the Google ID token in flutter?

You can get access token and id token more simple like this: final result = await _googleSignIn. signIn(); final ggAuth = await result. authentication; print(ggAuth.

How do I get my Google ID token?

An ID token is available when a Credential object's user ID matches the user ID of a Google account that is signed in on the device. To sign in with an ID token, first retrieve the ID token with the getIdTokens method. Then, send the ID token to your app's backend.

How do I use my Google ID token?

To sign in or sign up a user with an ID token, send the token to your app's backend. On the backend, verify the token using either a Google API client library or a general-purpose JWT library. If the user hasn't signed in to your app with this Google Account before, create a new account.

How do I use Google sign in flutter?

Android integration To access Google Sign-In, you'll need to make sure to register your application. You don't need to include the google-services. json file in your app unless you are using Google services that require it. You do need to enable the OAuth APIs that you want, using the Google Cloud Platform API manager.


Video Answer


4 Answers

You can try using this

 _googleSignIn.signIn().then((result){
          result.authentication.then((googleKey){
              print(googleKey.accessToken);
              print(googleKey.idToken);
              print(_googleSignIn.currentUser.displayName);
          }).catchError((err){
            print('inner error');
          });
      }).catchError((err){
          print('error occured');
      });
like image 188
nonybrighto Avatar answered Oct 22 '22 03:10

nonybrighto


You can get access token and id token more simple like this:

final result = await _googleSignIn.signIn();
final ggAuth = await result.authentication;
print(ggAuth.idToken);
print(ggAuth.accessToken);

Or you also can add it to try-catch to handle an error.

try {
   final result = await _googleSignIn.signIn();
   final ggAuth = await result.authentication;
   print(ggAuth.idToken);
   print(ggAuth.accessToken);
} catch (error) {
  print(error);
}
like image 5
icaksama Avatar answered Oct 22 '22 04:10

icaksama


or try like this if id token was null, it worked for me.

As the docs point out you need oauth2 client id of your backend to request idToken or serverAuthCode.

from firebase google sigin in authentication copy the Web SDK configuration add paste in the following to res/values/strings.xml, That should work

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="default_web_client_id">{Web app Client id goes here}</string>
</resources>
like image 4
giveJob Avatar answered Oct 22 '22 04:10

giveJob


To retrieve the Is idToken worked for me:

1. The google-services.json file must be placed in /android/app/

2. you need to add to your /android/app/build.gradle

apply plugin: 'com.google.gms.google-services'

3. and to /android/build.gradle

classpath 'com.google.gms:google-services:4.3.4'

And that's it. GoogleSignIn will return a real idToken instead of null.

font: https://github.com/flutter/flutter/issues/12140#issuecomment-348720774

like image 2
Diogo Souza Avatar answered Oct 22 '22 05:10

Diogo Souza