firebase-realtime-database - 无法在我的项目中获取用户访问令牌 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);
});
真的需要帮助,谢谢。
解决方案
推荐阅读
- java - 从另一个类中的方法更改字段属性
- angular - 如何在 docker 中为 Angular 应用程序公开端口
- vue.js - vue.js Toast 未定义(使用 mint-ui 时)
- php - PHP - Laravel - Trait 方法标记尚未应用,因为与 App\User 上的其他 trait 方法存在冲突
- angular - 在 html datepicker 上动态设置 minDate/maxDate
- ms-access - 禁用与 Ms Access 中的表单连接的所有控件
- java - 更新代码以包含 wait.until 条件
- php - WordPress Inside Subapp Angular js 和 web api 在 IIS 中不起作用?
- c# - WPF MVVM:如何禁用单击的按钮并启用所有其他按钮?
- typescript - TypeScript:普通和不可变js对象的深度遍历