-1

I have to use login with Facebook functionality in my android application. For this I need to generate key hash. I am using following code to generate facebook key hash.

public class GenerateFacebookSignature extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        printHashKey();
    }

    public void printHashKey() {
        try {
            PackageInfo packageInfo = getPackageManager().getPackageInfo("com.example.facebooklogin", PackageManager.GET_SIGNATURES);
            for (Signature signature : packageInfo.signatures) {
                MessageDigest messageDigest = MessageDigest.getInstance("SHA");
                messageDigest.update(signature.toByteArray());

                Log.d("FaceBookKeyHash:", Base64.encodeToString(messageDigest.digest(), Base64.DEFAULT));
            }
        }
        catch (PackageManager.NameNotFoundException e) {
        }
        catch (NoSuchAlgorithmException e) {
        }
    }
} 

This class is under a different package named com.example.facebooklogin.application while my other application classes are in the package com.example.facebooklogin.

I have included this in my manifest.xml file but it is not showing any hash key in logcat.

<application
        android:name=".application.GenerateFacebookSignature">
</application>

Can anybody point out what I am doing wrong?

Egor Neliuba
  • 14,784
  • 7
  • 59
  • 77
  • check out my answer here http://stackoverflow.com/questions/4388992/key-hash-for-android-facebook-app/17732453#17732453 – Shahar Mar 21 '16 at 13:40

1 Answers1

2

It is raising an exception, because it can't find "com.example.facebooklogin".

Change it to your package name and it should work. You can use Context#getPackageName() like so:

PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);
Egor Neliuba
  • 14,784
  • 7
  • 59
  • 77
  • But it is not showing any exception means application is running well. – Ashish Kumar Gupta Apr 21 '15 at 10:56
  • You are swallowing exceptions by not printing anything in the logcat. Use `e.printStackTrace()` inside `catch` clauses. – Egor Neliuba Apr 21 '15 at 10:58
  • i have e.printStackTrace() but it is running well till. – Ashish Kumar Gupta Apr 21 '15 at 11:01
  • i have used getPackageName() also but no key hash would be generated. – Ashish Kumar Gupta Apr 21 '15 at 11:03
  • @AshishKumarGupta, alright, add a different `Log.d()` message inside each piece of your code: `onCreate`, `printHashKey`, `try`, the `for` loop, both `catch`. This way you can see what gets executed and what's not executed and report back. You can also do the same thing with a debugger. – Egor Neliuba Apr 21 '15 at 11:07
  • Now i am having a facebook connect button in my activity on whose click i want the user to got login with facebook. Whereas facebook documentation is giving a default button for login purpose. Which activity i would call to login my user on my own button click. – Ashish Kumar Gupta Apr 21 '15 at 11:57
  • @AshishKumarGupta, post a separate question and someone will help you. – Egor Neliuba Apr 21 '15 at 13:21