How to Integrate Facebook in your Android App

Social media integration is one of the most important modes of OAuth for any App. Recently we have been getting a lot of queries on ‘how to integrate Facebook with Apps’. To resolve these queries we have created a sample Android App and below mentioned are the steps to integrate Facebook with this App.

6 How to Integrate Facebook in your Android App

About the application:

1. This application shows how we can integrate an Android App with Facebook API.
2. The sample shows how we can get our Facebook friends to increase the online presence.

Running Sample:

1. First of all download Facebook SDK from here .
2. Create a Facebook application here to get a Facebook APP ID for your application.
3. You need to create a key hash of your signature from android debug.keystore (for the development stage).
4. For this First download OpenSSL from here and extract to a folder (in my case, c:\openssl) and follow these steps.

1
2
3
4
5
6
7
8
9
10
11
12
A. To create key hash, you need to navigate to your JAVA jdk folder, where the keytool.exe is. (in my case, in windows 
    is: C:\Program Files (x86)\Java\jdk1.6.0_16\bin)
B. This also requires path of your debug.keystore (in my case, in windows is: C:\Users\MyUsername\.android).
C. Now open your command prompt and navigate to the jdk bin folder and run following command.
   keytool -exportcert -alias androiddebugkey -keystore "" > C:\openssl\bin\debug.txt
D. Navigate to C:\openssl\bin\ using command prompt and run following commands.
   openssl sha1 -binary debug.txt >debug_sha.txt
   openssl base64 -in debug_sha.txt >debug_base64.txt
E. debug_base64.txt contains the key hash.
F. Copy this key hash to your Facebook Application that you made in step 2.
   (Edit Settings -> Native Android App -> Key Hashes:) and also enable Facebook login and save it.
G. Now your application is authenticated with Face-Book.

5.  Download the project from here and import it in the eclipse.
6.  Import Facebook SDK project in your eclipse (which you have downloaded in step 1) and make it a library project.
7.  Add this library project into your sample android application.
8.  Open Constants.java file and change FB_APP_ID variable value with your FB APP ID.
9.  Build your android application and install on your android device.

Design Details:

Authorization With Facebook: To use Facebook API in your android application you have to authorize application. As a sample I have authorized my application in FacebookService.java file. In this method you have to pass following three parameters :

1. Your host Activity on which you have to get callback from Facebook API.
2. All Facebook API permissions required for your application in form of String array.(in this sample I had requested for friends_online_presence).
3. This is called only the first time after installation with Facebook.
4. Once your application is authorized , you can use Facebook API directly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public void fetchFacebookProfile(final FriendList hostActivity)
{
if(mUIThreadHandler == null){
mUIThreadHandler = new Handler();
}
facebook.authorize(hostActivity, new
String[] {
"friends_online_presence"}
, new DialogListener() {
@Override
public void onComplete(Bundle values)
{ if(mPrefs == null){
mPrefs = appContext.getSharedPreferences("MyGamePreferences", android.content.Context.MODE_PRIVATE);
}
SharedPreferences.Editor editor = mPrefs.edit();
UserContext.accessToken= facebook.getAccessToken();
editor.putString("access_token", facebook.getAccessToken());
editor.putLong("access_expires", facebook.getAccessExpires());
editor.commit();
FacebookService.this.getFacebookProfile(hostActivity);
}
 
@Override
public void onFacebookError(FacebookError error) {
hostActivity.onFbError();
}
 
@Override
public void onError(DialogError e) {
hostActivity.onFbError();
}
 
@Override
public void onCancel() {
hostActivity.onFbError();
}
});
}

Authorization callback : After the authorization step you have to authorize callback as an acknowledgement in onActivityResult method of your host Activity. This is done in FriendList.java file.

1
2
3
4
5
6
7
8
9
10
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
if (!UserContext.authorized) {
FacebookService.instance().authorizeCallback(requestCode,
resultCode, data);
UserContext.authorized = true;
}
}

Get Facebook Profile: You can get your Facebook profile after getting authorization (in onComplete() describe above) which is done in FacebookService.java file.

1
2
3
4
5
6
public void getFacebookProfile(FriendList callingActivity)
{
Bundle params = new Bundle();
params.putString("fields", "name, picture");
mAsyncRunner.request("me", params, new FacebookRequestListener(callingActivity));
}

Get Facebook Friends: You can get your Facebook friends by writing your own query or can refer the following code , written in FacebookService.java file.

1
2
3
4
5
6
7
8
9
10
public void getFacebookFriends(FacebookFriendListRequester caller){
if(mUIThreadHandler == null){
mUIThreadHandler = new Handler();
}
Bundle params = new Bundle();
params.putString("method","fql.query");
params.putString("query","SELECT name,uid,pic,online_presence FROM user WHERE uid IN
( SELECT uid2 FROM friend WHERE uid1 = me()) ORDER BY name" );
mAsyncRunner.request(params, new FacebookFriendListRequest(caller));
}

It would be great to hear the views from the readers of this blog. In case you have any more questions or suggestions, please feel free to reach out to us at support@shephertz.com.

twitter How to Integrate Facebook in your Android Appfacebook How to Integrate Facebook in your Android Appgoogle How to Integrate Facebook in your Android Applinkedin How to Integrate Facebook in your Android Apppinterest How to Integrate Facebook in your Android Appreddit How to Integrate Facebook in your Android Appstumbleupon How to Integrate Facebook in your Android Apptumblr How to Integrate Facebook in your Android App

Leave a Reply

Your email address will not be published. Required fields are marked *


6 − four =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>