首页 > 解决方案 > 无法在我的项目中获取用户访问令牌 facebook(使用 firebase),或者可以获取但无法使其工作

问题描述

我正在与 firebase 统一制作 2D 游戏。我无法成功制作自己的代码,所以我在互联网上找到了一些东西,但它也不起作用。所以,我尝试根据 facebook 网站做一些事情,我得到了下面的代码(结果是调试日志有效,但我卡住了,无法通过登录屏幕。我错过了什么?)

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


public class FacebookManager : MonoBehaviour
{

    FirebaseAuth auth;
    FirebaseUser User;
    // Awake function from Unity's MonoBehavior
    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()
    {
        var perms = new List<string>() { "email" };
        FB.LogInWithReadPermissions(perms, AuthCallback);
        //FB.Android.RetrieveLoginStatus(LoginStatusCallback);
        Debug.Log("SUCCEED TO LOGIN WITH FACEBOOK");
        
    }
    private void AuthCallback(ILoginResult result)
    {
        if (FB.IsLoggedIn)
        {
            // AccessToken class will have session details

            var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
            Firebase.Auth.Credential credential =
            Firebase.Auth.FacebookAuthProvider.GetCredential(aToken.ToString());
            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 User ID
            Debug.Log(aToken.UserId);
            // Print current access token's granted permissions
            foreach (string perm in aToken.Permissions)
            {
                Debug.Log(perm);
            }
        }
        else
        {
            Debug.Log("User cancelled login");
        }

    }
    
    public void authwithfirebase(string accesstoken)
    {
        auth = FirebaseAuth.DefaultInstance;
        Firebase.Auth.Credential credential = Firebase.Auth.FacebookAuthProvider.GetCredential(accesstoken);
        auth.SignInWithCredentialAsync(credential).ContinueWith(task =>
        {
            if (task.IsFaulted)
            {
                Debug.Log("singin encountered error" + task.Exception);
            }
            Firebase.Auth.FirebaseUser newuser = task.Result;
            Debug.Log(newuser.DisplayName);
        });
    }
}

我也有来自 firebase 的这段代码,我真的不知道应该放在哪里:

Firebase.Auth.Credential credential =
    Firebase.Auth.FacebookAuthProvider.GetCredential(accessToken);
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);
});

真的需要帮助,谢谢。

标签: firebase-realtime-databasefacebook-unity-sdk

解决方案


推荐阅读