简体   繁体   中英

Android - getting hash key for Facebook Integration

I am trying to implement Facebook into my app to allow things like sending stuff to the user's wall, updating status', etc.

So after downloading the stuff I was told to get a key using the keystore file. I decided to be brave and create my own keystore file (for publishing stuff later) and did so successfully.

After creating the file though I am having all sorts of problems trying to retreieve a key from it, i entered the command via keytool/command prompt and it asked me for my keystore password...

After entering the password i set in the first place, I am getting nothing but wierd ascii symbols and letters and my keystore details also in the middle of it. What is going wrong??

UPDATE: Ok, I am aware you may need to use OpenSSL to display the text correctly. I have installed OpenSSL but how do I use it to get the Hash code?

  1. Download Openssl from: http://code.google.com/p/openssl-for-windows/downloads/list

  2. Make a openssl folder in C drive

  3. Extract Zip files into openssl folder

  4. check that the directory the keytool executable is in is on your path. (For example, on my Windows 7 machine, it's in C:\\Program Files (x86)\\Java\\jre6\\bin.)
  5. open cmd and paste:

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

type password android if need you receive your hash code

I always use this method under Linux, because it fails in the original way:

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore > key.out
cat key.out | openssl sha1 -binary > key.bin
cat key.bin | openssl base64

You will get the key you need to enter on page mobile, android section of your Facebook apllication. Every time you use different keytore to sign your app, you need to change the key on the facebook page also.

The best method.

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

Some times the openssl provide the wrong keyhash.

you can use code below to get the Hash key :

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) {
   Log.e("name not found", e.toString());
  } catch (NoSuchAlgorithmException e) {
   Log.e("no such an algorithm", e.toString());
  }

Reference :

http://limbaniandroid.blogspot.com/2013/04/how-to-get-hash-key-for-integarte.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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