带有无效密钥哈希的 Android Facebook 集成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23674131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:20:54  来源:igfitidea点击:

Android Facebook integration with invalid key hash

androidfacebook

提问by Android

In one of my apps I need to get data from Facebook... I am doing this:

在我的一个应用程序中,我需要从 Facebook 获取数据......我这样做:

I have created app ID. It logs in successfully, but after logging out, I log in, and then it gives me:

我已经创建了app ID。它登录成功,但注销后,我登录,然后它给了我:

Screenshot of invalid key hash error Facebook

无效密钥哈希错误 Facebook 的屏幕截图

What is wrong I am doing? I am using the Facebook SDK... I have installed Facebook on my phone... It is running well within an emulator, but that does not have the inbuilt Facebook application installed.

我在做什么错?我正在使用Facebook SDK... 我已经在我的手机上安装了 Facebook ... 它在模拟器中运行良好,但没有安装内置的 Facebook 应用程序。

This is my code:

这是我的代码:

if (FB_APP_ID == null) {
    Builder alertBuilder = new Builder(this);
    alertBuilder.setTitle("Warning");
    alertBuilder.setMessage("A Facebook Applicaton ID must be " +
                            "specified before running this example: see App.java");
    alertBuilder.create().show();
}

// Initialize the dispatcher
Dispatcher dispatcher = new Dispatcher(this);
dispatcher.addHandler("login", LoginHandler.class);
dispatcher.addHandler("stream", StreamHandler.class);
dispatcher.addHandler("logout", LogoutHandler.class);

// If a session already exists, render the stream page
// immediately. Otherwise, render the login page.
Session session = Session.restore(this);
if (session != null) {
    dispatcher.runHandler("stream");
}
else {
    dispatcher.runHandler("login");
}

回答by Mahendran Sakkarai

The generated hash key is wrong. You may get the hash key using two steps.

生成的哈希键错误。您可以通过两个步骤获取哈希键。

One is through a command prompt. Another one is through coding. The hash key through a command prompt is working on the first time only. I don't know the reason. I have also got the same problem. So I tried it through programmatically.

一种是通过命令提示符。另一种是通过编码。通过命令提示符的哈希键仅在第一次使用。我不知道原因。我也遇到了同样的问题。所以我以编程方式尝试了它。

Follow these steps:

按着这些次序:

Paste the following code in oncreate().

将以下代码粘贴到oncreate().

try {
    PackageInfo info = getPackageManager().getPackageInfo(
                           "com.example.packagename",
                           PackageManager.GET_SIGNATURES);
    for (Signature signature : info.signatures) {
        MessageDigest md = MessageDigest.getInstance("SHA");
        md.update(signature.toByteArray());
        Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
    }
}
catch (NameNotFoundException e) {
}
catch (NoSuchAlgorithmException e) {
}

Modify "com.example.packagename" with your package name in the above coding without fail (you may find your package name in the Android manifest file).

在上面的代码中用你的包名修改“com.example.packagename”,不会失败(你可以在Android清单文件中找到你的包名)。

Run your application. Go to the activity where you pasted the above code. In the LogCat file, search for "KeyHash". You may find a key hash. Copy the key hash and go to Facebook application dashboard page. Go to settings and enter the details like in the below image.

运行您的应用程序。转到粘贴上述代码的活动。在 LogCat 文件中,搜索“KeyHash”。您可能会找到一个密钥散列。复制密钥哈希并转到 Facebook 应用程序仪表板页面。转到设置并输入如下图所示的详细信息。

Enter image description here

在此处输入图片说明

Once you finished the above step, relaunch the app again. You may now log into Facebook. For more details about key hash, check the link.

完成上述步骤后,再次重新启动应用程序。您现在可以登录 Facebook。有关密钥散列的更多详细信息,请查看链接

If you add wrong information in the settings page, it means it will give some error. So use the correct information there. And also if the public (other than you) need to use your application means you need to enable the permission (change "yes" in the "Status & Review" next to the setting).

如果您在设置页面中添加了错误的信息,则意味着会出现一些错误。所以在那里使用正确的信息。而且,如果公众(除您之外)需要使用您的应用程序意味着您需要启用该权限(在设置旁边的“状态和”中更改“是”)

回答by Rafal Malek

If you are using Google Play App signing:

如果您使用的是 Google Play 应用签名:

Open the App signingsection in Google Play Console, and get the SHA-1 hashunder App signing certificate. Then convert it to Base64, for example with this tool: Hexadecimal -> base64 string decoder

在 Google Play Console 中打开App 签名部分,并获取App 签名证书下的SHA-1 哈希值。然后将其转换为Base64,例如使用此工具:十六进制 -> base64 字符串解码器

Console screenshot

控制台截图

Convert to Base64 screenshot

转换为 Base64 屏幕截图

回答by Arpit Patel

If you are facing this problem then put this key into your developer.facebook.com:

如果您遇到此问题,请将此密钥放入您的developer.facebook.com

Enter image description here

在此处输入图片说明

Then make sure your app is liveon developer.facebook.com.

然后确保你的应用程序是developer.facebook.com

This green circle is indicating the app is live:

这个绿色圆圈表示该应用程序已上线:

Enter image description here

在此处输入图片说明

If it is not then follow these two steps for make your app live:

如果不是,请按照以下两个步骤使您的应用程序上线:

Step 1Go to your application→settingadd Contact Emailand apply Save Changes.

步骤 1转到您的应用程序 →设置添加联系人电子邮件并应用保存更改。

Step 2Go to the App Reviewoption and make sure this toggle is Yes. I added a screenshot:

步骤 2转到App Review选项并确保此切换为Yes。我添加了一个截图:

Enter image description here

在此处输入图片说明

Note:If you want to copy the hashkey, check the BlueServiceQueue in LogCat.

注意:如果要复制哈希键,请检查LogCat 中的 BlueServiceQueue 。

回答by Akash Bisariya

I got the same problem. I was sure that it was due to very small fault and yes it was!

我遇到了同样的问题。我确信这是由于很小的故障造成的,是的!

I found the solution:

我找到了解决方案:

When generating the debughash key in my computer, I entered the password of my system. But the password should be the following -

在我的电脑中生成调试哈希键时,我输入了我的系统密码。但密码应该如下——

Enter keystore password: "android". This was the only problem in my case.

输入密钥库密码:“android”。这是我唯一的问题。

----- For generating Debugkey hash, use this command -

----- 要生成调试密钥哈希,请使用此命令 -

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

Enter keystore password: 'android'

输入密钥库密码:'android'

----- To Generate Releasekey hash, use this command -

----- 要生成发布密钥哈希,请使用此命令 -

keytool -exportcert -alias "alias of keystore" -keystore "Your path to the keystore when signing app" | openssl sha1 -binary | openssl base64

Provide your keystore password after executing this command.

执行此命令后提供您的密钥库密码。

回答by Martin Doychev

I experienced the same problem. I made a short research on the possible reasons for this strange behavior and I found the following:

我遇到了同样的问题。我对这种奇怪行为的可能原因进行了简短的研究,我发现了以下几点:

  • During the first execution of a new Facebook app, it will allow connection/login even if you don't specify any key hashes.

  • For me, the tutorial which Facebook provided didn't generate the correct key hash, because it was giving the wrong configuration. When executing:

    keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | openssl sha1 -binary | openssl
    base64
    
  • 在新 Facebook 应用程序的第一次执行期间,即使您没有指定任何密钥哈希,它也将允许连接/登录

  • 对我来说,Facebook 提供的教程没有生成正确的密钥哈希,因为它给出了错误的配置。执行时:

    keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | openssl sha1 -binary | openssl
    base64
    

make sure you check all properties - the HOMEPATH, the existence of the keystore, etc. Maybe you also have to provide password.

确保检查所有属性 - HOMEPATH,密钥库的存在等。也许您还必须提供密码。

  • What generated the proper configuration was the solution suggested by @Mahendran.

  • Also, if you see the error originally posted (http://i.stack.imgur.com/58q3v.png), most probably the key hash you see on the screen is your real one.If nothing else works, try inputting it in Facebook.

  • 生成正确配置的是@Mahendran 建议解决方案

  • 此外,如果您看到最初发布的错误 ( http://i.stack.imgur.com/58q3v.png),则您在屏幕上看到密钥哈希很可能就是您的真实密钥。如果没有其他效果,请尝试在 Facebook 中输入它。

I got all those results with: Windows 7 64-bit edition, Android Studio 1.2.2, JDK 7.

我得到了所有这些结果:Windows 7 64 位版本、Android Studio 1.2.2、JDK 7。

回答by JP Ventura

According Facebook Login for Android, you must provide the key hash value. In order to obtain it, you will need the keyused to sign your application.

根据Facebook Login for Android,您必须提供密钥哈希值。为了获得它,您将需要用于签署您的应用程序的密钥

keytool \
    -exportcert \
    -alias YourKeyAlias \
    -storepass YourStoreKeyPassword \
    -keystore PathToYourKeyStoreFile | openssl sha1 -binary | openssl base64

回答by MBH

You must create two key hashes, one for Debug and one for Release.

您必须创建两个密钥散列,一个用于调试,一个用于发布。

For the Debugkey hash:

对于调试键哈希:

On OS X, run:

在 OS X 上,运行:

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

On Windows, run:

在 Windows 上,运行:

keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | openssl sha1 -binary | openssl
base64

Debug key hashes source

调试密钥哈希源

For the Releasekey hash:

对于Release密钥哈希:

On OS X, run (replace what is between <>with your values):

在 OS X 上,运行<>用您的值替换之间的内容)

keytool -exportcert -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEY_PATH> | openssl sha1 -binary | openssl base64

On Windows, use (replace what is between <>with your values):

在 Windows 上,使用<>用您的值替换之间的内容)

keytool -exportcert -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEY_PATH> | openssl sha1 -binary | openssl base64

Release key hashes source

发布密钥哈希源

回答by Matan Dahan

I tried all of the previous answers and nothing helped my case with my clients!

我尝试了所有以前的答案,但对我的客户没有任何帮助!

Then my client remembered he had the Facebook App installed on his device. After he removed it. the login worked perfectly.

然后我的客户记得他在他的设备上安装了 Facebook 应用程序。在他取下之后。登录工作完美。

The hashkey had been changed, and I've replaced the old hash keys at Facebook Developers Console with the key from the error (as suggested above), and it works!

哈希键已更改,我已将 Facebook Developers Console 中的旧哈希键替换为错误中的键(如上文所述),并且有效!

The Facebook App itself might be the issue, so you better figure this out on a device with the Facebook app installed and on a device with the Facebook app not installed and handle both cases...

Facebook 应用程序本身可能是问题所在,因此您最好在安装了 Facebook 应用程序的设备和未安装 Facebook 应用程序的设备上弄清楚这一点,并处理这两种情况......

回答by Sajid Zeb

This is how I solved this problem:

我是这样解决这个问题的:

First you have to get the SHA-1 value. For that there are two ways.

首先,您必须获得 SHA-1 值。为此,有两种方法。

To get the SHA-1 value in Android Studio.

在 Android Studio 中获取 SHA-1 值。

  1. Click Gradle
  2. Click Signing Report
  3. Copy the SHA-1 value
  1. 单击摇篮
  2. 点击签名报告
  3. 复制 SHA-1 值

OR

或者

To get the SHA-1 value from the keystorefile.

密钥库文件中获取 SHA-1 值。

keytool -list -v -keystore keystore_file_name.jks -alias key0

Copy the SHA-1value to your clipboard like this:

像这样将SHA-1值复制到剪贴板:

CD:A1:EA:A3:5C:5C:68:FB:FA:0A:6B:E5:5A:72:64:DD:26:8D:44:84

And open Hexadecimal -> Base64 string decoderto convert your SHA-1 value to Base64.

并打开十六进制 -> Base64 字符串解码器将您的 SHA-1 值转换为 Base64。

This is what Facebook requires.

这正是 Facebook 所需要的。

Get the generated hash " ********************= " and copy the key hash to the Facebook app.

获取生成的哈希“ ********************= ”并将密钥哈希复制到 Facebook 应用程序。

回答by Jefferson Suzuki Bitencourt

I was having the same problem. First log in, just fine, but then, an invalid key hash.

我遇到了同样的问题。首先登录,就好了,但是,一个无效的密钥散列。

The Facebook SDK for Unity gets the wrong key hash. It gets the key from "C:\Users\"your user".android\debug.keystore" and, in a perfect world, it should get it from the keystore you created in your project. That's why it is telling you that the key hash is not registered.

Facebook SDK for Unity 获取错误的密钥哈希。它从 "C:\Users\"your user".android\debug.keystore" 获取密钥,在理想情况下,它应该从你在项目中创建的密钥库中获取它。这就是为什么它告诉您密钥哈希未注册的原因。

As suggested by Madi, you can follow the steps on this linkto find the right key. Just make sure to point them to the keystore inside your project. Otherwise you won't get the right key.

根据Madi 的建议,您可以按照此链接上的步骤找到正确的密钥。只需确保将它们指向项目内的密钥库。否则,您将无法获得正确的密钥。