Getting idToken 'null' when using google_sign_in in Flutter

Viewed 2197

I am using google_sign_in plug in.

It is working good. but I have a problem.

I need to send idToken to backend server to verify a user.

But IdToken is null from google sign in response.

code is simple

  final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
  final GoogleSignInAuthentication googleAuth =
      await googleUser.authentication;
  print("Printing google user info");
  print(googleAuth.accessToken);
  print(googleAuth.idToken);
  print(googleUser.displayName);

Other properties have a correct value. but idToken is null

I googled for this and says that I need to web client id.

So I did

final _googleSignIn = GoogleSignIn(clientId: webClientId);

Can you guys help?

Am I missing something?

2 Answers

I faced with this Problem for two days and finally. (in my case) this solution works.

https://firebase.flutter.dev/docs/installation/android#installing-your-firebase-configuration-file

  • adding com.google.gms:google-services as a dependency inside of the android/build.gradle

      dependencies {
        // ... other dependencies
        classpath 'com.google.gms:google-services:4.3.8'
      }
    }
    
    
  • adding apply plugin: 'com.google.gms.google-services' in /android/app/build.gradle

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

Before trying to authenticate the user you have to specify the scopes like this

GoogleSignIn _googleSignIn = GoogleSignIn(
            scopes: [
                'email',
                'https://www.googleapis.com/auth/contacts.readonly',
                "https://www.googleapis.com/auth/userinfo.profile"
            ],
        );

then you can request the user to authenticate. to get the isToken call this method

GoogleSignInAuthentication googleSignInAuthentication = await result.authentication;

if yu have correctly configurate the project in the cloud console you should get the id with no problem

Related