c# - 如何使用 Firebase 创建统一的 Facebook 登录信息?

标签 c# android firebase authentication unity-game-engine

因此,我尝试使用 Firebase 进行统一并为我的项目创建 Facebook 登录信息。

我已遵循 Firebase 文档和 Facebook 开发人员中的所有说明,包括将 SDK 导入我的项目并在 Facebook 登录中添加 appID keyhashes 单独工作正常,但一旦我尝试将其发送到 Firebase 以保留登录信息我不断遇到异常。

这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Facebook.Unity;

public class facebookLogin : MonoBehaviour {

// Use this for initialization
void Awake()
        {
        if (!FB.IsInitialized)
            {
                // Initialize the Facebook SDK
                FB.Init(InitCallback, OnHideUnity);
            }
            else
            {
                // Already initialized, signal an app activation App Event
                FB.ActivateApp();
            }
        }
        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;
            }
        }
        public void FBlogin()
        {
            List<string> permissions = new List<string>();
            permissions.Add("public_profile");
            permissions.Add("email");
            permissions.Add("user_friends");
            FB.LogInWithReadPermissions(permissions, AuthCallback);
        }
        private void AuthCallback(ILoginResult result)
        {
            if (FB.IsLoggedIn)
            {
                Firebase.Auth.FirebaseAuth auth = 
Firebase.Auth.FirebaseAuth.DefaultInstance;
                List<string> permissions = new List<string>();

                // AccessToken class will have session details
                var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
                // Print current access token's User ID
                Debug.Log(aToken.TokenString);
                Debug.Log(aToken.UserId);


                Firebase.Auth.Credential credential =

Firebase.Auth.FacebookAuthProvider.GetCredential(aToken.TokenString);
                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);
                });
                // Print current access token's granted permissions
                //foreach(string perms in aToken.Permissions)
                //{
                // Debug.Log(perms);
                //}
            }
            else
            {
                Debug.Log("User cancelled login");
            }
        }
    }

这是我的异常(exception)情况:

SignInWithCredentialAsync encountered an error: System.AggregateException: Exception of type 'System.AggregateException' was thrown.
-----------------
Firebase.FirebaseException: An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address.

UnityEngine.Debug:LogError(Object)
facebookLogin:<AuthCallback>m__0(Task`1) (at Assets/Script/facebookLogin.cs:82)
System.Threading.Tasks.TaskCompletionSource`1:SetException(AggregateException)
Firebase.Internal.TaskCompletionSourceCompat`1:SetExceptionInternal(TaskCompletionSource`1, AggregateException)
Firebase.Internal.TaskCompletionSourceCompat`1:SetException(TaskCompletionSource`1, AggregateException)
Firebase.Auth.FirebaseAuth:CompleteFirebaseUserTask(Task`1, TaskCompletionSource`1)
Firebase.Auth.<SignInWithCredentialAsync>c__AnonStorey4:<>m__0(Task`1)
System.Threading.Tasks.TaskCompletionSource`1:SetException(Exception)
Firebase.Auth.<GetTask>c__AnonStorey0:<>m__0()
Firebase.Auth.Future_User:SWIG_CompletionDispatcher(Int32)
Firebase.AppUtilPINVOKE:PollCallbacks()
Firebase.AppUtil:PollCallbacks()
Firebase.Platform.FirebaseAppUtils:PollCallbacks()
Firebase.Platform.FirebaseHandler:Update()
Firebase.Platform.FirebaseMonoBehaviour:Update()

我不知道出了什么问题。 SignInWithCredentialAsync 似乎有问题,但我不知道如何修复它。

最佳答案

我没有发现您的代码有任何问题。正如错误所示,似乎有另一个帐户已使用您尝试登录的 Facebook 帐户的电子邮件注册。您的应用程序允许其他登录方式吗?

来自 Firebase 帮助:

You can configure whether users can create multiple accounts that use the same email address, but are linked to different sign-in methods. For example, if you don't allow multiple accounts with the same email address, a user cannot create a new account that signs in using a Google Account with the email address [email protected] if there already is an account that signs in using the email address [email protected] and a password.

链接:https://support.google.com/firebase/answer/6400716?hl=en

您可以在 Firebase 控制台中修复此问题。在身份验证 > 登录方法中,向下滚动,您将看到“每个电子邮件地址多个帐户”部分。允许它,错误应该被修复。

关于c# - 如何使用 Firebase 创建统一的 Facebook 登录信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51407983/

相关文章:

android - xml onClick 中的 Kotlin 不起作用

c# - xamarin,visual studio 2015 组件元素未声明

android - 如何在 Firestore 中查询包含已发送参数的文档?

c# - 如何将 Guid.Empty 传递到参数化单元测试中?

C# Gecko 选择特定的列表框或组合框

c# - 为什么隐式使用 ToString() 不会导致异常?

firebase - Firestore文档错误-初始化程序中只能访问静态成员

c# - WPF:Scrollviewer里面的ListView;布局问题

java - 标题、菜单项未显示在 fragment 内的 ActionBar 上

ios - Firebase iOS - 在调用 observeSingleOfEventType 时保证 FDataSnapshot