googlesamples / google-signin-unity

Google Sign-In API plugin for Unity game engine. Works with Android and iOS.
Other
414 stars 226 forks source link

Google sign in sometimes passes and sometimes fails with Google.GoogleSignIn+SignInException #218

Open ghost opened 2 years ago

ghost commented 2 years ago

I have a Unity app that uses GoogleSignIn package and Firebase Authentication. It SOMETIMES works, but problem is that it also SOMETIMES fails and I have not been able to get the reproduce steps for getting it to fail. It seems totally random when it fails and GoogleSignIn.DefaultInstance.SignIn() throws the Google.GoogleSignIn+SignInException.

I have also noticed that the sign in might fail on device A but pass on device B at the same time, which suggest that the problem is locally on device rather than on the backend. It does not seem to be related to the google account used to sign in or the amount of google accounts available on the device.

However, I have debugged this and verified that when fail occurs the GoogleSignInStatusCode is 6 (Developer Error). According to the sources this suggest that "The application is misconfigured" and "The developer should look at the logs after this to determine more actionable information". But what configuration is it referring to and how could it even be wrong if the sign in does work sometimes. Also, the Unity logs do not provide any further information.

I have done the following: 1) Set up the Google as authentication provider in Firebase console and added the web client id + secret 2) Set up the SHA-1 and SHA-256 certificate fingerprints for my android app in Firebase console 3) Checked that the package name in Firebase console matches the one in Unity

Here are the relevant parts of my Google sign in code

   public void InitializeAuth() //this gets called at app start
        {
            auth = FirebaseAuth.DefaultInstance;
            auth.StateChanged += AuthStateChanged;
            AuthStateChanged(this, null);

            m_googleSignInConfiguration = new GoogleSignInConfiguration
            {
                WebClientId = "[THE CLIENT ID FROM google-services.json DOWNLOADED FROM FIREBASE CONSOLE].apps.googleusercontent.com", 
                RequestIdToken = true
            };           
        }

 public async Task LogInToFirebaseWithGoogleAccountAsync() //this gets called and is awaited when login button is pressed
        {
            GoogleSignIn.Configuration = m_googleSignInConfiguration; 
            GoogleSignIn.Configuration.UseGameSignIn = false;
            GoogleSignIn.Configuration.RequestIdToken = true;

            try
            {
                var googleUser = await GoogleSignIn.DefaultInstance.SignIn(); //this throws the Google.GoogleSignIn+SignInException 

                Debug.Log($"User signed in with Google successfully: {googleUser.Email}");

                Credential credential = GoogleAuthProvider.GetCredential(googleUser.IdToken, null);

                await auth.SignInWithCredentialAsync(credential);

                Debug.Log($"Logged in to Firebase with Google as {user.DisplayName} - {user.Email}");

                FirebaseLoginCompleted?.Invoke();
            }
            catch (Exception e)
            {
                Debug.LogError("Error in LogInToFirebaseWithGoogleAccountAsync: "+e.Message);
                FirebaseLoginFailed("Google sign in failed - please try again");
            }
        }

Setup: Unity 2021.3.4f1 Firebase Auth 9.0.0

To me this seems like a bug in the GoogleSignIn plugin as there is not much I can do differently in the code. Any help/hints/suggestions would be very welcome as I have been trying to solve this problem for days already!

ghost commented 2 years ago

Oh, I should add that this only happens on Android devices - on iOS I have not experienced this problem.

ghost commented 2 years ago

I finally was able to solve this: turns out that adding the SHA-1 fingerprint from my .keystore file to Firebase SDK setup and configuration was not enough - I also had to add the App signing key certificate SHA-1 fingerprint from Google Play Console. Then the failing stopped. I am not at all sure why it was required, and why the lack of it it caused occasional fails, though.

geoseong commented 2 years ago

I had same problem as yours, so as you described I put following fingerprints to the Firebase project SDK setup on Android

and solved problem without any additional build!!

thank you for mentioning the tips!!

andreymillion commented 1 year ago

The same problem for me. It seems to start randomly. One day - all good, next day - SignInException from nowhere. Still can't fix. Adding all SHAs (App signing and keystore) doesn't help :(