Pages

Monday, April 1, 2013

How to get Hash Key for integarte facebook SDK in android application


Hello Everyone today I am posting to get Hash Key for Facebook integration.




There is a two way to get Hash Key.

1. Using CMD

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

and then press Enter. Insert password: android

 - You will get your hash key for debug.keystore.

 - When you export a signed APK and you create keystore for application, just replace in cmd debugkeystore alias with your alias for app, keystore path with path to your new created keystore for app and insert password for your app.keystore and you will get a new hash key for your signed app.

2. Just Copy below code in Activity class:

Open up the Your Activity class and make the following temporary change to the onCreate() method:
This is easy to use other then ssl and so.

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

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.os.Bundle;
import android.util.Base64;
import android.util.Log;

public class MainActivity extends Activity
{
 @Override
 public void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);

  // Add code to print out the key hash
  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());
  }
 }
}

If you Run app without signed APK then You will get your hash key for debug.keystore.
When you export a signed APK, just signed APK with your keystore and Run the Application.

You can delete the code after knowing the key ;)
enjoy !

Note :- If you getting below error in Facebook integration.
A non-native Login Dialog is displayed that includes an error message at the top:

''..App is Misconfigured for facebook login...''.

Besides double checking your key hash generation steps.

6 comments:

  1. HI Durgesh, i had added your code but there nothing showing in logcat.

    ReplyDelete
    Replies
    1. Hello Soma, I think you are missing to replace package name. I edited my post now check it.

      Delete
    2. please check it thoroughly ,definitely it will be work, i used so many code to get key hash but finally using this one i got keyhash from my log

      Delete
  2. hello i face some difficult problem , i got different hash key using code and cmd, which one more preferable ? i used both simultaneously , but in android facebook apps i cant success to post image , through default app-id image share but with my app-id not, i set all thing see in my stackoverflow question "http://stackoverflow.com/questions/25528091/android-image-can-not-share-in-facebook-using-my-app-id" and please give a solutions. thank you

    ReplyDelete
    Replies
    1. Hello Nirav,
      I think you using different keystore to generate Hash key on code and CMD. that's why it is not same. First you need to generate correct hash key using same keystore which one you use to sign APK to publish. Make sure you create correct hash key after you need test sign APK file.
      thank you.

      Delete
  3. Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include.
    taxi dispatch software

    ReplyDelete