【问题标题】:Unity firebase facebbok authenticationunity firebase facebbok 身份验证
【发布时间】:2018-01-19 10:10:00
【问题描述】:

我不了解 firebase 网站上关于统一 firebase facebook 身份验证的文档。

我在统一论坛上找到了这段代码:

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

public class facebookAuthenticator : MonoBehaviour
{
public Text userID;
private FirebaseAuth facebookAuth;

private void Awake()
{
    if (!FB.IsInitialized)
        FB.Init();
    else
        FB.ActivateApp();
}

public void logIn()
{
    FB.LogInWithReadPermissions(callback: onLogIn);
}

private void onLogIn(ILoginResult result)
{
    if (FB.IsLoggedIn)
    {
        AccessToken tocken = AccessToken.CurrentAccessToken;
        userID.text = tocken.UserId;
        Credential credential = FacebookAuthProvider.GetCredential(tocken.TokenString);
    }
    else
        Debug.Log("log failed");
}

public void accessToken(Credential firebaseResult)
{
    FirebaseAuth auth = FirebaseAuth.DefaultInstance;

    if (FB.IsLoggedIn)
        return;

    auth.SignInWithCredentialAsync(firebaseResult).ContinueWith(task =>
    {
        if (task.IsCanceled)
        {
            Debug.LogError("SignInWithCredentialAsync was canceled.");
            return;
        }
        if (task.IsFaulted)
        {
            Debug.LogError("SignInWithCredentialAsync encountered an error: " + task.Exception);
            return;
        }

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

}

我对如何调用它感到困惑。 在执行此代码之前我必须采取哪些步骤?我该怎么称呼它?我是否只需将其附加到按钮并让用户输入字段填写?请帮忙,谢谢。

【问题讨论】:

    标签: c# facebook firebase unity3d firebase-authentication


    【解决方案1】:

    首先,您应该从回调结果中收到访问令牌。 其次,您应该在通过accessToken(credential); 按照您的代码收到令牌后调用firebase auth 方法,因此您的onLogin 方法应该是这样的

    private void onLogIn(ILoginResult result)
    {
       if (FB.IsLoggedIn)
       {
          AccessToken tocken = result.AccessToken;//received access token
          userID.text = tocken.UserId;
          Credential credential = FacebookAuthProvider.GetCredential(tocken.TokenString);
          accessToken(credential);//invoke auth method
       }
       else
          Debug.Log("log failed");
    }
    

    【讨论】:

      【解决方案2】:

      我正在做与此非常相似的事情,是的,您几乎必须将登录功能分配到一个按钮中,以便一旦单击它就会开始 Facebook 身份验证,但对于 firebase 部分,您必须获取访问令牌这是另一种方式。我建议研究这样的事情: http://greyzoned.com/tutorials/facebook-sdk-7-3-0-in-unity-5-3-login-username-profile-picture-tutorial/ 关于如何做到这一点的想法。祝你好运!

      【讨论】:

      • 建议在答案中包含必要的内容,而不是使用参考链接
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-21
      • 1970-01-01
      • 2020-05-22
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多