19

In my android application, I used facebook login. It is first time ever I used it.

The login functionality is working fine in release apk file. Also, I have generated key hash by using keytool, openssl:-

keytool -exportcert -alias "MyAppAlias" -keystore "Path to keystore" | 
openssl sha1 -binary | openssl base64

I added the generated key hash in App settings on my Facebook developer account. Now when I am generating Signed apk, Facebook login is working fine, but after publishing the same apk on Play Store, Facebook login is not working, it's simply redirecting to activity from where it was called(My App's login activity) without any crashes or not responding message.

Thanks.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
vChamps
  • 963
  • 2
  • 7
  • 14
  • did u made your app as public in developer account – Sunil P Jul 29 '17 at 10:53
  • Yes, I have made it public. – vChamps Jul 29 '17 at 10:55
  • Can you confirm whether you chose to publish via Google Play App Signing or manual signing of application? – Mani Jul 29 '17 at 11:02
  • try this @vChamps it may help you https://stackoverflow.com/questions/14994433/facebook-not-working-with-exported-signed-apk-file make sure you have submitted your keyHash to facebook as well – Sunil P Jul 29 '17 at 11:04
  • @Mani I generated signed apk from android studio using the keystore I created. Then while Publishing on Play store, I found a link named APP SIGNING, so signed it there also. – vChamps Jul 29 '17 at 11:23
  • @vChamps you can [try this](https://stackoverflow.com/questions/44671778/published-app-on-play-store-cant-communicate-with-google-maps-api-and-facebook/44672565#44672565) sir. This is your issue it will resolve your problem. – Andy Developer Jul 29 '17 at 11:25
  • @AndyDeveloper I checked the link you mentioned. The issue is not regarding Google Maps API, I got the point that while signing in from Google Play, it generates new SHA-1 key, but my problem is with Facebook, where we don't use SHA-1 key, we use KeyHash as per my knowledge. But any comment over it is welcome. – vChamps Jul 29 '17 at 11:33
  • @vChamps keyhash is generate from SHA. If you see the question it is regarding the google map api and facebook api too. You can see other developers answer there. – Andy Developer Jul 29 '17 at 11:37
  • @AndyDeveloper For Facebook, there are only two answer 1) Generate keyhash from Java code - that is for debug mode - I already have done it. 2) Generate keyhash from keytool, openssl - I already have done this too. And added both key hashes to settings in Facebook developer account. Is there any way to generate KeyHash by SHA-1? – vChamps Jul 29 '17 at 11:58

8 Answers8

57

Finally, I resolved the issue.

Reason Behind this issue While publishing an App to play store, I did APP SIGNING from Google Play, hence new SHA-1 key was created there.

To see this key, go to Google Play Console, select your app, then Release Management -> App Signing

On this page, I got new SHA-1 key under section "App signing certificate "

enter image description here

So, the point is Google Play Signing creates a new certificate as shown in above image.

In Facebook developer account, we need to add Key hashes generated by our keystore. But in this case, we also need to add Key hash corresponds to this APP SIGNING certificate. Now the question is, how to get key hash for this certificate/SHA-1 fingerprint?

How to create Key Hash from SHA-1 key of Google Play APP SIGNING?

To generate key hash from SHA-1 key, execute a small Java program,

// GOOGLE PLAY APP SIGNING SHA-1 KEY:- 65:5D:66:A1:C9:31:85:AB:92:C6:A2:60:87:5B:1A:DA:45:6E:97:EA
            byte[] sha1 = {
                    0x65, 0x5D, 0x66, (byte)0xA1, (byte)0xC9, 0x31, 0x85, (byte)0xAB, (byte)0x92, (byte)0xC6, (byte)0xA2, 0x60, 0x87, 0x5B, 0x1A, (byte)0xDA, 0x45, 0x6E, (byte)0x97, (byte)0xEA
            };
            System.out.println("keyhashGooglePlaySignIn:"+ Base64.encodeToString(sha1, Base64.NO_WRAP));

Output:-

keyhashGooglePlaySignIn: ZV1dkSgxvc2p4aCtFx9tcaQr8N4=

Copy this key hash and paste it to Facebook Developer account settings for your app. This is how my problem got solved.

Thanks all developers for comments. :)

vChamps
  • 963
  • 2
  • 7
  • 14
  • Hi, can you please post complete java code to convert the SHA-1 to key hash. I'm new to java facing little difficulty in executing above code. – Vasanth Aug 03 '17 at 06:20
  • It's very easy, just 2-3 lines of code. Observe the byte array I have written in my answer, just prepend 0x before each SHA1 key parts. After creating this byte array, you can print it by Base64 encoding. This line is also written in my code above. – vChamps Aug 03 '17 at 06:26
  • Cannot find symbol Base64 error is coming. I have imported java.util.Base64 – Vasanth Aug 03 '17 at 06:40
  • import android.util.Base64 – vChamps Aug 03 '17 at 06:48
  • thanks alot, It is working now. Mistake was I was trying it in eclipse it has to be tried in android studio. – Vasanth Aug 03 '17 at 07:53
  • if you are using java.util.Base64 use below method Base64.encodeBase64String(sha1) – Himeshgiri gosvami Nov 25 '17 at 10:39
  • 4
    I found a online converter https://hash.online-convert.com/sha1-generator – mehmet Jan 19 '18 at 22:43
  • 2
    If you are using the online converter mentioned by @mehmet, then download the certificate from the console and upload it to generate the key hash – rakex Sep 27 '18 at 18:49
  • In Kotlin, it would be `val sha1 = byteArrayOf(0x34.toByte(), 0xAD.toByte() ....)` – Bugs Happen May 18 '19 at 06:33
  • Thanks! a lot! about 1 weak to solve this problem, make sense because now google Release our App bundle with his own SHA-1 – finalpets Jun 24 '19 at 07:05
  • 1
    Alternatively another online key output from SHA1 http://tomeko.net/online_tools/hex_to_base64.php – Angwenyi Oct 06 '19 at 21:59
9

I improved @vChamps answer a bit. just pass the SHA1 string to below function

public void hashFromSHA1(String sha1) {
    String[] arr = sha1.split(":");
    byte[] byteArr = new  byte[arr.length];

    for (int i = 0; i< arr.length; i++) {
        byteArr[i] = Integer.decode("0x" + arr[i]).byteValue();
    }

    Log.e("hash : ", Base64.encodeToString(byteArr, Base64.NO_WRAP));
}
Farhan C K
  • 1,159
  • 18
  • 35
8
echo SHA1_here | xxd -r -p | openssl base64

Does the same work as the above code.

renish p.r
  • 105
  • 1
  • 5
5

Copy Paste the SHA1 key here to Reduce all the Headache.Link Internallly its converting Hex to Base 64.

vinay shetty
  • 895
  • 1
  • 9
  • 15
2

Kotlin code:

        import android.util.Base64
        import android.util.Log

        fun hashFromSHA1(sha1: String) {
            val arr = sha1.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
            val byteArr = ByteArray(arr.size)

            for (i in arr.indices) {
                byteArr[i] = Integer.decode("0x" + arr[i])!!.toByte()
            }

            Log.e("hash : ", Base64.encodeToString(byteArr, Base64.NO_WRAP))
        }
Amit Prajapati
  • 13,525
  • 8
  • 62
  • 84
1

To get the hash based on the Google Play signIn, just convert the SHA-1 key from Play Console to base64 and them paste into Developer Facebook android config. You can use this online converter: http://tomeko.net/online_tools/hex_to_base64.php?lang=en

Andrey Gil
  • 11
  • 1
1

Make sure to enable Facebook in Sign-in providers Firebase.

  1. enter App ID & App secret

  2. copy OAuth Redirect URIs and paste facebook console Client OAuth settings ==>Valid OAuth Redirect URIs

  3. App live mode should be on

Dr Mido
  • 2,414
  • 4
  • 32
  • 72
0

If you already have your SHA1 key you can use the online converter tool - to generate your hash code here

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 17 '22 at 18:10