SDK for Android:How facebook check the Hash key? - android

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

Related

Few questions about key hash for Facebook SDK

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.

can not login facebook when export apk eclipse

I'm trying to use Facebook Android SDK to develop a app with the Facebook Login. When I debug it run but when I export it to apk and run, it show notification invalid hash key...
When setting my app in enter link description here
I use two key for debug key and release key.I try log key hash in my app with method
public static void showHashKey(Context context) {
try {
PackageInfo info = context.getPackageManager().getPackageInfo(
"com.example.tryitonjewelry", PackageManager.GET_SIGNATURES); //Your package name here
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.i("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
}
And I see it like key debug so why the export of APK reappeared error. In error i see key invalid it not like either key I setting.

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

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.

Android: Facebook login cannot call the selection fragment (Facebook api 3.0)

I followed the tutorial of Facebook to create a login function with Facebook (
enter link). When running the example, I had problem: after authorizing the app, I cannot see the welcome Text (selection fragment), it just went back the login page(splash fragment).
As my debug, the Session of Facebook seems not to be opened. Please give me some advices. Thanks
Make sure you're providing the correct Key Hash.
Take your key with this method:
static final String GetKey(Context context)
{
String KeyHash = "";
PackageInfo info;
try {
info = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures)
{
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
KeyHash = Base64.encodeToString(md.digest(), Base64.DEFAULT);
}
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return KeyHash;
}
I had a similar problem and it was because I was not overriding the onActivityResult method, after I implemented that everything worked. Since it seems that your session is failing authentication have you updated your applicationId in the manifest? I'm not sure it really matters but there is also a section on the facebook app settings page that requires the name of your activity, make sure that matches your current app.

Categories

Resources