Code Explosion

Code That Explodes Conventions (In daily life as a programmer I meet problems. This blog where I record them (and more importantly their solutions) so that I can dig them in the future.)

Add your app’s signature to the Facebook App Settings (Alternative Approach)

While developing android apps for Facebook, Facebook requires additional layer of security for mobile apps in the form an application signature. Using keytool that comes with Java JDK, I found difficulties; I couldn’t make it somehow. But I found way to get the hash key. Here is the alternative way to get the hash key.


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Base64;
import android.util.Log;

public class Util {
private Context context;

public Util(Context context) {
this.context = context;
}

public void genHashKeyForFacebook() {
PackageInfo info;
try {
info = context.getPackageManager().getPackageInfo(
context.getPackageName(), PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md;

md = MessageDigest.getInstance("SHA1");
md.update(signature.toByteArray());
String hash = new String(Base64.encode(md.digest(), 0));
Log.e("hash key", hash);
}
} catch (NameNotFoundException e) {
Log.e("name not found", e.toString());
}

catch (NoSuchAlgorithmException e) {
Log.e("no such an algorithm", e.toString());
} catch (Exception e) {
Log.e("exception", e.toString());
}
}
}

Call this method genHashKeyForFacebook() from your activity; you will find the hash key in your LogCat verbose.

app's signature

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Information

This entry was posted on March 17, 2012 by in Android, facebook, Tips and tagged .

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 319 other followers

RSS Jenkov.com

  • SVG Compressed
    My recently published Kindle book, SVG Compressed, has received some great reviews already. 4 reviews with 5 stars. Not a bad start, but this is also my longest book so far, and it is available for only $0.99 on Amazon, so you get a lot for the money!
  • jQuery Compressed - 2nd Edition
    I have updated my Kindle book, jQuery Compressed 2nd edition, to be compatible with jQuery 1.11.0 and 2.1.0 .
  • Book Review: Purpose
    A book about why companies and individuals having a higher purpose achieve more in life.
  • Book Review: Crush It
    A motivating book about why now is the time to cash in on your passion. I am doing it. Are you?

my tweets

Follow

Get every new post delivered to your Inbox.

Join 319 other followers