Got Error: DeveloperError Exception of type 'Google.GoogleSignIn+SignInException' was thrown --- unity, firebase, google login?

Got Error: DeveloperError Exception of type 'Google.GoogleSignIn+SignInException' was thrown --- unity, firebase, google login?

我正在统一开发一个应用程序。我使用 firebase google 登录方法。基本上 google 登录正常,用户在登录后列在 google firebase 用户日志中。问题是,它抛出一个错误。因此无法从 firestore 中获取数据。即使没有 firestore 代码,应用程序也会显示错误

Got Error: DeveloperError Exception of type 'Google.GoogleSignIn+SignInException' was thrown

可能是什么问题。

下面是我的代码

 public class GoogleSignInDemo : MonoBehaviour
    {
        public Text infoText;
        private string webClientId = "xxxxxxaaaaaaabbbb.apps.googleusercontent.com";
    
        private FirebaseAuth auth;
        private GoogleSignInConfiguration configuration;
    
        private void Awake()
        {
            configuration = new GoogleSignInConfiguration { WebClientId = webClientId, RequestEmail = true, RequestIdToken = true };
            CheckFirebaseDependencies();
        }
    
        private void CheckFirebaseDependencies()
        {
            FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
            {
                if (task.IsCompleted)
                {
                    if (task.Result == DependencyStatus.Available)
                        auth = FirebaseAuth.DefaultInstance;
                    else
                        AddToInformation("Could not resolve all Firebase dependencies: " + task.Result.ToString());
                }
                else
                {
                    AddToInformation("Dependency check was not completed. Error : " + task.Exception.Message);
                }
            });
        }
    
        public void SignInWithGoogle() { OnSignIn(); }
        public void SignOutFromGoogle() { OnSignOut(); }
    
        private void OnSignIn()
        {
            GoogleSignIn.Configuration = configuration;
            GoogleSignIn.Configuration.UseGameSignIn = false;
            GoogleSignIn.Configuration.RequestIdToken = true;
            AddToInformation("Calling SignIn");
    
            GoogleSignIn.DefaultInstance.SignIn().ContinueWith(OnAuthenticationFinished);
        }
    
        private void OnSignOut()
        {
            AddToInformation("Calling SignOut");
            GoogleSignIn.DefaultInstance.SignOut();
        }
    
        public void OnDisconnect()
        {
            AddToInformation("Calling Disconnect");
            GoogleSignIn.DefaultInstance.Disconnect();
        }
    
        internal void OnAuthenticationFinished(Task<GoogleSignInUser> task)
        {
            if (task.IsFaulted)
            {
                using (IEnumerator<Exception> enumerator = task.Exception.InnerExceptions.GetEnumerator())
                {
                    if (enumerator.MoveNext())
                    {
                        GoogleSignIn.SignInException error = (GoogleSignIn.SignInException)enumerator.Current;
                        AddToInformation("Got Error: " + error.Status + " " + error.Message);
                    }
                    else
                    {
                        AddToInformation("Got Unexpected Exception?!?" + task.Exception);
                    }
                }
            }
            else if (task.IsCanceled)
            {
                AddToInformation("Canceled");
            }
            else
            {
                AddToInformation("Welcome: " + task.Result.DisplayName + "!");
                AddToInformation("Email = " + task.Result.Email);
                AddToInformation("Google ID Token = " + task.Result.IdToken);
                AddToInformation("Email = " + task.Result.Email);
                SignInWithGoogleOnFirebase(task.Result.IdToken);
    
                SceneManager.LoadScene(1); //Savad - Load Welcome screen when Google Login
            }
        }
    
        private void SignInWithGoogleOnFirebase(string idToken)
        {
            Credential credential = GoogleAuthProvider.GetCredential(idToken, null);
    
            auth.SignInWithCredentialAsync(credential).ContinueWith(task =>
            {
                AggregateException ex = task.Exception;
//==============Here is the problem
                if (ex != null)
                {
                    if (ex.InnerExceptions[0] is FirebaseException inner && (inner.ErrorCode != 0))
                        AddToInformation("\nError code = " + inner.ErrorCode + " Message = " + inner.Message);
//=======================================
                }
                else
                {
                    AddToInformation("Sign In Successful.");
                }
            });
        }
    
        public void OnSignInSilently()
        {
            GoogleSignIn.Configuration = configuration;
            GoogleSignIn.Configuration.UseGameSignIn = false;
            GoogleSignIn.Configuration.RequestIdToken = true;
            AddToInformation("Calling SignIn Silently");
    
            GoogleSignIn.DefaultInstance.SignInSilently().ContinueWith(OnAuthenticationFinished);
        }
    
        public void OnGamesSignIn()
        {
            GoogleSignIn.Configuration = configuration;
            GoogleSignIn.Configuration.UseGameSignIn = true;
            GoogleSignIn.Configuration.RequestIdToken = false;
    
            AddToInformation("Calling Games SignIn");
    
            GoogleSignIn.DefaultInstance.SignIn().ContinueWith(OnAuthenticationFinished);
        }
    
        private void AddToInformation(string str) { infoText.text += "\n" + str; }
    
        public void SwitchToPhoneSignup()
        {
            SceneManager.LoadScene(2);
        }
    
        public void SwitchToOtp()
        {
            SceneManager.LoadScene(2);
        }
        public void SwitchToEmailSignUP()
        {
            SceneManager.LoadScene(2);
        }
    }

   

这里是 Google 带有 Firebase 身份验证和 Google 登录库的登录代码的工作示例:

    private void SignInWithGoogle(bool linkWithCurrentAnonUser)
       {
          GoogleSignIn.Configuration = new GoogleSignInConfiguration
          {
             RequestIdToken = true,
             // Copy this value from the google-service.json file.
             // oauth_client with type == 3
             WebClientId = "[YOUR API CLIENT ID HERE].apps.googleusercontent.com"
          };
    
          Task<GoogleSignInUser> signIn = GoogleSignIn.DefaultInstance.SignIn();
    
          TaskCompletionSource<FirebaseUser> signInCompleted = new TaskCompletionSource<FirebaseUser>();
          signIn.ContinueWith(task =>
          {
             if (task.IsCanceled)
             {
                signInCompleted.SetCanceled();
             }
             else if (task.IsFaulted)
             {
                signInCompleted.SetException(task.Exception);
             }
             else
             {
                Credential credential = Firebase.Auth.GoogleAuthProvider.GetCredential(((Task<GoogleSignInUser>)task).Result.IdToken, null);
                if (linkWithCurrentAnonUser)
                {
                   mAuth.CurrentUser.LinkWithCredentialAsync(credential).ContinueWith(HandleLoginResult);
                }
                else
                {
                   SignInWithCredential(credential);
                }
             }
          });
       }

该参数用于登录,目的是将新的 google 帐户与当前登录的匿名用户相关联。如果需要,您可以忽略该方法的那些部分。请注意,所有这些都是在 Firebase Auth 库正确初始化后调用的。

来源:https://github.com/googlesamples/google-signin-unity

自述文件页面包含为您的环境获取此设置的分步说明。在遵循这些并使用上面的代码之后,你应该在 android 和 iOS.

上工作

这里是上面代码中使用的SignInWithCredential方法:

    private void SignInWithCredential(Credential credential)
       {
          if (mAuth != null)
          {
             mAuth.SignInWithCredentialAsync(credential).ContinueWith(HandleLoginResult);
          }
       }

`mAuth` is a reference to FirebaseAuth:

    mAuth = Firebase.Auth.FirebaseAuth.DefaultInstance;