0

I am trying to access Facebook info of loggedIn user. For that I read through the various docs and found that hash key configuration is required for this on developer section of facebook where application is created and generating Facebook App-Id. I tried to generate hash key for filling into Native Android App section on facebook developer portal. I gone through many links for this like Here

But hash key is not working at all. Sometimes it says no hash is match or sometime it just promt a facebook dialog and disappears.

But still it's not working in my Device. It's working fine on Emulator. I am using Mac system, is this any issue?

Can anybody guide me if I am missing something? Do I need to edit more things in Application setting on facebook developer portal?

Community
  • 1
  • 1
Suresh Sharma
  • 1,826
  • 22
  • 41

3 Answers3

0

This is because the device is creating a new key hash which is not in your app settings. So you have to update the keyhash accordingly. Use this in onCreate()

try {
            PackageInfo info = getPackageManager().getPackageInfo("YOUR_PACKAGE_NAME", 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) {

        }
Himanshu Joshi
  • 3,391
  • 1
  • 19
  • 32
  • I also tried this from this link http://stackoverflow.com/questions/5989237/generating-hash-key-for-app-using-facebook-sdk But no luck. – Suresh Sharma Oct 30 '13 at 12:23
  • did u enter your package name correctly in facebook app settings and in code? – Himanshu Joshi Oct 30 '13 at 12:24
  • Yes it is just same as it's declared in Manifest file as package="XXX.XXX.XXX" is that OK ? m i missing something. Should it be similar what is mentioned in Native IOS APP on Facebook Developer Portal as bundle ID or not mendatory ? – Suresh Sharma Oct 30 '13 at 12:46
  • if you are using package name as com.example.xyz, then in your facebook under native android app use the same package name com.example.xyz – Himanshu Joshi Oct 30 '13 at 12:48
  • Yes, i did exactly same package name but it's still not working while in Emulator it's working perfectly. – Suresh Sharma Oct 31 '13 at 06:41
  • did u hve installed facebook sdk in your device or emulator? – Himanshu Joshi Oct 31 '13 at 06:54
  • I dont understand facebook sdk installation in device. How can i do that. But yes, the facebook official application works fine in my device samsung Galaxy S3. is there anything should be done? – Suresh Sharma Oct 31 '13 at 07:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40296/discussion-between-droid-and-suresh-sharma) – Himanshu Joshi Oct 31 '13 at 07:25
0

This has been an issue for many people. Alternatively from generating the hash from commandline, you can generate it from code, and then print it to the log so you can copy it from there.

The following code does that. You can place it in your activity onCreate() for instance, and remove it once you have the hash. Obviously you need to change the package name to your own.

try {
    PackageInfo info = getPackageManager().getPackageInfo(
            "com.facebook.samples.loginhowto", 
             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) {
}

This answer nicely describes how to do the same from commandline, if you don't want to use the code. Both should have the same result.

Community
  • 1
  • 1
Jeffrey Klardie
  • 3,020
  • 1
  • 18
  • 23
0

Try this when session open

try {
            PackageInfo info = getPackageManager().getPackageInfo("YOUR_PACKAGE_NAME", 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) {

        }
Gajendra Rawat
  • 3,673
  • 2
  • 19
  • 36
  • I also tried this from this link http://stackoverflow.com/questions/5989237/generating-hash-key-for-app-using-facebook-sdk But no luck. – Suresh Sharma Oct 30 '13 at 12:23
  • @SureshSharma add this code onCreate() method in your MainActivity – Gajendra Rawat Oct 30 '13 at 12:25
  • yes i did it already...i have splash activity as main in my application. I created hash key by using this method in there onCreateMethod. and just copy this from the DDMS logcat and paste to the hash key field in my Native Android App section of facebook developer portal. But still it's not working. Please let me know if i am missing something. – Suresh Sharma Oct 30 '13 at 12:35
  • you can not search hash key by simply pasting hashkey in Logcat(i dont know why :( ). you have to search it in Logcat @SureshSharma. and also clean your project before run. – Gajendra Rawat Oct 30 '13 at 12:37
  • No i just print console like: 10-30 18:22:52.646: I/System.out(15333): the hash key=====kOnrWjnscryHCsV7H7+Pjzu4FcI= and get hash key and configure the same to facebook developer Native Android App section. Is that OK? – Suresh Sharma Oct 30 '13 at 12:54
  • My app packagename is com.example.appname is there any issue. can we write 'example' in package name? – Suresh Sharma Oct 30 '13 at 12:55
  • yes this is default package name but it should be use reverse domain name of your organization. Its a convention not mendetory @SureshSharma – Gajendra Rawat Oct 30 '13 at 12:59