26

I get "Login failed error" with the Facebook Android SDK while running on the device. I have done everything what they specified, like creating a hash and all.

The error is:

Facebook-authorize(5539): Login failed: invalid_key facebook error: com.facebook.android.FacebookError: invalid_key

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aswan
  • 5,085
  • 10
  • 46
  • 70

13 Answers13

25

Update: I wrote a more detailed blog post about this problem and explains how SSO causes it: http://sean.lyn.ch/2011/07/android-the-facebook-sdk-sso-and-you/


This question is long since answered here (and in the Facebook Android SDK), but I'm going to try and capture the full solution for anyone that ends up stumbling upon this thread.

I was developing using the Facebook Android SDK in combination with PhoneGap and the Phonegap Facebook plug in. The authentication step was working just fine until I moved from deploying on the emulator to an actual device. The failure I saw when running adb logcat was the following.

D/Facebook-authorize( 2194): Login failed: invalid_key
W/System.err( 2194): com.facebook.android.FacebookError: invalid_key

I have no idea why this worked on the emulator but failed on the device. I suspect that Facebook has a blanket policy to allow unsigned .apk applications, because they can't be distributed.

The issue is that Facebook needs information about the key used to sign the application in order to allow the authorization. What I didn't know is that the Eclipse environment is signing builds automatically when you push them to the device using a debug keystore. Details about the Debug keystore are available in the Android Documentation - Signing Applications.

In order to provide Facebook with information about the signature, you need to run the command Jay provides above (repeated here):

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

This generates a short string of characters (which may include characters such as '=' or '/') that identify the signature called a certificate. Once you have this, you need to give it to Facebook.

Find your application on Facebook's Developer page (or create a new one if you haven't set one up already). Once you're in the application summary page, choose Edit Settings and then pick Mobile and Devices on the left-hand side. Under the Android section, you'll see a box for Key Hash. Paste the certificate string from the command above into this box and hit save.

Give it a few minutes to propagate and you should be all set!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sean Lynch
  • 705
  • 8
  • 10
  • I use a Macbook pro with OSX 10.5 and i get an error Illegal option: -exportcert followed by a list of available options and some key type string. I tried using the string but im getting the Invalid key error. Please help – Kartik Aug 24 '11 at 08:27
18

Just spent a couple hours on the same problem.

When you are exporting the hash value of your key, be sure to specify the correct keystore and alias. For instance in:

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore 
| openssl sha1 -binary
| openssl base64

If you're not using the debug key to sign your APK, be sure that keytool references your export keystore and that it's using the alias that you've specified. You see the keystore and alias to be used in the "keystore selection" and "key alias selection" screens in the Eclipse export Android App wizard screen.

Also, under the "Mobile and devices" section of the app settings, I've set the application to be "Native app" and not "HTML 5 / Mobile web" since I'm working with an Android app (and an iOS one as well).

Jay Springfield
  • 207
  • 3
  • 8
  • So stupid, I used mydebug instead of androiddebugkey :( hours of fail, thanks for the post with full path. – Warpzit Aug 17 '12 at 08:09
  • here add the path with openssl.exe like "D:\android examples\facebook integration app\openssl-0.9.8k_X64\bin\openssl.exe" – harikrishnan Aug 13 '14 at 09:54
10

If the Facebook application is installed on the device, the described error will be raised.

Uninstall the existing Facebook application and run the application; it is working well. This is an SDK problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aswan
  • 5,085
  • 10
  • 46
  • 70
  • Aswan, are you saying the latest FB Android version, 1.5.1 does not work with Single Sign On? – Richard Feb 01 '11 at 21:54
  • 2
    Thanx Aswan, Same problem occured here. For the first time it is asking for login in existing app and next time it just vanished. I have also tried by removing existing FB application and its working fine. So what to do when existing app is installed? – Paresh Mayani Jun 01 '11 at 09:36
  • 2
    it's problem with `key` not with sdk. See @Jay Springfield answer. – jamapag Sep 02 '11 at 15:28
  • 2
    This answer is incorrect. It should not have the checkmark, nor should it be upvoted more than the correct answers beneath! – Yevgeny Simkin Oct 06 '11 at 03:34
  • 2
    if you uninstall the facebook app then your app will use the old WebView auth flow. that's not the correct solution – Erdal Oct 12 '11 at 04:56
  • 3
    +1... accepting your own answer doesn't make it correct. This might be a functional work-around, but it is not a solution for the root cause of the problem. – DougW Jan 25 '12 at 23:54
  • 1
    You cannot uninstall the facebook app from ICS builds anymore. – IgorGanapolsky Aug 31 '12 at 15:44
  • 1
    this isn't a real solution, more needs to be done to solve this, can an admin mark this as unresolved please? It's a misleading answer – Uriel Arvizu Feb 14 '14 at 19:55
  • this wold not give a permanent solution for the problem. Could you share something which can give a permanent solution to this problem. – Nitesh Verma Aug 12 '14 at 05:13
  • To make the app open dialog instead of preinstalled Facebook app one may use setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO) method on Session.OpenRequest instance. – vir us Feb 16 '15 at 13:04
  • I'm also facing same problem.Can anyone tell the exact steps to fix it. – Aditya Gupta Jan 27 '16 at 13:43
8

You can use this Java Android code to genereate your key:

try {
   PackageInfo info = getPackageManager().getPackageInfo("**YOURPACKAGENAME**", PackageManager.GET_SIGNATURES);
   for (Signature signature : info.signatures) {
        MessageDigest md = MessageDigest.getInstance("SHA");
        md.update(signature.toByteArray());
        Log.i("PXR", Base64.encodeBytes(md.digest()));
   }
}
catch (NameNotFoundException e) {}
catch (NoSuchAlgorithmException e) {}
marzapower
  • 5,531
  • 7
  • 38
  • 76
metch
  • 673
  • 7
  • 14
  • However from where to call this method to provide the hash key, as Facebook is replacing +,- and / by _ when it is returning the same key. – jeevs Jan 05 '14 at 11:45
7

Another trap for new players: if you get the keystore password wrong in

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

it will silently give the wrong result (the digest for the password wrong message, I suspect).

Working through intermediate files avoids this. Using a Linux desktop might, as well.

Rich
  • 885
  • 12
  • 15
  • 3
    that was my problem - the default debug keystore password is `android` –  Jun 16 '11 at 15:45
  • Oh my god this is the solution for me. I've tried everything but all the while I was entering a wrong password. I didn't even know that I was entering the wrong password! Thank you user765140 – Kevin Jul 16 '19 at 12:19
3

I fixed the bug with this:

If you add Facebook.FORCE_DIALOG_AUTH to the authorize line:

mFacebook.authorize(
    MundialRugby2011Activity.this,
    new String[] {"publish_stream", "read_stream", "offline_access"},
    Facebook.FORCE_DIALOG_AUTH,
    new LoginDialogListener()
);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

I had a similar problem (invalid_key) and for me the solution was to install Cygwin (I am using Windows 7 64-bit) and regenerate the key from there. I got a totally different key (than on PowerShell) and now my application does login just fine.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Harri
  • 828
  • 10
  • 13
1

I may just have solved the wrong keyhash issue in Windows 7. See my report in Invalid key issue for an Android application to call the Facebook API.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RRTW
  • 3,160
  • 1
  • 35
  • 54
0

I am using Phonegap and the FB SDK -> didn't get a correct key generated, maybe I have to use the hash key of the build service? Nevertheless, I just put in the Hash the cordova library say that is didn't match with the one at facebook.

Jens Peters
  • 2,075
  • 1
  • 22
  • 30
0

I wasted about four hours solving such a problem (Windows 7, Eclipse). The keytool utility is really sly. I already had Eclipse installed.

  1. Install Cygwin.
  2. Download OpenSSL for Windows. Put it in any folder and add path to "lib" & "bin" subfolders in Windows PATH variable.
  3. Now you should be able to open the Cygwin Bash shell (from the start menu) and successfully run OpenSSL from it.

Some keytool tricks:

  1. Do NOT USE the Windows shell (CMD) - always use Cygwin. Running keytool from CMD just silent produce the wrong hash!

  2. Remember, that the right Unix path separator you should use is "/", not "\"!

  3. If the keytool can not find the keystore file, it just silently generates the WRONG key! If you set the right path to the file, it asks your "Enter keystore password:". So, if it is not ask you about it, be sure you pass wrong path (see also #2).
  4. If you type the right keystore password, the hash is the same as if you do not pass a keystore password at all. If you type the wrong keystore password it silently generates the wrong hash.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mike Keskinov
  • 11,614
  • 6
  • 59
  • 87
0

I have used Cygwin on Windows and iOS Bash, but both gave me the wrong keys! Finally I have found the solution in Implementing Facebook into your application invalid key with keytool.

ProgrammerXR have written a really useful method that extracts the key hash straight from the signed application installed on the device - brilliant!

edited:

The above link doesn't work anymore but you can get some more details in the Facebook docs (see the 'Troubleshooting' paragraph in the 'Run the Samples' section) https://developers.facebook.com/docs/android/getting-started/#samples

Marqs
  • 17,800
  • 4
  • 30
  • 40
0

Thanks to Facebook, now it's giving a key itself along with invalid_key exception. Use that value and update in application settings. I am using Windows 7 64-bit machine and for me the key doesn't have = (equal to) in the end but it worked cleanly.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pawan Maheshwari
  • 15,088
  • 1
  • 48
  • 50
0

This problem seems to be triggered when the Facebook app is installed thus the SDK is trying to use the app for authentication. And that part seems to fail always in my case.

My current workaround to solve this is to make sure that the web login dialog gets triggered instead. The way to make that occur is to tamper with the FB_APP_SIGNATURE in Facebook.java (Line 763 latest SDK from GitHub), in my case I just replaced the last part "928a2" with an empty string.

By doing this it seems that the SDK falls back on the web dialog and everything works.

Please note that this isn't a perfect workaround, but it do solve the issue with the Facebook SDK and the Facebook App being incompatible for the moment on Android.

grenangen
  • 168
  • 2
  • 10