简体   繁体   中英

Facebook UNITY SDK login issue

I just installed the new version of Unity 4.3 and the new facebook sdk and I can't get it working.

I created the app on facebook, copied over the app id to the unity facebook settings as required and copied the Package Name and Class name back to facebook.

Because the Android Key Hash is empty ( even it shouldn't be ) I used the methods posted by others to create one with openssl. I created it and copied over to facebook as required.

After this I created a small script to be able to login.

// Use this for initialization
void Start () {
    enabled = false;                  
    FB.Init(SetInit, OnHideUnity);  
}

// Update is called once per frame
void Update () {

}

private void SetInit()                                                                       
{                                                                                            
    FbDebug.Log("SetInit");                                                                  
    enabled = true; // "enabled" is a property inherited from MonoBehaviour                  
    if (FB.IsLoggedIn)                                                                       
    {                                                                                        
        FbDebug.Log("Already logged in");                                                    
        OnLoggedIn();                                                                        
    }                                                                                        
}                                                                                            

private void OnHideUnity(bool isGameShown)                                                   
{                                                                                            
    FbDebug.Log("OnHideUnity");                                                              
    if (!isGameShown)                                                                        
    {                                                                                        
        // pause the game - we will need to hide                                             
        Time.timeScale = 0;                                                                  
    }                                                                                        
    else                                                                                     
    {                                                                                        
        // start the game back up - we're getting focus again                                
        Time.timeScale = 1;                                                                  
    }                                                                                        
}    

void OnGUI(){
    if (!FB.IsLoggedIn)                                                                                              
    {                                                                                                                         
        if (GUI.Button(new Rect(179 , 11, 287, 160), "Login to Facebook"))                                      
        {                                                                                                            
            FB.Login("email", LoginCallback);                                                        
        }                                                                                                            
    }    
}

void LoginCallback(FBResult result)                                                        
{                                                                                          
    FbDebug.Log("LoginCallback");                                                          
    Debug.Log("LoginCallback");
    if (FB.IsLoggedIn)                                                                     
    {                                                                                      
        OnLoggedIn();                                                                      
    }                                                                                      
}                                                                                          

void OnLoggedIn()                                                                          
{                                                                                          
    FbDebug.Log("Logged in. ID: " + FB.UserId);                                            
} 

Now when I click on the login button a Facebook window appears requesting permission, after I press ok, it returns, but I'm still not logged in... Can anybody help why is this? Another strange thing I observed that the LoginCallback gets called as soon as I click on the login button, even though I would think it should only when I gave permission. Anyway when I give permission it returns to my app and nothing happens. I can click on the login button again and same thing happens, login callback called, it asks for permisions, I give the permision and returns back, nothing happened. Can anybody help?

Version 4.3.6 of the sdk should fix this problem. It's available here: https://developers.facebook.com/ We are still waiting for it to be approved on the asset store, so the only place to get it right now is from Facebook's site.


Note - it's still broken (5/2014) IF you use a Mac. Just follow the "Rafael solution", discover your hash properly from public void OnLoginComplete(string message). Cheers

So after being frustrated for a few days with having to trace out the key to my phone, I decided to look into what it was doing.

After some research it turned out that when you published to an android device, facebook would use the keystore that was defined in your publish settings, not your .android/debug.keystore file. So I went in and changed the sdk to make it work the proper way. Essentially I changed the SDK to look at the ProjectSettings instead of the debug directory for grabbing the key hash. In the FacebookAndroidUtils.cs I added the following.

// Using the user defined keystore values instead of the debug one.
degbugKeyHash = GetKeyHash( PlayerSettings.Android.keyaliasName, PlayerSettings.Android.keystoreName, PlayerSettings.Android.keyaliasPass, PlayerSettings.Android.keystorePass );

I created a small repo that provides the fix as well as some gui changes to make it more easy to update the key hash.

Github Facebook Unity SDK 6.1 Fix

Update - Fixed a bug with OS X related to escaping spaces on the string path

Hope this helps!

Take the "email" permission out of the login function and try it. Oops I thought I seen the "publish_actions" permission in there too.

Make sure the loginactivity in the manifest is in portrait.

Instead of implementing everything yourself, try using the free and open source SOOMLA Profile plugin for all of your social network needs: https://github.com/soomla/unity3d-profile

Also available on the asset store for download: https://www.assetstore.unity3d.com/en/#!/content/24601

It covers Facebook, Twitter and Google+ and has a unified API for logging in, posting statues, uploading images and getting friends lists. For proper disclosure, I'm one of the founders.

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