Android 如何创建 Facebook 密钥哈希?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10042022/
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 02:17:38  来源:igfitidea点击:

How to create a Facebook key hash?

androidfacebookfacebook-android-sdk

提问by Mel

In the Facebook android tutorial we are told to use following code to create a key hash:

在 Facebook android 教程中,我们被告知使用以下代码来创建密钥哈希:

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

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

Is this the exact code to use in all situations? For example instead of ~/.android/debug.keystoreshould it something like C:/folderone/foldertwo/.android/debug.keystore?

这是在所有情况下使用的确切代码吗?例如,而不是 ~/.android/debug.keystore应该像C:/folderone/foldertwo/.android/debug.keystore?

As you can see I'm unsure of whether inverted commas are required or not, whether full paths are required or not!

如您所见,我不确定是否需要引号,是否需要完整路径!

Is anyone able to provide a real world example?

有没有人能够提供一个真实世界的例子?

See
https://developers.facebook.com/docs/mobile/android/build/#sso

请参阅
https://developers.facebook.com/docs/mobile/android/build/#sso

回答by cesards

try

尝试

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

} catch (NoSuchAlgorithmException e) {

}

in your main Activity :-) This is the only solution it works for me for Android SDK 3.0

在您的主要活动中 :-) 这是适用于我的 Android SDK 3.0 的唯一解决方案

回答by kodeshpa

You can create this way

你可以这样创建

keytool -exportcert -alias androiddebugkey -keystore c:\Users\<your windows default user>\.android\debug.keystore | openssl sha1 -binary | openssl base64

Enter keystore password: android

输入密钥库密码:android

回答by nightfixed

 /**
     * Generates the hash key used for Facebook console to register app. It can also be used for other sdks) Method copied from: https://developers.facebook.com/docs/android/getting-started/
     */
    public static String printHashKey(Context ctx) {
        // Add code to print out the key hash
        try {
            PackageInfo info = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                return Base64.encodeToString(md.digest(), Base64.DEFAULT);
            }
        } catch (NameNotFoundException e) {
            return "SHA-1 generation: the key count not be generated: NameNotFoundException thrown";
        } catch (NoSuchAlgorithmException e) {
            return "SHA-1 generation: the key count not be generated: NoSuchAlgorithmException thrown";
        }

        return "SHA-1 generation: epic failed";
    }

回答by sleepcry

In eclipse, window-> preferences-> Android-> build-> default debug keystore, copy the path to replace the ~/.android/debug.keystore

在eclipse中,window-> preferences-> Android-> build-> defaultdebugkeystore,复制路径替换~/.android/debug.keystore

回答by android developer

When having the error in the log, when trying to login to Facebook, look for something that looks like:

当日志中出现错误时,在尝试登录 Facebook 时,查找如下所示的内容:

Invalid key hash. The key hash *** does not match any stored key hashes. Configure your app key hashes at http://developers.facebook.com/apps/565561836797777

where "***" is the key that you need to use.

其中“***”是您需要使用的密钥。

回答by Thanos Karpouzis

I had the same problem, I spend a couple of hours to find a solution, but actually the Facebook SDK provides the solution by itself.

我遇到了同样的问题,我花了几个小时来寻找解决方案,但实际上 Facebook SDK 自己提供了解决方案。

in the DialogListener class I modified the onFacebookError method:

在 DialogListener 类中,我修改了 onFacebookError 方法:

@Override 
public void onFacebookError(FacebookError error) {
   Log.d("myTag",error.getmessage); 
 }

Execute the app (which was sign with the same key i use for the market), and on LogCat will be a message under this tag with the correct key.

执行应用程序(使用我在市场上使用的相同密钥进行签名),LogCat 上将在此标签下显示一条带有正确密钥的消息。

We had also created a simple project which does all the work, and returns the correct key on an alert-box and on LogCat. You can find it on our blog.

我们还创建了一个简单的项目来完成所有工作,并在警报框和 LogCat 上返回正确的密钥。您可以在我们的博客上找到它。

回答by Shygar

One brute force option is to just go ahead and try to share something from your app. My app then displays a Facebook page with the key it is trying to match. Then you can just copy this key and put it in your Facebook 'Settings' page on your developer Facebook account.

一种蛮力选择是继续尝试从您的应用程序中分享一些内容。然后,我的应用程序会显示一个 Facebook 页面,其中包含它试图匹配的密钥。然后,您只需复制此密钥并将其放入开发者 Facebook 帐户的 Facebook“设置”页面中即可。

Not ideal, but in a pinch it may be helpful.

不理想,但在紧要关头它可能会有所帮助。

回答by anddevmanu

keytool -exportcert -alias androiddebugkey -keystore "debug.keystore path" | openssl sha1 -binary | openssl base64

keytool -exportcert -alias androiddebugkey -keystore "debug.keystore 路径" | openssl sha1 -binary | openssl base64

if you haven't setup environment variables for open ssl and java sdk than put jdk's bin folder path in place of keytool and your openssl path in place of openssl and not to forget to put double quotes for your path

如果您还没有为 open ssl 和 java sdk 设置环境变量,那么将 jdk 的 bin 文件夹路径代替 keytool 并将您的 openssl 路径代替 openssl 并且不要忘记为您的路径加上双引号

ex-"C:\Program Files\Java\jdk1.5.0_11\bin" -exportcert -alias androiddebugkey -keystore "C:\Users\amin.android\debug.keystore" | "F:\openssl\binsha1\openssl.exe" -binary | "F:\openssl\binsha1\openssl.exe" base64

ex-"C:\Program Files\Java\jdk1.5.0_11\bin" -exportcert -alias androiddebugkey -keystore "C:\Users\amin.android\debug.keystore" | "F:\openssl\binsha1\openssl.exe" -binary | "F:\openssl\binsha1\openssl.exe" base64