简体   繁体   中英

Unity Android Unable to login into Facebook after first try

I got the facebook to work in Unity with android but after the first time i log in it doesn't work anymore. I think it's because i don't log out but i don't know why it doesn't stay logged in either so i can use the API. Here is my code :

public class FacebookLogin : MonoBehaviour {


void Awake()
{
    if (!FB.IsInitialized)
    {
        FB.Init(InitCallback, OnHideUnity);
    }
    else
    {
        FB.ActivateApp();
    }
}

private void InitCallback()
{
    if (FB.IsInitialized)
    {
        FB.ActivateApp();
    }
    else
    {
        Debug.Log("Failed to Initialize the Facebook SDK");
    }
}

private void OnHideUnity(bool isGameShown)
{
    if (!isGameShown)
    {
        Time.timeScale = 0;
    }
    else
    {
        Time.timeScale = 1;
    }
}

public void FBLogin()
{
    List<string> perms = new List<string>() { "public_profile", "email", "user_friends" };
    FB.LogInWithReadPermissions(perms, AuthCallback);
}

private void AuthCallback(ILoginResult result)
{
    if (FB.IsLoggedIn)
    {
        var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;

        FB.API("/me?fields=first_name", HttpMethod.GET, getName);
        FB.API("/me/picture?type=square&height=128&width=128", HttpMethod.GET, getProfilePic);
        SceneManager.LoadScene("Main_Menu");
    }
    else
    {
        Debug.Log("User cancelled login");
    }
}

private void getName(IResult result)
{
    LocalDataBase.Name = result.ResultDictionary["first_name"].ToString();
}

private void getProfilePic(IGraphResult result)
{
    LocalDataBase.profilePicture = result;
}

}

Can someone please explain to me how do i save the login token or does it save automatically?

There is no need to save the FB token. Whenever you want to use it, you can get it from these lines of code:

string token = null;
if (FB.IsLoggedIn)
{
    token = Facebook.Unity.AccessToken.CurrentAccessToken;
}
//... use the token here onwards...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM