当我使用Firebase Unity插件在android中实现FaceBook Auth函数时,它崩溃了

克里斯·赵

我尝试使用Facebook和Firebase Unity插件在android平台上实现用户身份验证。我通过Facebook登录并想要使用Facebook令牌获取Firebase凭据后,应用程序崩溃了。我可以从androidstudio logcat中找到信息:“ A / firebase:Firebase身份验证未初始化,无法创建凭据。请首先创建一个Auth实例。”。似乎Firebase Auth需要一些初始化。但是我已经看到Firebase Unity文档,并且找不到任何初始化信息。任何帮助将不胜感激。

我使用Unity 2018.3.8f1,facebook-unity-sdk-7.15.1和firebase_unity_sdk_5.6.0。因为Facebook Unity SDK无法在Unity编辑器中运行,所以我总是通过Unity编辑器构建apk,并在android模拟器中进行尝试。

这是我的C#代码(U​​nity脚本),几乎所有人都使用firebase文档中的示例代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Facebook.Unity;
using Firebase.Auth;

public class LoginWithFB : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    public Firebase.Auth.FirebaseAuth auth;
    void Awake ()
    {
        Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => 
        {
            var dependencyStatus = task.Result;
            if (dependencyStatus == Firebase.DependencyStatus.Available) 
            {
                // Create and hold a reference to your FirebaseApp,
                // where app is a Firebase.FirebaseApp property of your application class.
                //   app = Firebase.FirebaseApp.DefaultInstance;

                // Set a flag here to indicate whether Firebase is ready to use by your app.
                InitializeFirebase();
            } 
            else 
            {
                UnityEngine.Debug.LogError(System.String.Format("Could not resolve all Firebase dependencies: {0}", dependencyStatus));
                // Firebase Unity SDK is not safe to use here.
            }
        });

        if (!FB.IsInitialized) {
            // Initialize the Facebook SDK
            FB.Init(InitCallback, OnHideUnity);
        } else {
            // Already initialized, signal an app activation App Event
            FB.ActivateApp();
        }
        //auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
    }

    protected void InitializeFirebase() 
    {
        //DebugLog("Setting up Firebase Auth");
        auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
        auth.StateChanged += AuthStateChanged;
        //auth.IdTokenChanged += IdTokenChanged;
        // Specify valid options to construct a secondary authentication object.
        AuthStateChanged(this, null);
    }

    void AuthStateChanged(object sender, System.EventArgs eventArgs) 
    {
        Firebase.Auth.FirebaseAuth senderAuth = sender as Firebase.Auth.FirebaseAuth;
        Firebase.Auth.FirebaseUser user = null;
        //if (senderAuth != null) userByAuth.TryGetValue(senderAuth.App.Name, out user);
        if (senderAuth == auth && senderAuth.CurrentUser != user) 
        {
            bool signedIn = user != senderAuth.CurrentUser && senderAuth.CurrentUser != null;
            if (!signedIn && user != null) 
            {
                Debug.Log("Signed out " + user.UserId);
            }
            user = senderAuth.CurrentUser;
            //userByAuth[senderAuth.App.Name] = user;
            if (signedIn) 
            {
                Debug.Log("Signed in " + user.UserId);
                //displayName = user.DisplayName ?? "";
                //DisplayDetailedUserInfo(user, 1);
            }
        }
    }

    private void InitCallback ()
    {
        if (FB.IsInitialized) {
            // Signal an app activation App Event
            FB.ActivateApp();
            // Continue with Facebook SDK
            // ...
        } else {
            Debug.Log("Failed to Initialize the Facebook SDK");
        }
    }

    private void OnHideUnity (bool isGameShown)
    {
        if (!isGameShown) {
            // Pause the game - we will need to hide
            Time.timeScale = 0;
        } else {
            // Resume the game - we're getting focus again
            Time.timeScale = 1;
        }
    }
    private void AuthCallback (ILoginResult result) 
    {
        if (FB.IsLoggedIn) 
        {
            // AccessToken class will have session details
            var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
            // Print current access token's User ID
            Debug.Log(aToken.UserId);
            Debug.Log(aToken.TokenString);
            Debug.Log(auth);
            // Print current access token's granted permissions
            foreach (string perm in aToken.Permissions) 
            {
                Debug.Log(perm);
            }

            Firebase.Auth.Credential credential = Firebase.Auth.FacebookAuthProvider.GetCredential(aToken.TokenString);
            //auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
            auth.SignInWithCredentialAsync(credential).ContinueWith(task => 
            {
                if (task.IsCanceled) 
                {
                    Debug.LogError("SignInWithCredentialAsync was canceled.");
                    return;
                }
                if (task.IsFaulted) 
                {
                    Debug.LogError("SignInWithCredentialAsync encountered an error: " + task.Exception);
                    return;
                }

                Firebase.Auth.FirebaseUser newUser = task.Result;
                Debug.LogFormat("User signed in successfully: {0} ({1})",
                    newUser.DisplayName, newUser.UserId);
            });

            Debug.Log("Ready goto AsterPlayMainScene");
            Application.LoadLevel("AsterPlayMainScene");
            //SceneManager.LoadScene("AsterPlayMainScene");

        } 
        else 
        {
            Firebase.Auth.Credential credential = Firebase.Auth.FacebookAuthProvider.GetCredential("asadjkahfidhfahfguisodhui");

            Debug.Log("User cancelled login");
            Application.LoadLevel("AsterPlayMainScene");
            //SceneManager.LoadScene("AsterPlayMainScene");
        }
    }

    public void OnFBLoginClick()
    {
        var perms = new List<string>(){"public_profile", "email"};
        FB.LogInWithReadPermissions(perms, AuthCallback);
    }
}

这是从android studio logcat找到的错误日志:

2019-03-31 22:22:25.313 20155-20186/? E/firebase: g_methods_cached
2019-03-31 22:22:25.313 20155-20186/? A/firebase: Firebase Auth was not initialized, unable to create a Credential. Create an Auth instance first.

Also I can find callstack from logcat:
2019-03-31 22:22:25.548 20155-20186/? E/CRASH:  #32  il 00000012  at (wrapper managed-to-native) Firebase.Auth.AuthUtilPINVOKE.FacebookAuthProvider_GetCredential (string) <0x00012>
2019-03-31 22:22:25.548 20155-20186/? E/CRASH:  #33  il 00000013  at Firebase.Auth.FacebookAuthProvider.GetCredential (string) [0x00000] in <7ff3b01c54e9444eb36c9f4350522434>:0
2019-03-31 22:22:25.548 20155-20186/? E/CRASH:  #34  il 000000eb  at LoginWithFB.AuthCallback (Facebook.Unity.ILoginResult) [0x00064] in <9cb866e367a54ac3882a6e4448a28c2e>:0
2019-03-31 22:22:25.548 20155-20186/? E/CRASH:  #35  il 00000069  at Facebook.Unity.CallbackManager.TryCallCallback<Facebook.Unity.ILoginResult> (object,Facebook.Unity.IResult) [0x0000a] in <8c4c3b91cd24414db7bdbc53fc97f955>:0
2019-03-31 22:22:25.548 20155-20186/? E/CRASH:  #36  il 000000f3  at Facebook.Unity.CallbackManager.CallCallback (object,Facebook.Unity.IResult) [0x00046] in <8c4c3b91cd24414db7bdbc53fc97f955>:0
2019-03-31 22:22:25.548 20155-20186/? E/CRASH:  #37  il 00000077  at Facebook.Unity.CallbackManager.OnFacebookResponse (Facebook.Unity.IInternalResult) [0x00021] in <8c4c3b91cd24414db7bdbc53fc97f955>:0
2019-03-31 22:22:25.548 20155-20186/? E/CRASH:  #38  il 00000033  at Facebook.Unity.FacebookBase.OnAuthResponse (Facebook.Unity.LoginResult) [0x00019] in <8c4c3b91cd24414db7bdbc53fc97f955>:0
2019-03-31 22:22:25.548 20155-20186/? E/CRASH:  #39  il 0000003c  at Facebook.Unity.Mobile.MobileFacebook.OnLoginComplete (Facebook.Unity.ResultContainer) [0x00007] in <8c4c3b91cd24414db7bdbc53fc97f955>:0
2019-03-31 22:22:25.549 20155-20186/? E/CRASH:  #40  il 00000046  at Facebook.Unity.FacebookGameObject.OnLoginComplete (string) [0x0000c] in <8c4c3b91cd24414db7bdbc53fc97f955>:0
2019-03-31 22:22:25.549 20155-20186/? E/CRASH:  #41  il 0000005b  at (wrapper runtime-invoke) <Module>.runtime_invoke_void__this___object (object,intptr,intptr,intptr) <0x0005b>

// auth = Firebase.Auth.FirebaseAuth.DefaultInstance; 尝试取消注释此代码

此代码初始化firebase身份验证。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

当我在EditText中添加空间时,它崩溃

来自分类Dev

当我在Android Gingerbread中使用Cookies时,应用崩溃

来自分类Dev

当我在真实设备 iOS 中运行它时应用程序崩溃

来自分类Dev

当我在模拟器中运行它时,应用程序不断崩溃

来自分类Dev

当我尝试使用Firebase时,在Swift5中使用未解析的标识符'Snapshot'

来自分类Dev

当我获得Geocoord时,Android崩溃了

来自分类Dev

当我使用LinearLayout.addView()时,我的Android应用程序崩溃

来自分类Dev

当我使用TabLayout时,我的Android Studio应用程序崩溃,该如何解决?

来自分类Dev

当我转到主屏幕时,使用Facebook Login SDK的应用程序崩溃

来自分类Dev

为什么当我将图像设置为 ImageView 并在 android 7.1.1 上运行它时应用程序崩溃

来自分类Dev

在Three.js中,当使用OrbitControls时,出现Uncaught TypeError:当我尝试实例化它时,THREE.OrbitControls不是构造函数。

来自分类Dev

当我安装了插件时,SSMS 挂起然后崩溃

来自分类Dev

当我将图像上传到Firebase存储并检索下载URL并显示在img标签中时,它显示了多次

来自分类Dev

当我替换正在运行的PostgreSQL服务器使用的.so文件时,它崩溃了

来自分类Dev

当我使用printf()函数在C中编写函数名称时显示的内容

来自分类Dev

当我在 ionic 中输入电子邮件时,我想从 firebase 获取用户 ID

来自分类Dev

当我使用新的android项目appcompt时显示错误

来自分类Dev

当我使用 Jenkins 构建时,Android 构建总是失败

来自分类Dev

Android,当我使用应用内更新时发生 InstallException

来自分类Dev

当我使用 react js 从 firebase 中删除数据时,它会删除数据并输入一个空值。如何避免空条目

来自分类Dev

当我没有在ccsm中启用unity插件时如何应用更改

来自分类Dev

当我没有在ccsm中启用unity插件时如何应用更改

来自分类Dev

当我们在Chrome中测试webrtc时,它使用的是用Android编写的本机代码和jni代码

来自分类Dev

当我想加载列表视图时,Android ArrayAdapter崩溃

来自分类Dev

当我的android应用程序崩溃时获取通知

来自分类Dev

当我调用 EditText 时,android 应用程序崩溃

来自分类Dev

当我使用附加到类Controller的函数时,它在Yii中不起作用

来自分类Dev

当我尝试从函数中的脚本中使用变量时出现错误

来自分类Dev

当我使用firebase.database()。goOnline();时 我得到一个错误

Related 相关文章

  1. 1

    当我在EditText中添加空间时,它崩溃

  2. 2

    当我在Android Gingerbread中使用Cookies时,应用崩溃

  3. 3

    当我在真实设备 iOS 中运行它时应用程序崩溃

  4. 4

    当我在模拟器中运行它时,应用程序不断崩溃

  5. 5

    当我尝试使用Firebase时,在Swift5中使用未解析的标识符'Snapshot'

  6. 6

    当我获得Geocoord时,Android崩溃了

  7. 7

    当我使用LinearLayout.addView()时,我的Android应用程序崩溃

  8. 8

    当我使用TabLayout时,我的Android Studio应用程序崩溃,该如何解决?

  9. 9

    当我转到主屏幕时,使用Facebook Login SDK的应用程序崩溃

  10. 10

    为什么当我将图像设置为 ImageView 并在 android 7.1.1 上运行它时应用程序崩溃

  11. 11

    在Three.js中,当使用OrbitControls时,出现Uncaught TypeError:当我尝试实例化它时,THREE.OrbitControls不是构造函数。

  12. 12

    当我安装了插件时,SSMS 挂起然后崩溃

  13. 13

    当我将图像上传到Firebase存储并检索下载URL并显示在img标签中时,它显示了多次

  14. 14

    当我替换正在运行的PostgreSQL服务器使用的.so文件时,它崩溃了

  15. 15

    当我使用printf()函数在C中编写函数名称时显示的内容

  16. 16

    当我在 ionic 中输入电子邮件时,我想从 firebase 获取用户 ID

  17. 17

    当我使用新的android项目appcompt时显示错误

  18. 18

    当我使用 Jenkins 构建时,Android 构建总是失败

  19. 19

    Android,当我使用应用内更新时发生 InstallException

  20. 20

    当我使用 react js 从 firebase 中删除数据时,它会删除数据并输入一个空值。如何避免空条目

  21. 21

    当我没有在ccsm中启用unity插件时如何应用更改

  22. 22

    当我没有在ccsm中启用unity插件时如何应用更改

  23. 23

    当我们在Chrome中测试webrtc时,它使用的是用Android编写的本机代码和jni代码

  24. 24

    当我想加载列表视图时,Android ArrayAdapter崩溃

  25. 25

    当我的android应用程序崩溃时获取通知

  26. 26

    当我调用 EditText 时,android 应用程序崩溃

  27. 27

    当我使用附加到类Controller的函数时,它在Yii中不起作用

  28. 28

    当我尝试从函数中的脚本中使用变量时出现错误

  29. 29

    当我使用firebase.database()。goOnline();时 我得到一个错误

热门标签

归档