In This blog, we will look into how we can get or log Facebook Key Hash through Java.
The native Facebook login integration instructions are very clear.
You can read the official docs for the same.
But when you are working on the application in the debug/ development mode, then you might have seen this error :
Invalid Key Hash. The Key xxxxxxxxxxxxxxxxxxxxxxxxxxxx does not match any of the keys ……………
Seeing this itself is annoying, but when you are not able to add/ save the key you see on your developers.facebook dashboard, that moment is frustrating.
And the most probable reason for this is that you missed some invisible character in the key hash.
Also, many times you don’t even get the right key for the debug version you just started checking on some of your devices.
I am not saying running terminal commands is bad, but using android studio is pretty simple and straightforward.
So, I have got java code for printing my key hash as a string and I just print it in my launcher activity and check if this is a new key hash or some old, if it is a new key hash I just copy and update it to developers.facebook account dashboard.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 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("TAG", "MyActivity onCreate : " +Base64.encodeToString(md.digest(), Base64.DEFAULT)); } } catch (Exception e) { e.printStackTrace(); } } |