Integration of android application with facebook.

Thursday, September 27, 2012

This tutorial is about integrating facebook into your android application. I am going to explain various steps like generating your application signature, registering facebook application, downloading facebook sdk and other steps.

  • Generating App Signature for Facebook Settings

To create facebook android native app you need to provide your Android application signature in facebook app settings. You can generate your application signature (keyhash) usingkeytool that comes with java. But to generate signature you need openssl installed on your pc. If you don’t have one download openssl from here and set it in your system environment path.
Open your command prompt (CMD) and run the following command to generate your keyhash. While generating hashkey it should ask you password. Give password as android. If it don’t ask for password your keystore path is incorrect.

keytool -exportcert -alias androiddebugkey -keystore "<path-to-users-directory>\.android\
debug.keystore" | openssl sha1 -binary | openssl base64

check the following command how i generated hashkey on my pc.

keytool -exportcert -alias androiddebugkey -keystore "C:\users\raj\.android\debug.keystore"
| openssl sha1 -binary | openssl base64

  • Registering your Facebook Application

After generating your app signature successfully, register your facebook application by going to create new facebook application and fill out all the information needed. And select Native Android App and give your hashkey there which you generated previously using keytool.

  • Creating Facebook Reference Project

Once you are done with registering your facebook application, you need to download facebook SDK and create a new reference project. This reference project will be used to compile your actual project.
1. Download facebook android SDK from git repositories.
(git clone git://
2. In your Eclipse goto File ⇒ Import ⇒ Existing Projects into Workspace and select the facebook project you downloaded from git repository.

  • Creating Your Facebook Connect Project

1. Create new Project in your Eclipse IDE. File ⇒ New ⇒ Android Project and fill out all the details.
2. Now we need to add reference of this project to existing facebook project. Right Click on Project ⇒ Properties ⇒ android ⇒ Click on Add button ⇒ select your facebook project ⇒ Click Apply.

Now our project setup is done. We can start coding our facebook application.
3. Open your AndroidManifest.xml file add network connect permission in order to connect to internet.
<uses-permission android:name="android.permission.INTERNET"/>

4. Open Your Main Activity Class and initialize all the variables needed.
public class AndroidFacebookConnectActivity extends Activity {
    // Your Facebook APP ID
    private static String APP_ID = "308180782571605"; // Replace your App ID here
    // Instance of Facebook Class
    private Facebook facebook;
    private AsyncFacebookRunner mAsyncRunner;
    String FILENAME = "AndroidSSO_data";
    private SharedPreferences mPrefs;
    public void onCreate(Bundle savedInstanceState) {
        facebook = new Facebook(APP_ID);
        mAsyncRunner = new AsyncFacebookRunner(facebook);
5. After that make changes based on your requirement.

Login to Facebook Account

I used a button to login into facebook account. In your activity write a click event for Login button click. Inside click event declare a function named loginToFacebook();
Login button click event
btnFbLogin.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
and function body for loginToFacebook() function is:
public void loginToFacebook() {
    mPrefs = getPreferences(MODE_PRIVATE);
    String access_token = mPrefs.getString("access_token", null);
    long expires = mPrefs.getLong("access_expires", 0);
    if (access_token != null) {
    if (expires != 0) {
    if (!facebook.isSessionValid()) {
                new String[] { "email", "publish_stream" },
                new DialogListener() {
                    public void onCancel() {
                        // Function to handle cancel event
                    public void onComplete(Bundle values) {
                        // Function to handle complete event
                        // Edit Preferences and update facebook acess_token
                        SharedPreferences.Editor editor = mPrefs.edit();
                    public void onError(DialogError error) {
                        // Function to handle error
                    public void onFacebookError(FacebookError fberror) {
                        // Function to handle Facebook errors


Post a Comment

Note: Only a member of this blog may post a comment.