Few questions about key hash for Facebook SDK - android

I am developing an iOS app in swift and I am new to iOS but I have good knowledge of android. In Android we generally use:
public static void printHashKey(Context pContext) {
try {
PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
String hashKey = new String(Base64.encode(md.digest(), 0));
Log.i(TAG, "printHashKey() Hash Key: " + hashKey);
}
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "printHashKey()", e);
} catch (Exception e) {
Log.e(TAG, "printHashKey()", e);
}
}
to generate a key hash but now I need key hash to register my iOS project on facebook development console. I have no idea how to do it. So I want understand:
What is the role of key-hash. Is it like: In development mode if we make a build from our IDE and the machine key-hash is register on facebook then we can use facebook login else not. But from android code to generate key-hash it seems like package dependent(Uff!! lots of confusion).
How to generate key hash for an iOS app?
Can we use same key has for both iOS and android application?
Please share your thoughts and solution.

Related

invalid key hash - android facebook

I've created an app which uses the facebook login. I've added the key hash to the facebook developer page and it worked fine in all devices.
Now I've uploaded the app to google play, and when I try to login, it sais that the key hash does not match any stored key hashes.
I copied the key hash in the error message and pasted it. Still - doesn't work.
I tried to generate a key hash in cmd using the release key store - no luck.
Does anyone know what is the problem and how to fix it?
Thanks in advance!
Try adding the following code snippet to your app while it's signed with the release certificate, compare the hash output with the one you submitted to the Facebook portal:
PackageInfo info;
try {
info = getPackageManager().getPackageInfo("com.package.name", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md;
md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
String hash= new String(Base64.encode(md.digest(), 0));
Log.e("hash", hash);
}
} 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());
}

error while adding development keyhash in developer.facebook.com

i am currently making a demo app for integrating Facebook sso and i made a debug keyhash for login via facebook but when i am adding development keyhash in facebook developers site its asking me for the release keyhash at the same time and i dont want to upload this demo to the playstore and its not allowing me to finish the process.
Please anyone suggest me something so that i can proceed.
public void generateHashKeyForFacebook(Context context) throws Exception {
try {
PackageInfo info = context.getPackageManager().getPackageInfo("com.yourPackageName", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("FBKeyHash >>> ", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
Just put this function to your activity and see your log cat for obtaining your hash key
Below is the link
https://developers.facebook.com/docs/android/getting-started

Generating hash key for facebook development

I have recently started working on facebook API, where I came under the situation of generating Hash key and registering it on facebook for further use.
For that, I used the following code
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());
}
All worked well, as I was able to work with facebook in my app.
But, after publishing the app on playstore, I found the error Invalid_Android_key parameter. The key does not match any allowed key Configure your app key hashes at https://developers.facebook.com/apps/..........
Please, let me know the cause of this problem and how to handle this.
I had same Problem, after creating the apk, the key hash is changed! because using this code u get the debug keystore hash, but when creating apk, it's another hash, gotta capture it from log after trying ur apk on emulator , then delete code and export again without this log , i know it's a hassle but for me it was easier than keytool...

Facebook Integration in Android Application

I had integrated facebook in my android application .
Generate key using debugkeytool and it works fine on both emulator and real device.
Now i have to make release apk file and i had created keystore using eclipse android tool
to export signed application package .
And using this keystore i had generated new key hash for facebook and set it on facebook developers site.
but still i am not able to post on facebook wall after signing my app with my own created keystore.
I had check all the steps for creating keystore and it is correct.
please help me out of this situation.
Thanks
I got the same error but when i checked the hash key by PackageManager i got the different hash key of the application and update it on facebook and it worked for me.
PackageInfo info;
try {
info = getPackageManager().getPackageInfo("com.example.yourpackagename", 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());
}
change your package name in the code. The hash key will be printed in the log.
It may help you.

SDK for Android:How facebook check the Hash key?

When we want to use Facebook SDK for Android as our SSO solution, we need to put our Android application signature into our Facebook application settings (Step 5 of Facebook sdk for android).
And that signature should be generated by running the keytool that comes with the Android SDK.
I am curious how facebook verify this signature?
After more than one year, I think I'd better answer my question.
Android's app can get other app's signature by:
public String WriteSignature(String packageName)
{
PackageManager pm = this.getPackageManager();
String sig = "";
PackageInfo pi = null;
try {
pi = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
} catch (NameNotFoundException e1) {
e1.printStackTrace();
}
try {
for (Signature signature : pi.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
sig = Base64.encodeToString(md.digest(), Base64.DEFAULT);
Log.d(ACTIVITY_TAG, sig);
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return sig;
}
For facebook app to function we need to provide Facebook "App Id"
For android platform we need to provide package name, class name & "HashKey
App will mostly have 2 HashKey for "Debug" & "Release" version of app
When app access facebook, SDK internally generate & compare the HashKey with one submitted during the facebook app

Categories

Resources