Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to generate keyhash for Android App(Facebook integration)

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 112
    Comment on it

    If you need to integrate Facebook SDK in your android app you have to generate keyhash, to configure Facebook API console. Add this method to your utility class and call this method from onCreate() of any Activity. For digital data integrity and authentication we need Signature class which provide the functionality of digital signature algorithm.

    public static String generateKeyHash(Activity context) {
            PackageInfo packageInfo;
            String key = null;
            
    	try {
                //getting application package name, as defined in manifest
                String packageName = context.getApplicationContext().getPackageName();
    
                // Retriving package name
                packageInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
    
                Log.e("Package Name=", context.getApplicationContext().getPackageName());
    
                for (Signature signature : packageInfo.signatures) {
                    MessageDigest md = MessageDigest.getInstance("SHA");
                    md.update(signature.toByteArray());
                    key = new String(Base64.encode(md.digest(), 0));
    
                    // String key = new String(Base64.encodeBytes(md.digest()));
                    Log.e("Key Hash=", key);
                }
            } catch (PackageManager.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());
            }
    
            return key;
        }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: