首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >基于Android的统一Firebase Facebook

基于Android的统一Firebase Facebook
EN

Stack Overflow用户
提问于 2022-03-09 16:57:59
回答 1查看 332关注 0票数 0

将facebook集成为防火墙中的auth提供者(已经有了有效的gmail实现)。下面的代码在联合中运行良好,FB提示输入令牌,手动输入令牌,令牌被传递到firebase,登录可以在控制台中看到,然后加载所需的场景。

当这构建在android设备上时,完全行为不会发生,正确的Facebook令牌将从新登录或现有登录接收到,我们输入SignInWithFacebookOnFirebase函数将该帐户登录到防火墙中,其他任何事情都不会发生,位于(“您的令牌是”+aToken)的调试中;

我很确定这与异步行为有关,也许没有等待任务,但我不确定是什么,任何建议都会很棒!

代码语言:javascript
运行
复制
using System.Collections.Generic;
using UnityEngine;
using Facebook.Unity;
using TMPro;
using UnityEngine.SceneManagement;
using Firebase.Auth;
using Firebase;
    
    public class FacebookAuth : MonoBehaviour
    {
        private FirebaseAuth auth;
        public TMP_Text debug; 
       void Awake ()
    {
      if (FB.IsInitialized) {
        FB.ActivateApp();
      } else {
        //Handle FB.Init
        FB.Init( () => {
          FB.ActivateApp();
        });
      }
      
      CheckFirebaseDependencies();
    }
    
       private void CheckFirebaseDependencies()
        {
            FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
            {
                if (task.IsCompleted)
                {
                    if (task.Result == DependencyStatus.Available)
                        auth = FirebaseAuth.DefaultInstance;
                    else
                        debug.text =("Could not resolve all Firebase dependencies: " + task.Result.ToString());
                }
                else
                {
                    debug.text =("Dependency check was not completed. Error : " + task.Exception.Message);
                }
            });
        }
    
    void OnApplicationPause (bool pauseStatus)
    {
      // Check the pauseStatus to see if we are in the foreground
      // or background
      if (!pauseStatus) {
        //app resume
        if (FB.IsInitialized) {
          FB.ActivateApp();
        } else {
          //Handle FB.Init
          FB.Init( () => {
            FB.ActivateApp();
          });
        }
      }
    }
        private void InitCallBack()
        {
            if(!FB.IsInitialized)
            {
                FB.ActivateApp();
            }
            else
            {
              //  debug.text=("Failed to initialize");
            }
        }
        private void OnHideUnity(bool isgameshown)
        {
            if(!isgameshown)
            {
                Time.timeScale = 0;
            }
            else
            {
                Time.timeScale = 1;
            }
        }    
    
        public void Facebook_Login()
        {
            var permission = new List<string>() { "public_profile", "email" };
            if (!FB.IsLoggedIn)
            {
            FB.LogInWithReadPermissions(permission, AuthCallBack);
            }
            else
            {
              var aToken = AccessToken.CurrentAccessToken.TokenString;
              debug.text=("already logged in - starting game" + aToken);  
              //THIS IS THE PROBLEM ON ANDROID - ITS NOT HAPPENING/async issue? 
              SignInWithFacebookOnFirebase(aToken); 
              
              SceneManager.LoadScene(1);
            }
        }
    
        public void LogOut()
        {
          FB.LogOut(); 
           debug.text=("Logged out of facebook");
        }
    
    
    
            private void SignInWithFacebookOnFirebase(string idToken)
        {
          
            Firebase.Auth.Credential credential = Firebase.Auth.FacebookAuthProvider.GetCredential(idToken);
            auth.SignInWithCredentialAsync(credential).ContinueWith(task => {
              if (task.IsCanceled) {
                  Debug.LogError("SignInWithCredentialAsync was canceled.");
                  return;
              }
              if (task.Exception != null) {
                  Debug.LogWarning("SignInWithCredentialAsync encountered an error: " + task.Exception);
              
                FirebaseException firebaseEx = task.Exception.GetBaseException() as FirebaseException;
                AuthError errorCode = (AuthError)firebaseEx.ErrorCode;
                  string message = "Login Failed!";
                switch (errorCode)
                {
                    case AuthError.AccountExistsWithDifferentCredentials:
                        message = "Your account is already linked to an email address";
                        break;
                      //we can add other conditions here if required to catch exceptions 
                }
                debug.text=(message); 
              }
               else{
                            
    
              Firebase.Auth.FirebaseUser newUser = task.Result;
            
              Debug.LogFormat("User signed in successfully: {0} ({1})",
                  newUser.DisplayName, newUser.UserId);
                  debug.text=("Logged into facebook");  
                  
                
              }
               });
            
        }

    
        private void AuthCallBack(ILoginResult result)
        {
            if(FB.IsLoggedIn)
            {
                var aToken = result.AccessToken.TokenString;
                
                debug.text=("your token is " + aToken); 
                  //THIS IS THE PROBLEM ON ANDROID - ITS NOT HAPPENING/async issue? 
                SignInWithFacebookOnFirebase(aToken); 
                debug.text=("weve signed into firebase");
              
                SceneManager.LoadScene(1);
                
            }
            else
            {
                debug.text=("User Cancelled login");
            }
        }
    
    
    }

编辑-所以这个问题与异步无关,这是一个事实,即在移动设备上正在持久化火基凭证。我取消了我的所有已经工作的谷歌代码下面,我们登录到火基地与我们的facebook信用罚款!所以我需要一些方法来清除我们的这个令牌时,当A)用户注销和B)用户关闭应用程序(干净或不干净),任何帮助将是伟大的!

代码语言:javascript
运行
复制
 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Firebase;
    using Firebase.Auth;
    using Google;
    using TMPro;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    using UnityEngine.UI;
    
    public class GoogleAuth : MonoBehaviour
    {  /*
        public TMP_Text infoText;
        public string webClientId = "<your client id here>";
    
        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()
        {
            
            GoogleSignIn.DefaultInstance.Disconnect();
            infoText.text=("signed out");
        }
    
        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("Cancelled");
            }
            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);
                
            }
        }
    
        private void SignInWithGoogleOnFirebase(string idToken)
        {
            Credential credential = GoogleAuthProvider.GetCredential(idToken, null);
    
            auth.SignInWithCredentialAsync(credential).ContinueWith(task =>
            {
                AggregateException ex = task.Exception;
                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.");
                    SceneManager.LoadScene(1);
                }
            });
        }
    
        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; } */
    }
EN

回答 1

Stack Overflow用户

发布于 2022-03-14 10:43:14

这个问题已经解决了,这是因为我在检查这些脚本中是否存在防火墙依赖项,并且它抛出了一个异常。我使用logcat日志\adb logcat -s统一ActivityManager PackageManager dalvikvm调试发现了这一点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71413158

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档