firebasefluttersha1google-signin

Unhandled Exception: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null, null)


I'm trying to make google login with flutter app, and I got this error. I've already get and put SHA-1 key to OAuth and Firebase and set google-services.json file under app directory. But I still got same problem.

 E/flutter (12401): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null, null)
E/flutter (12401): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
E/flutter (12401): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
E/flutter (12401): <asynchronous suspension>
E/flutter (12401): #2      MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:356:43)
E/flutter (12401): <asynchronous suspension>
E/flutter (12401): #3      GoogleSignIn._callMethod (package:google_sign_in/google_sign_in.dart:235:30)
E/flutter (12401): <asynchronous suspension>
E/flutter (12401): #4      GoogleSignIn.signIn.isCanceled (package:google_sign_in/google_sign_in.dart)
E/flutter (12401): <asynchronous suspension>
E/flutter (12401): 

This is how I set.

GCP OAuth

Firebase setting - I filled all blanks

and I don't know which clientId has to be on this area.

which clientId has to be on this area?

I got 4 clientId because I already used google clientId to make django web login with google


Solution

  • Step 1 : Add this dependency into your pubsec file

    firebase: ^9.0.2
      firebase_core: ^1.2.1
      firebase_database: ^7.1.0
      firebase_auth: ^1.2.0
      font_awesome_flutter: ^9.0.0
      google_sign_in: ^5.0.3
    

    Step : 2 Create a new dart file and paste this code

    import 'package:firebase_auth/firebase_auth.dart';
    import 'package:google_sign_in/google_sign_in.dart';
    
    class FirebaseService {
      final FirebaseAuth _auth = FirebaseAuth.instance;
      final GoogleSignIn _googleSignIn = GoogleSignIn();
    
      Future<String?> signInwithGoogle() async {
        try {
          final GoogleSignInAccount? googleSignInAccount =
              await _googleSignIn.signIn();
          final GoogleSignInAuthentication googleSignInAuthentication =
              await googleSignInAccount!.authentication;
          final AuthCredential credential = GoogleAuthProvider.credential(
            accessToken: googleSignInAuthentication.accessToken,
            idToken: googleSignInAuthentication.idToken,
          );
          await _auth.signInWithCredential(credential);
        } on FirebaseAuthException catch (e) {
          print(e.message);
          throw e;
        }
      }
    
      Future<void> signOutFromGoogle() async{
        await _googleSignIn.signOut();
        await _auth.signOut();
      }
    }
    

    Step 3: call sign in with google function:

    Enable Google sign in Firebase Add your SHA 1 key into Firebase Project