Search Unity

How to fix Facebook SDK Android Login

Discussion in 'Android' started by 00christian00, Jan 16, 2014.

  1. 00christian00

    00christian00

    Joined:
    Jul 22, 2012
    Posts:
    1,035
    I just spent the whole day trying to understand how make the facebook sdk works on my Android device.

    So here is the solution for anybody in my same shoes:
    Avoid the user generated key hash, it doesn't work( at least for me and probably many other). Anyway it's made just for debugging and it's not explained how to use for release.

    To generate the hash use this on Windows.
    And this on Mac:
    It will ask the password for the keystore, if you don't know yours just create a new keystore.
    The path after -keystore is the keystore you want to use, and the word after -alias is your release key, so you can use the same method when publishing.

    Copy the hash that it will give you in the facebook app settings and you are done.


    More details here(the facebook unity sdk guide is totally useless):
    https://developers.facebook.com/docs/android/getting-started/
     
    MohammadMansour_ likes this.
  2. newlife

    newlife

    Joined:
    Jan 20, 2010
    Posts:
    1,081
    Hello christian,
    I already followed the sdk guide, but nothing has changed. Btw, the command for windows needs a fix (at least on windows 8 - 8.1):

    if yuo dont put "C:\" before %HOMEPATH% it doesn't ask you for a password.

    Anyway, tested on a LG P700 and a samsung galaxy wonder, login doesn't work.
     
  3. 00christian00

    00christian00

    Joined:
    Jul 22, 2012
    Posts:
    1,035
    Adding c: was what I did in the editor script to make it work, but on the commandline I didn't have that issue on Windows 7.
    Are you sure the password you are using is correct? Did you try regenerating the debug.keystore from unity?
     
  4. newlife

    newlife

    Joined:
    Jan 20, 2010
    Posts:
    1,081
    from https://developers.facebook.com/docs/android/getting-started/:

    "You will be prompted for a password. This should be 'android' without quotes."

    anyway, how do you regenerate the debug.keystore from unity?
     
  5. 00christian00

    00christian00

    Joined:
    Jul 22, 2012
    Posts:
    1,035
    On the player settings where you can set android specific settings like icons, there are two buttons, Use existing keystore, and create new keystore.
    Try creating a new one, I didn't test with the existing one, it could be the password isn't really android.
     
  6. newlife

    newlife

    Joined:
    Jan 20, 2010
    Posts:
    1,081
    Tried using both C:\%HOMEPATH%\.android\debug.keystore and a new one.
    On a samsung galaxy (with facebook app installed) after clicking login, it ask for permissions (faceboook_test wants to access bla bla). Clicked ok, it return to the app page with login false.
    On a Android 4.2.2 emulator (without facebook app installed) clicking on login crashes the app.

    UPDATE: it seems that there is a weird error in the permission dialog: even if i press "OK" it comes back as cancel was pressed (in the log it appears: "login cancelled by player").
     
    Last edited: Jan 20, 2014
  7. 00christian00

    00christian00

    Joined:
    Jul 22, 2012
    Posts:
    1,035
    I had the same issue until I properly configured it. Login cancelled by user is because of the hash.
    Did you copy the hash generated by the command in the facebook app settings?

    I generated a new one and overwritten the existing one at the HOMEPATH location using android as password, then generated the hash with the command line and copied it in the facebook dev center.
     
  8. Sampora

    Sampora

    Joined:
    May 28, 2013
    Posts:
    2
    Battling with the same issue i think.
    Did you notice that things work differently depending on whether you have the native android Facebook app ("FB app" from now on) installed on your device (or maybe that's common knowledge but just not mentioned on this thread :p )? For me the login works if i don't have the native FB app installed.
    Maybe that's the problem "newlife" is having too.


    I have an old, already released app, into which i'm trying to implement FB login. I regenerated the hash with my "release" keystore and openssl.

    The problem is that I can't perform the "first time flow" if I have the native FB app installed. Everything works perfectly if I log into my game without the FB app installed for the first time (so that FB SDK brings up the browser for logging in, instead of the FB app). After one successful login, I can install the FB app on my device and still my game continues to log in without problems.
    With the native FB app login screen, my game doesn't seem to be able to get the permissions right.

    I guess i gotta try to regenerate the hashes again in case i made a mistake or something. Otherwise this is getting really frustrating.
     
  9. NoseKills

    NoseKills

    Joined:
    Jun 4, 2013
    Posts:
    25
    EDIT ^^ posted from my other account accidentally

    OK so the hashes surely aren't the problem for me.
    In Eclipse's LogCat i'm able to see the calls and if I intentionally screw up the login call, i can clearly see the error messages now.

    With my original code the login call goes through ok but it just doesn't seem to return the FBid and FB.isLoggedIn stays false

    here's the LogCat of the reply i get

    sending to Unity OnLoginComplete({"key_hash":"n0RW5G6...8HF8AjM8=\n","access_token":"CAAG3N......jxzwZDZD","opened":true})

    even though the reply seems to contain the access_token, the FB.accessToken stays empty
     
    Last edited: Feb 17, 2014
  10. splendor

    splendor

    Joined:
    Mar 21, 2014
    Posts:
    1
    I understood why when the device is installed a facebook app, my app can't login facebook.

    + First : software OpenSSL create keyhass wrong. I have downloaded OpenSSL from here : http://code.google.com/p/openssl-for-windows/downloads/list , and it creates keyhash wrong.
    I have used other OpenSSL sofware, you can download it here : https://www.dropbox.com/sh/t6tt44dc2nm6vza/6mp1W74GiZ , it is create keshash right.

    + Second: In class FacebookAndroidUtil.cs , line 62, you add System.Environment.GetEnvironmentVariable("HOMEDRIVE") before System.Environment.GetEnvironmentVariable("HOMEPATH"). Facebook SDK requires a file debug.keystore in : C:\Users\UserName\.android\debug.keystore.
    If the Unity Editor, Edit Setting Facebook has yet to create android debug hash key. Then use the Eclipse Software, create and run onr any project. Eclipse will generate a debug.keystore. If it is't create in folfer : C:\Users\UserName\.android\debug.keystore , then find it, coppy and paste into C:\Users\UserName\.android\debug.keystore

    + Third: publish your app in Facebook, so be sure it is:"This app is public and available to all users".
     
  11. sheffieldlad

    sheffieldlad

    Joined:
    Oct 9, 2013
    Posts:
    154
    @splendor can you post a copy of your FacebookAndroidUtil.cs file please? I'm still having problems.
     
  12. sheffieldlad

    sheffieldlad

    Joined:
    Oct 9, 2013
    Posts:
    154
    Fixed it! I ran adb logcat *:W and got the correct hash from the logs.

    @splendor the OpenSSL file you linked to got flagged up as a trojan on my anti virus..
     
  13. newlife

    newlife

    Joined:
    Jan 20, 2010
    Posts:
    1,081
    Glad to know! :)
    Can you please describe in detail the process you follow to make this work?
    thanks.
     
  14. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    Sampora, you are 10000% correct,

    #WORKS GREAT IF YOU do NOT have THE FB APP INSTALLED ON THE ANDROID DEVICE

    # WILL NOT WORK IF THE USER HAPPENS TO HAVE THE FB APP INSTALLED.

    what a drama!
     
  15. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
  16. MaZy

    MaZy

    Joined:
    Jun 29, 2012
    Posts:
    105
    No I didn't. I still cannot log in. On Windows it works nice. It asks for token and then I can see friends and co.. but on my android device cannot login. I just get "Cancelled by Player" something like that.

    I have a platfomr there. I put package name, class name, key hash too. Everything took from unity editor. But doesn't work :(
     
  17. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
  18. MaZy

    MaZy

    Joined:
    Jun 29, 2012
    Posts:
    105
  19. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    Hi Mazy -- obviously I'm talking about the ACCEPTED ANSWER, the one in green

    It's extremely fortunate Rafael figured that out ! it's the only solution if you're on Mac, BTW.
     
  20. wfs

    wfs

    Joined:
    Dec 6, 2013
    Posts:
    15
    My solution was:

    1 . Get the fingerprint SHA1 (keytool -printcert -file CERT.RSA)
    2 . Copy the SHA1 HEX and convert that code to Base 64 (http://tomeko.net/online_tools/hex_to_base64.php)
    3 . You should copy the fingerprint in base 64 in the Android key hashes on your facebook app configuration.

    That was all and it worked perfectly!
     
  21. johnnymoha

    johnnymoha

    Joined:
    Jun 27, 2013
    Posts:
    2
    Did ANYONE solve the problem with the FB app needing to be uninstalled for first login?
     
  22. johnnymoha

    johnnymoha

    Joined:
    Jun 27, 2013
    Posts:
    2
    BUUUUUUUUUUUUMP
     
  23. Noztradamuz

    Noztradamuz

    Joined:
    May 15, 2013
    Posts:
    6
    I have managed to solve it, by changing the line 70 in the FacebookAndroidUtil.cs script
    from:
    vararguments = @"""keytool -storepass {0} -keypass {1} -exportcert -alias {2} -keystore {3} | openssl sha1 -binary | openssl base64""";
    to:
    vararguments = @"""keytool -storepass {0} -keypass {1} -exportcert -alias {2} -keystore {3} | openssl md5 -binary | openssl base64""";
    got the correct MD5 Hash key from unity and added to Facebook Developoer page! :) not first login needed without the FB app :)
     
  24. Unity Ninja

    Unity Ninja

    Joined:
    Jun 14, 2014
    Posts:
    14
    What is the user generated key hash?
     
  25. VonTala

    VonTala

    Joined:
    Feb 17, 2015
    Posts:
    10
    thaaaaaaaaaaaaaaaaaaaaaanks
     
  26. payalzariya07

    payalzariya07

    Joined:
    Oct 5, 2018
    Posts:
    85
    how to do Facebook login already? And Get user name and profile if already Player login.
     
  27. newatlanta19006

    newatlanta19006

    Joined:
    Feb 23, 2019
    Posts:
    13
    Something that may be the problem for folks. I ended up needed a whopping 4 hashes to make all variants work for me:
    Editor (the hash that shows up in the Facebook Settings in the unity plugin)
    debug keystore (the one that located in .android/debug.keystore)
    release keystore (my release keystore)
    google play key (THIS IS THE ONE I FORGOT)

    I have automatic signing turned on my Google Play dashboard and there is another sha1 there. So I ran that through this site: http://tomeko.net/online_tools/hex_to_base64.php
    After adding the 4th one to FB it started working for me.

    The hash is located in the Google Play dashboard: Release Management=>App Signing=>App Signing Certificate
     
  28. victorbadea

    victorbadea

    Joined:
    Feb 10, 2020
    Posts:
    3
    Thank you kindly!