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,
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.
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) {
}
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. :)
Kotlin code to get Hash key
private fun logHashKey() {
try {
val info = getPackageManager().getPackageInfo("your.package.name", PackageManager.GET_SIGNING_CERTIFICATES);
for (signature in info.signingInfo.signingCertificateHistory) {
val md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
val something = Base64.getEncoder().encodeToString(md.digest());
Log.e("hash key", something);
}
} catch (e1: PackageManager.NameNotFoundException) {
Log.e("name not found", e1.toString());
} catch (e: NoSuchAlgorithmException) {
Log.e("no such an algorithm", e.toString());
} catch (e: Exception) {
Log.e("exception", e.toString());
}
}
Please don't forgot to generate keys in Debug and Release environment as they change as per build setting.
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());
}
}
});
}
}
[EDIT 2020]-> Now I totally recommend the answer here, way easier using android studio, faster and no need to wright any code - the one below was back in the eclipse days :) -.
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 ;)