Key hash for Android-Facebook app

匿名 (未验证) 提交于 2019-12-03 01:54:01

问题:

I'm working on an Android app, in which I want to integrate a Facebook posting feature. I downloaded the Facebook-Android SDK, and I got the readme.md (text file) in there, in which it is mentioned to generate the key hash for Android. How do I generate it?

回答1:

Here are the steps-

  1. Download openssl from Google code (If you have a 64 bit machine you must download openssl-0.9.8e X64 not the latest version)

  2. Extract it. create a folder- OpenSSL in C:/ and copy the extracted code here.

  3. detect debug.keystore file path. If u didn't find, then do a search in C:/ and use the Path in the command in next step.

  4. detect your keytool.exe path and go to that dir/ in command prompt and run this command in 1 line-

    $ keytool -exportcert -alias androiddebugkey -keystore "C:\Documents and Settings\Administrator.android\debug.keystore" | "C:\OpenSSL\bin\openssl" sha1 -binary |"C:\OpenSSL\bin\openssl" base64

    • it will ask for password, put android
    • that's all. u will get a key-hash

For more info visit here



回答2:

You can use this code in any activity. It will log the hashkey in the logcat, which is the debug key. This is easy, and it's a relief than using SSL.

PackageInfo info; try {     info = getPackageManager().getPackageInfo("com.you.name", PackageManager.GET_SIGNATURES);     for (Signature signature : info.signatures) {         MessageDigest md;         md = MessageDigest.getInstance("SHA");         md.update(signature.toByteArray());         String something = new String(Base64.encode(md.digest(), 0));         //String something = new String(Base64.encodeBytes(md.digest()));         Log.e("hash key", something);     } } catch (NameNotFoundException e1) {     Log.e("name not found", e1.toString()); } catch (NoSuchAlgorithmException e) {     Log.e("no such an algorithm", e.toString()); } catch (Exception e) {     Log.e("exception", e.toString()); } 

You can delete the code after knowing the key ;)



回答3:

I've created a small tool for Windows and Mac OS X. Just throw in the key-store file, and get the hash key.

If you want the default debug.keystore file, use the default alias and password. Else, use your own keystore file and values.

Check it out, download the Windows version or download the Mac OS X version (Dev-Host might be down sometimes... so if the link is broken, PM me and I'll fix it).

I hope that help you guys...

Dec 31, 2014 - EDIT: Changed host to AFH. Please let me know if the links are broken

Nov 21, 2013 - EDIT:

As users requested, I added a default keystore location and a DONATE button. Feel free to use it if I've helped you. :)



回答4:

The instructions currently in Facebook's Android Tutorial do not work well under Windows. Their example shows how to pipe the keytool output to openssl but if you try this under Windows the output is not valid for some reason. I found that I had to use intermediary files to get it to work properly. Here are the steps that worked for me:

Start by downloading openssl for Windows from Google.

C:\Users\Me>keytool -exportcert -alias my_key -keystore my.keystore -storepass PASSWORD > mycert.bin  C:\Users\Me>openssl sha1 -binary mycert.bin > sha1.bin  C:\Users\Me>openssl base64 -in sha1.bin -out base64.txt 

After running these commands the valid hash is stored in the file base64.txt. Copy and paste this to your app settings on Facebook.



回答5:

This is what is given at the official page of Facebook:

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

Let me break this command into fragments.

  1. Look for "keytool.exe". You can search that on the C: drive. You can find it in "java jdk" or "java jre". If you have installed multiple versions, choose any.

  2. Open a CMD prompt and go to the above directory where you found "keytool.exe".

    Clip the "exe`" and paste the above command provided on the Facebook page.

  3. You will get an error on entering this that OpenSSL is not recognized as in input output command. Solution : Download "Openssl" from OpenSSL (if you have a 64-bit machine you must download openssl-0.9.8e X64). Extract and save it anywhere... I saved it on the C: drive in the OpenSSl folder

  4. Replace the openssl in the above command in which you was getting an error of OpenSSL with "C:\OpenSSL\bin\openssl" at both the places after the pipe, "|".

  5. If prompted for a password, enter android.

And you will get your hash key. For further steps, refer again to the Facebook page.



回答6:

Add this code to onCreate of your activity, it will print the hash under the KeyHash tag in your logCat

try {     PackageInfo info = getPackageManager().getPackageInfo(                            getPackageName(),                            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) {  } 

You can add multiple hashkeys for your account, so if you been running in debug don't forget to run this again in release mode.



回答7:

To get the Android key hash code, follow these steps:

  1. Download OpenSSL for Windows here
  2. Now unzip to the C drive
  3. Open a CMD prompt
  4. Type cd C:\Program Files\Java\jdk1.6.0_26\bin
  5. Then type only keytool -export -alias myAlias -keystore C:\Users\your user name\.android\myKeyStore | C:\openssl-0.9.8k_WIN32\bin\openssl sha1 -binary | C:\openssl-0.9.8k_WIN32\bin\openssl enc -a -e
  6. Done


回答8:

The simplest solution I have found is this:

  • Open up Log Cat
  • Try and access Facebook with the Android SDK
  • Look for the line in the log that looks like this:

    04-24 01:14:08.605: I/System.out(31395): invalid_key:Android key mismatch.  Your key "abcdefgHIJKLMN+OPqrstuvwzyz" does not match the allowed keys specified in your application settings. Check your application settings at  http://www.facebook.com/developers 
  • Copy "abcdefgHIJKLMN+OPqrstuvwzyz" and paste it into the Facebook Android Key Hash area.



回答9:

I have done by this way for Linux OS & Windows OS:

Linux:

  • Download Openssl
  • Open terminal
  • keytool -exportcert -alias **myaliasname** -keystore **/home/comp-1/Desktop/mykeystore.jks** | openssl sha1 -binary | openssl base64

Kindly change Alias Name and Keystore with it's path as your requirement.

Terminal would ask for Password of Keystore. You have to provide password for the same Keystore.

So finally you would get the Release Hashkey.

Windows:

Steps for Release Hashkey:

  • Download Openssl (Download from here), I have downloaded for 64 bit OS, you can find more here
  • Extract downloaded zip file to C:\ drive only
  • Open command prompt
  • keytool -exportcert -alias **myaliasname** -keystore **"C:\Users\hiren.patel\Desktop\mykeystore.jks"** | "C:\openssl-0.9.8e_X64\bin\openssl.exe" sha1 -binary | "C:\openssl-0.9.8e_X64\bin\openssl.exe" base64

Kindly change Alias Name and Keystore with it's path as your requirement.

Note:

Please put your details where I have marked between ** **.

Terminal would ask for Password of Keystore. You have to provide password for the same Keystore.

So finally you would get the Release Hashkey.

Done



回答10:

  • download openSSL for windows in here you can find 64bit and 32bit here

  • extract the downloaded file

  • create folder name openSSL in C drive
  • copy all the extracted items in to openSSL folder (bin,include,lib,openssl.cnf)
  • get android debug keystore, default location will be

C:\Users\username\.android\debug.keystore

  • now get your command prompt and paste this code

keytool -exportcert -alias androiddebugkey -keystore C:\Users\username.android\debug.keystore | "C:\openSSL\bin\openssl" sha1 -binary | "C:\openSSL\bin\openssl" base64

  • hit enter and you will get the 28 digit keycode


回答11:

Download openSSL -> Install it -> it would usually install in C:\OpenSSL

then open cmd and type

cd../../Program Files (Enter)  java (Enter)  dir (Enter)  cd jdk1.6.0_17 (varies with jdk versions) (Enter) 

to check jdk version go to C:/program files/java/jdk_version

cd bin (enter)  keytool -exportcert -alias androiddebugkey -keystore C:Users\Shalini\.android\debug.keystore | "C:\OpenSSL\bin\openssl sha1 -binary | "C:\OpenSSL\bin\openssl base64 (Enter) 

It will ask you for password which is android.



回答12:

1) Create a key to sign your application, and remember the alias.

2) Install OpenSSL.

3) Put the bin folder of OpenSSL in your path.

4) Follow the steps mentioned under "Setup Single Sign-On" on the FB-Android-SDK page, and generate your Hash Key. Make sure you put the correct alias and keystore file name.

5) Create an application on Facebok, and under Mobile Devices tab, enter this Hash Key.



回答13:

You need to create a keystore by the keytool for signed apps for android like the procedure described in Android Site and then you have to install cygwin and then you need to install openssl from google code then just execute the following command and you will get the hash key for android and then put that hash key into the facebook application you created. And then you can access the facebook application through the Android Application for posting wall ("publish_stream") could be an example.

$ keytool -exportcert -alias alias_name -keystore sample_keystore.keystore | openssl sha1 -binary | openssl base64

You need to execute the above command from cygwin.



回答14:

Official Documentation on facebook developer site:

@Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);      // Add code to print out the key hash     try {         PackageInfo info = getPackageManager().getPackageInfo(                 "com.facebook.samples.hellofacebook",                  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) {      } 


回答15:

  1. Simply Open you Main Activity File and create below mention function:

         try {     PackageInfo info = getPackageManager().getPackageInfo(             "your.application.package.name",             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 (PackageManager.NameNotFoundException e) {  } catch (NoSuchAlgorithmException e) {  } 

1.1 Run you Application, this will generate a Hash key for your application.

  1. Now, Open log cat and search with "KeyHash" and copy the hash key.

  2. One you generate the Hash key you can remove this function.



回答16:

keytool -exportcert -alias androiddebugkey -keystore       C:\Users\pravin\.android\debug.keystore | "H:\OpenSSL\bin\openssl" sha1 -binary | "H:\OpenSSL\bin\openssl" base64 

This worked for me ...

Steps:

1) Open command line go to - > java Keytool..... for me C:\Program Files\Java\JDK1.7\bin 2) Download OpenSSL from google 3) paste this with changing your paths -    keytool -exportcert -alias androiddebugkey -keystore C:\Users\pravin\.android\debug.keystore | "H:\OpenSSL\bin\openssl" sha1 -binary | "H:\OpenSSL\bin\openssl" base64       ....................   give proper debug.keystore path and openSSL path ..   4) Finley it may be ask u password .. so give password -> android   ... 5) you will get 28 characters that will be your has key 


回答17:

For Linux

Open Terminal :

For Debug Build

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

you wil find debug.keystore from ".android" folder copy it from and paste on desktop and run above command

For release Build

keytool -exportcert -alias  -keystore  | openssl sha1 -binary | openssl base64 

NOTE : Make sure In Both case it must ask for password. If it does not asks for password that means something is wrong in command.



回答18:

For an Android application

This code is used to get the hash key in your Android application for Facebook integration. I have tested all devices and it's working. Only change the package name of this code:

private void facebookHashKey() {      try {         PackageInfo info = getPackageManager().getPackageInfo("com.app.helpcove", PackageManager.GET_SIGNATURES);         for (Signature signature : info.signatures) {             MessageDigest md = MessageDigest.getInstance("SHA");             md.update(signature.toByteArray());             String hashCode  = Base64.encodeToString(md.digest(), Base64.DEFAULT);             System.out.println("Print the hashKey for Facebook :"+hashCode);             Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));         }     } catch (NameNotFoundException e) {      } catch (NoSuchAlgorithmException e) {      } } 


回答19:

To generate a hash of your release key, run the following command on Mac or Windows substituting your release key alias and the path to your keystore.

On Windows, use:

keytool -exportcert -alias  -keystore  | openssl sha1 -binary | openssl base64 

This command should generate a 28 characher string. Remember that COPY and PASTE this Release Key Hash into your Facebook App ID's Android settings.

image: fbcdn-dragon-a.akamaihd.net/hphotos-ak-xpa1/t39.2178-6/851568_627654437290708_1803108402_n.png

Refer from : https://developers.facebook.com/docs/android/getting-started#release-key-hash and http://note.taable.com



回答20:

The simplest solution:

  1. Don't add the hash key, implement everything else
  2. When facebook login is pressed, you will get an error saying "Invalid key hash. The key hash "xxx" does not match any stored key. ..."
  3. Open the facebook app dashboard and add the hash "xxx=" ("xxx" hash from the error + "=" sign)


回答21:

The best approach is to use the following code:

private void getHashKey(String pkgName) {     try     {         PackageInfo info = getPackageManager().getPackageInfo(pkgName, PackageManager.GET_SIGNATURES);         for (Signature signature : info.signatures)         {             MessageDigest md = MessageDigest.getInstance("SHA");             md.update(signature.toByteArray());             String hashKey = Base64.encodeBytes(md.digest());             _hashKey_et.setText(hashKey);             Log.i("KeyTool", pkgName + " -> hashKey = " + hashKey);         }     }     catch (NameNotFoundException e)     {         e.printStackTrace();     }     catch (NoSuchAlgorithmException e)     {         e.printStackTrace();     } } 

But I was so frustrating with the fact that there is no simple tool to generate the HashKey for the Facebook app. Each time I had to play with Openssl and Keytool or to use a code to get the hash from signature ...

So I wrote a simple KeyGenTool that will do that work for you: -> KeyGenTool on Google Play

Enjoy :)



回答22:

I did a small mistake that should be kept in mind. If you are using your keystore then give your alias name, not androiddebugkey...

I solved my problem. Now if Facebook is there installed in my device, then still my app is getting data on the Facebook login integration. Just only care about your hash key.

Please see below.

C:\Program Files\Java\jdk1.6.0_45\bin>keytool -exportcert -alias here your alias name  -keystore "G:\yourkeystorename.keystore" |"G:\ssl\bin\openssl" sha1 -binary | "G:\ssl\bin\openssl" base64 

Then press Enter - it will ask you for the password and then enter your keystore password, not Android.

Cool.



回答23:

import java.security.MessageDigest; import java.security.NoSuchAlgorithmException;  import android.os.Bundle; import android.app.Activity; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.Signature; import android.text.Editable; import android.util.Base64; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText;  public class MainActivity extends Activity {      Button btn;     EditText et;     PackageInfo info;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         btn=(Button)findViewById(R.id.button1);         et=(EditText)findViewById(R.id.editText1);         btn.setOnClickListener(new OnClickListener() {              @Override             public void onClick(View v) {                 // TODO Auto-generated method stub                  try {                     info = getPackageManager().getPackageInfo("com.example.id", PackageManager.GET_SIGNATURES);                     for (Signature signature : info.signatures) {                         MessageDigest md;                         md = MessageDigest.getInstance("SHA");                         md.update(signature.toByteArray());                         String something = new String(Base64.encode(md.digest(), 0));                         //String something = new String(Base64.encodeBytes(md.digest()));                         et.setText("" + something);                         Log.e("hash key", something);                     }                 } catch (NameNotFoundException e1) {                     Log.e("name not found", e1.toString());                 } catch (NoSuchAlgorithmException e) {                     Log.e("no such an algorithm", e.toString());                 } catch (Exception e) {                     Log.e("exception", e.toString());                 }             }         });     }    } 


回答24:

Firstly, to generate your key hash on your local computer, run Java's keytool utility (which should be on your console's path) against the Android debug keystore. This is, by default, in your home .android directory).

on ubuntu, it's woking in my computer

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


回答25:

For people who dont know how to code, AivarsDa's key gen tool on google play i the easiest solution. just get the hash key from it and copy to the facebook app settings. The share on the keygen tool makes my app crash on my phone, so i just manually typed it in notepad and copied it to fb. Finally past this step after days of frustration



回答26:

Use this command after keep open ssl in c drive and in openssl folder.

keytool -exportcert -alias androiddebugkey -keystore "C:\Users\AJAY SUNDRIYAL.android\debug.keystore" | openssl sha1 -binary | openssl base64 


回答27:

As answered on a similar issue i found this to be working for me:

  • Copy the apkname.apk file you want to know the hash of to the 'Java\jdk1.7.0_79\bin' folder
  • Run this command keytool -list -printcert -jarfile apkname.apk
  • Copy the SHA1 value and convert it using this site
  • Use the converted Keyhash value (ex. zaHqo1xcaPv6CmvlWnJk3SaNRIQ=)


回答28:

Use this for print key hash in kotlin

try {         val info = context.getPackageManager().getPackageInfo(context.packageName,                 PackageManager.GET_SIGNATURES);         for (signature in info.signatures) {             val md = MessageDigest.getInstance("SHA")             md.update(signature.toByteArray())             Log.d("Key hash ", android.util.Base64.encodeToString(md.digest(), android.util.Base64.DEFAULT))         }     }catch (e:Exception){      } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!