简体   繁体   English

使用Facebook SDK登录失败无效密钥错误

[英]Login failed invalid key error with Facebook SDK

I get "Login failed error" with the Facebook Android SDK while running on the device. 在设备上运行时,我使用Facebook Android SDK获得“登录失败错误”。 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 Facebook授权(5539):登录失败:invalid_key facebook错误:com.facebook.android.FacebookError:invalid_key

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/ 更新:我写了一篇关于这个问题的更详细的博客文章,并解释了SSO如何导致它: 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. 这个问题很久以来一直在这里得到解答(以及在Facebook Android SDK中 ),但我将尝试为最终绊倒这个线程的任何人捕获完整的解决方案。

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. 我正在开发使用Facebook Android SDK与PhoneGap和Phonegap Facebook插件结合使用。身份验证步骤工作正常,直到我从模拟器上部署到实际设备。 The failure I saw when running adb logcat was the following. 我在运行adb logcat时看到的失败如下。

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. 我怀疑Facebook有一个全面的政策允许未签名的.apk应用程序,因为它们无法分发。

The issue is that Facebook needs information about the key used to sign the application in order to allow the authorization. 问题是Facebook需要有关用于签署应用程序的密钥的信息才能允许授权。 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. 我不知道的是,当您使用调试密钥库将它们推送到设备时,Eclipse环境会自动对构建进行签名。 Details about the Debug keystore are available in the Android Documentation - Signing Applications . 有关Debug密钥库的详细信息,请参阅Android文档 - 签名应用程序

In order to provide Facebook with information about the signature, you need to run the command Jay provides above (repeated here): 为了向Facebook提供有关签名的信息,您需要运行Jay提供的命令(在此处重复):

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. 一旦你有了这个,你需要把它交给Facebook。

Find your application on Facebook's Developer page (or create a new one if you haven't set one up already). Facebook的开发者页面上找到您的应用程序(如果您尚未设置一个,则创建一个新应用程序)。 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. 在Android部分下,您会看到一个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! 给它几分钟的宣传,你应该全力以赴!

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. 如果您没有使用调试密钥对APK进行签名,请确保keytool引用您的导出密钥库,并且它使用您指定的别名。 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. 您可以在Eclipse导出Android应用程序向导屏幕中的“密钥库选择”和“密钥别名选择”屏幕中看到要使用的密钥库和别名。

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). 此外,在应用程序设置的“移动和设备”部分下,我将应用程序设置为“本机应用程序”,而不是“HTML 5 /移动网络”,因为我正在使用Android应用程序(和iOS应用程序)以及)。

If the Facebook application is installed on the device, the described error will be raised. 如果Facebook应用程序安装在设备上,则会引发所描述的错误。

Uninstall the existing Facebook application and run the application; 卸载现有的Facebook应用程序并运行该应用程序; it is working well. 它运作良好。 This is an SDK problem. 这是一个SDK问题。

You can use this Java Android code to genereate your key: 您可以使用此Java Android代码生成密钥:

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) {}

Another trap for new players: if you get the keystore password wrong in 新玩家的另一个陷阱:如果你的密钥库密码错误了

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | keytool -exportcert -alias androiddebugkey -keystore~ / .android / debug.keystore | openssl sha1 -binary | openssl sha1 -binary | openssl base64 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. 也可以使用Linux桌面。

I fixed the bug with this: 我修复了这个bug:

If you add Facebook.FORCE_DIALOG_AUTH to the authorize line: 如果您将Facebook.FORCE_DIALOG_AUTH添加到授权行:

mFacebook.authorize(
    MundialRugby2011Activity.this,
    new String[] {"publish_stream", "read_stream", "offline_access"},
    Facebook.FORCE_DIALOG_AUTH,
    new LoginDialogListener()
);

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. 我有一个类似的问题(invalid_key),对我来说解决方案是安装Cygwin(我使用的是Windows 7 64位)并从那里重新生成密钥。 I got a totally different key (than on PowerShell) and now my application does login just fine. 我得到了一个完全不同的密钥(比在PowerShell上),现在我的应用程序确实登录了。

我可能刚刚在Windows 7中解决了错误的keyhash问题。请参阅我的报告,在Android应用程序的无效密钥问题中调用Facebook API

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? 我正在使用Phonegap和FB SDK - >没有生成正确的密钥,也许我必须使用构建服务的哈希密钥? Nevertheless, I just put in the Hash the cordova library say that is didn't match with the one at facebook. 尽管如此,我只是把哈希的cordova图书馆说成与facebook上的那个不匹配。

I wasted about four hours solving such a problem (Windows 7, Eclipse). 我浪费了大约四个小时来解决这个问题(Windows 7,Eclipse)。 The keytool utility is really sly. keytool实用程序非常狡猾。 I already had Eclipse installed. 我已经安装了Eclipse。

  1. Install Cygwin. 安装Cygwin。
  2. Download OpenSSL for Windows. 下载OpenSSL for Windows。 Put it in any folder and add path to "lib" & "bin" subfolders in Windows PATH variable. 将它放在任何文件夹中,并在Windows PATH变量中添加“lib”和“bin”子文件夹的路径。
  3. Now you should be able to open the Cygwin Bash shell (from the start menu) and successfully run OpenSSL from it. 现在您应该能够打开Cygwin Bash shell(从开始菜单)并成功运行OpenSSL。

Some keytool tricks: 一些keytool技巧:

  1. Do NOT USE the Windows shell (CMD) - always use Cygwin. 不要使用Windows shell(CMD) - 始终使用Cygwin。 Running keytool from CMD just silent produce the wrong hash! 从CMD运行keytool只是沉默产生错误的哈希!

  2. Remember, that the right Unix path separator you should use is "/", not "\\"! 请记住,您应该使用的正确的Unix路径分隔符是“/”,而不是“\\”!

  3. If the keytool can not find the keystore file, it just silently generates the WRONG key! 如果keytool找不到密钥库文件,它只是默默生成错误密钥! 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). 所以,如果没有问你这个问题,请确保你走错路(另见#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. 如果键入错误的密钥库密码,则会以静默方式生成错误的哈希。

I have used Cygwin on Windows and iOS Bash, but both gave me the wrong keys! 我在Windows和iOS Bash上使用过Cygwin,但两个都给了我错误的键! Finally I have found the solution in Implementing Facebook into your application invalid key with keytool . 最后,我找到了使用keytool将Facebook实现为应用程序无效密钥的解决方案。

ProgrammerXR have written a really useful method that extracts the key hash straight from the signed application installed on the device - brilliant! ProgrammerXR编写了一个非常有用的方法,直接从设备上安装的已签名应用程序中提取密钥哈希 - 太棒了!

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 以上链接不再起作用,但您可以在Facebook文档中获得更多详细信息(请参阅“运行示例”部分中的“疑难解答”段落) https://developers.facebook.com/docs/android/getting -started /#样品

Thanks to Facebook, now it's giving a key itself along with invalid_key exception. 感谢Facebook,现在它正在给出一个密钥以及invalid_key异常。 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. 我正在使用Windows 7 64位机器,对我来说,密钥最终没有= (等于),但它干净利落。

This problem seems to be triggered when the Facebook app is installed thus the SDK is trying to use the app for authentication. 安装Facebook应用程序时似乎会触发此问题,因此SDK正在尝试使用该应用程序进行身份验证。 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. 我目前解决此问题的解决方法是确保触发Web登录对话框。 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. 实现这一目标的方法是篡改Facebook.java中的FB_APP_SIGNATURE(来自GitHub的763行最新SDK),在我的情况下,我刚用空字符串替换了最后一部分“928a2”。

By doing this it seems that the SDK falls back on the web dialog and everything works. 通过这样做,似乎SDK回退到Web对话框,一切正常。

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. 请注意,这不是一个完美的解决方法,但它确实解决了Facebook SDK和Facebook应用程序目前在Android上不兼容的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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