- Home /
Connect facebook login to firebase in unity
I want in my unity game with firebase facebook login when user login through facebook there user name, email & profile picture save in firebase. I tried from firebase unity login auth docs but i cant get any thing from there. On there docs to difficult to understand for beginners.
using UnityEngine;
using Facebook.Unity;
using UnityEngine.UI;
using Firebase.Auth;
public class FacebookManager : MonoBehaviour
{
public Text userIdText;
private FirebaseAuth mAuth;
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;
userIdText.text = tocken.UserId;
Credential credential =FacebookAuthProvider.GetCredential(tocken.UserId);
}
else
{
Debug.Log("Login 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);
});
}
}
Please help me i am beginner in coding
Answer by b45h05h4 · Jun 21, 2017 at 12:34 PM
Hello @LZYGYZ
I had some problems but eventually I could login and get the profile picture and so,
first of all you need to fix line 41 and send the token string not the userId
Credential credential = FacebookAuthProvider.GetCredential(tocken.TokenString);
next I failed to use firebase returned data to get the data I need, So it was a better idea to use facebook APIs for that! inspired by this https://stackoverflow.com/questions/37384026/android-firebase-auth-get-users-photo
I got the profile picture for instance like them I had to use the next combination
const string fbLink = "https://graph.facebook.com/";
const string fbPic = "/picture?type=large";
string photoURL = string.Format("{0}{1}{2}", fbLink , token.UserId, fbPic);
That's all, if you need anything else let me know!
hey @b45h05h4 is the above code valid for the firebase facebook auth in unity?
I too am looking at the documentation in firebase and can't really wrap my head around it.
Also, if possible could we share facebook info and i could ask you some questions?? thanks!
What is the token string? Where do I get that? I didn't understand how you do that within Unity.
Answer by tungnguyendev · Mar 29, 2018 at 05:19 PM
You need to use Facebook SDK for Unity, login and after you logged, get token string to continues with firebase auth.