1 / 5

Integrate Facebook Easily in your Android App with this Tutorial.

Follow these simple steps to integrate Facebook using App42 social API in your android app so that your users can post and track their feed smoothly.

shephertz
Download Presentation

Integrate Facebook Easily in your Android App with this Tutorial.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. With the number of Facebook mobile users increasing rapidly, Facebook integration in Android Apps is highly recommended Click To Tweet Also Read: How to Integrate Twitter 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:

  2. 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. 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 c ommand. 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. 3. This is called only the first time after installation with Facebook. 4. Once your application is authorized , you can use Facebook API directly. 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. @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // super.onActivityResult(requestCode, resultCode, data);

  4. super.onActivityResult(requestCode, resultCode, data); if (!UserContext.authorized) { FacebookService.instance().authorizeCallback(requestCode, resultCode, data); UserContext.authorized = true; } } 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. publicvoid fetchFacebookProfile(final FriendList hostActivity) { if(mUIThreadHandler == null){ mUIThreadHandler = new Handler(); } facebook.authorize(hostActivity, new String[] { "friends_online_presence"} , new DialogListener() { @Override publicvoid 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 publicvoid onFacebookError(FacebookError error) { hostActivity.onFbError(); } @Override publicvoid onError(DialogError e) { hostActivity.onFbError(); } @Override

  5. publicvoid 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. @Override protectedvoid 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. 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. 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.

More Related