C# 如何使用Firebase在unity中创建Facebook登录?
所以我尝试使用Firebase for unity并为我的项目创建Facebook登录 我遵循了firebase文档和Facebook developer中的所有说明,包括将SDK导入到我的项目中,并在Facebook登录中添加C# 如何使用Firebase在unity中创建Facebook登录?,c#,android,firebase,authentication,unity3d,C#,Android,Firebase,Authentication,Unity3d,所以我尝试使用Firebase for unity并为我的项目创建Facebook登录 我遵循了firebase文档和Facebook developer中的所有说明,包括将SDK导入到我的项目中,并在Facebook登录中添加appIDkeyhash,这一切都很好,但一旦我试图将其发送到firebase以保存登录信息,我就会不断收到异常 这是我的代码: using System.Collections; using System.Collections.Generic; using Unity
appID
keyhash,这一切都很好,但一旦我试图将其发送到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");
}
}
}
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()
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用Facebook.Unity;
公共类facebookLogin:MonoBehavior{
//用于初始化
无效唤醒()
{
如果(!FB.i初始化)
{
//初始化Facebook SDK
FB.Init(InitCallback,OnHideUnity);
}
其他的
{
//已初始化,发出应用程序激活应用程序事件的信号
FB.激活EAPP();
}
}
私有void InitCallback()
{
如果(FB.已初始化)
{
//发出应用程序激活应用程序事件的信号
FB.激活EAPP();
//继续使用Facebook SDK
// ...
}
其他的
{
Log(“未能初始化Facebook SDK”);
}
}
私密的虚空隐藏(如图所示)
{
如果(!isgame显示)
{
//暂停游戏-我们需要隐藏
Time.timeScale=0;
}
其他的
{
//继续比赛-我们再次获得焦点
Time.timeScale=1;
}
}
公共登录名()
{
列表权限=新建列表();
权限。添加(“公共_配置文件”);
权限。添加(“电子邮件”);
权限。添加(“用户朋友”);
FB.LogInWithReadPermissions(权限、AuthCallback);
}
私有void AuthCallback(ILoginResult结果)
{
如果(FB.IsLoggedIn)
{
Firebase.Auth.FirebaseAuth Auth=
Firebase.Auth.FirebaseAuth.DefaultInstance;
列表权限=新建列表();
//AccessToken类将具有会话详细信息
var aToken=Facebook.Unity.AccessToken.CurrentAccessToken;
//打印当前访问令牌的用户ID
Log(aToken.TokenString);
Debug.Log(aToken.UserId);
Firebase.Auth.Credential凭据=
Firebase.Auth.FacebookAuthProvider.GetCredential(aToken.TokenString);
使用凭证进行身份验证登录同步(凭证)。继续使用(任务
=> {
如果(task.IsCanceled)
{
LogError(“SignInWithCredentialAsync被删除
已取消。”);
返回;
}
if(task.IsFaulted)
{
Debug.LogError(“遇到SignInWithCredentialAsync
错误:“+task.Exception”);
返回;
}
Firebase.Auth.FirebaseUser newUser=task.Result;
LogFormat(“用户成功登录:{0}({1})”,
newUser.DisplayName,newUser.UserId);
});
//打印当前访问令牌授予的权限
//foreach(aToken.Permissions中的字符串perms)
//{
//Debug.Log(perms);
//}
}
其他的
{
Log(“用户取消登录”);
}
}
}
这里是我的例外:
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");
}
}
}
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遇到错误:System.AggregateException:引发了类型为“System.AggregateException”的异常。
-----------------
Firebase.FirebaseException:已存在具有相同电子邮件地址但不同登录凭据的帐户。使用与此电子邮件地址关联的提供商登录。
调试:日志错误(对象)
facebookLogin:m__0(任务'1)(位于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.c__anostorey4:m__0(任务'1)
System.Threading.Tasks.TaskCompletionSource`1:SetException(异常)
Firebase.Auth.c__anostorey0:m__0()
Firebase.Auth.Future\u用户:SWIG\u CompletionDispatcher(Int32)
Firebase.AppUtilPINVOKE:PollCallbacks()
Firebase.AppUtil:PollCallbacks()
Firebase.Platform.FirebaseAppUtils:PollCallbacks()
Firebase.Platform.firebaseholder:Update()
Firebase.Platform.firebaseMonobhavior:Update()
我不知道出了什么问题。使用CredentialAsync的
登录似乎有问题,但我不知道如何解决。我看不出您的代码有任何问题。正如错误所说,看起来有另一个帐户已经在您尝试登录的Facebook帐户的电子邮件中注册。您的应用程序是否允许其他登录方法
从Firebase帮助:
您可以配置用户是否可以创建使用
相同的电子邮件地址,但链接到不同的登录方法。
例如,如果不允许多个帐户使用同一电子邮件
地址,用户无法创建使用
谷歌帐户的电子邮件地址ex@gmail.com如果已经有
使用电子邮件地址登录的帐户ex@gmail.com和
密码
链接:
你可以在你的电脑里解决这个问题