2012-03-19

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. Here is an official documentation of facebook integration.



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) using keytool 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.

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



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.



and note down your facebook App ID

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://github.com/facebook/facebook-android-sdk.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.

4. Open Your Main Activity Class and initialize all the variables needed.

5. I created a simple interface which contains button to login, post to wall, show access tokens and logout for testing purpose.

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

and function body for loginToFacebook() function is:

Posting Message to Facebook Wall

write a click event for post to wall button and inside click event write a function named postToWall()

and function body for postToWall() function is:

Getting Profile Information from Facebook

To get profile information we need to make an api request to facebook graph API. Following is a function that will make an api request to facebook profile graph api and will get profile information from facebook.

The above function will get json data from facebook. You need to parse the json in order to get individual profile data. If you are not aware of json parsing look at this article. Android JSON Parsing Tutorial.

The sample profile json from facebook will be like this

Extending facebook Permissions

If you want user’s other information like checkins, friends list etc., you need to extend facebook permissions while logging in user. Check list of facebook permissions

Getting Access Token

Sometimes you might needed users access token for future purpose usage. Following code will give you currently logged in user access token.

Logout from your app

When user want to stop using facebook for your app, you can provide logout method to clear app state and invalidate access token. So further you can’t make request to facebook from your app.

This image is for thumbnail purpose

Show more