得到错误:类型'Google.GoogleSignIn+SignInException'的DeveloperError异常被抛出--- unity,firebase,谷歌登录?

ddrv8njm  于 2022-12-19  发布在  Go
关注(0)|答案(2)|浏览(868)

我正在开发一个统一的应用程序。我使用firebase谷歌登录方法。基本上谷歌登录是工作和用户被列入谷歌firebase用户登录后。问题是,它是抛出一个错误。因为这不能从firestore获取数据。即使没有firestore代码,应用程序显示错误
出现错误:抛出了类型为“Occare.GoogleSignIn +SignInException”的异常。
到底是什么问题。
下面是我的代码

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);
        }
    }
zz2j4svz

zz2j4svz1#

下面是一个带有Firebase身份验证和GoogleSignIn库的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;
yzxexxkh

yzxexxkh2#

对于要求从@DIGI Byte获取HandleLoginResult的用户,以下是代码,出于调试目的,您可以随意删除try/catch块:

private void HandleLoginResult(Task<FirebaseUser> task)
        {
            try
            {
                if (task.IsCanceled)
                {
                    UnityEngine.Debug.LogError("SignInWithCredentialAsync was canceled.");
                    return;
                }
                if (task.IsFaulted)
                {
                    UnityEngine.Debug.LogError("SignInWithCredentialAsync encountered an error: " + task.Exception.InnerException.Message);
                    return;
                }
                else
                {

                    FirebaseUser newUser = task.Result;
                    UnityEngine.Debug.Log($"User signed in successfully: {newUser.DisplayName} ({newUser.UserId})");
                }
            }
            catch (Exception e)
            {
                if (e != null)
                {
                    UnityEngine.Debug.Log(e.InnerException.Message);
                }
            }
        }

相关问题