freewebwithme
freewebwithme

Reputation: 569

Getting idToken 'null' when using google_sign_in in Flutter

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?

Upvotes: 10

Views: 12326

Answers (7)

raw
raw

Reputation: 149

In my case (using Android) explicitly telling the package to request the ID token by using the serverClientId did help.

var googleSignIn = GoogleSignIn(
  scopes: <String>[
    'email',
    'https://www.googleapis.com/auth/userinfo.profile',
  ],
  // Provide the client ID of the web application from Google Cloud Console
  serverClientId: 'YOUR_WEB_CLIENT_ID',
);

The serverClientId should be the OAuth 2.0 Web Client ID (not the Android client ID) from your Google Cloud Console. This will ensure that Google returns an ID token as part of the authentication process.

Upvotes: 2

Pratik Butani
Pratik Butani

Reputation: 62421

As I got the solution in Flutter today:

I've passed the GoogleSignIn Parameters as below:

final GoogleSignIn googleSignIn = GoogleSignIn(
  clientId:
      '750800371744-xxxxx.apps.googleusercontent.com',
  scopes: [
    'email',
    'https://www.googleapis.com/auth/contacts.readonly',
  ],
);

This is how you'll get the client id:

enter image description here

And used that as below:

final GoogleSignInAccount? googleSignInAccount =
    await googleSignIn.signIn();

if (googleSignInAccount != null) {
  log(googleSignInAccount.toString());

  final GoogleSignInAuthentication googleSignInAuthentication =
      await googleSignInAccount.authentication;

  user = User(googleSignInAccount.displayName ?? '',
      googleSignInAccount.email ?? '', googleSignInAccount.photoUrl ?? '');

  log("Acccess Token : ${googleSignInAuthentication.accessToken}");
  log("idToken : ${googleSignInAuthentication.idToken}");
}

And I got both the token successfully. Hope it will helps.

Upvotes: 7

Luca K&#246;ster
Luca K&#246;ster

Reputation: 360

Hi after a long time i although found a solution.

In short there was a problem with the Firebase setup, which doesn't occure because i used:

await Firebase.initializeApp(options: 
defaultFirebaseOptions.currentPlatform);

Which makes my code work but hide the real failure, which occurs when i just use:

await Firebase.initializeApp();

But actually the first approach is the better one but long story short.

There is something wrong with the firebase sdk setup. Follow this official tutorial for setup first: https://firebase.google.com/docs/android/setup

Now i did not worked. But if i changed the build.gradle (root / project) from:

plugins {
  id 'com.google.gms.google-services' version '4.4.0' apply false
}

to

plugins {
  id 'com.google.gms.google-services' version '4.3.15' apply false
}

it worked. Does this make sense? For me not. Am i happy that it works now? Yes :)

Upvotes: 12

Sajjad
Sajjad

Reputation: 3218

Edit 27/12/2023

use flutter Cli

https://firebase.google.com/docs/flutter/setup


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'
    
    

Upvotes: 13

Wilyanto Liang
Wilyanto Liang

Reputation: 269

Here is my solutions:

  1. Since I generate my google-service.json from flutterfire CLI, my first google-service.json has empty array of oauth_client.
  2. Then I added my Android SHA1 fingerprint in my Firebase Project Settings. After that I checked on https://console.cloud.google.com/apis/credentials, the OAuth 2.0 is added for my Android Client with SHA1 autofilled there
  3. Here is the crucial part, re-generate your google-service.json from Firebase Project Settings (since your OAuth 2.0 just added on point #2, but your google-service.json is generated on point #1). Recheck your google-service.json, the oauth_client key inside should be filled with some OAuth 2.0 that you just added on point #2

Upvotes: 5

Eel
Eel

Reputation: 1449

For me, I delete the google sign in provider from the firebase project, (delete it entirely instead of disabling it) and delete all unused oauth 2.0 services, including the web application, then re-enable the google sign in provider and re-init firebase project using flutter-fire and it should work

Upvotes: 0

Patrick G.
Patrick G.

Reputation: 475

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

Upvotes: 3

Related Questions