How to integrate Facebook Login in any Application?

07 Sep 2015 Author : Harnil Oza
How to integrate Facebook Login in any Application?

Social Media Sites have been serving and helping people in different ways. It is considered an effective medium of communication. Because of these, some methods or ways are developed and introduced to maximize its functionality. For instance, many applications have been developed to make these social sites more functional.  There are many applications that can already be installed or integrated with the Facebook application. These different applications can cater to the different needs, concerns and interests of the Facebook users. These applications have also made Facebook more efficient and a more reliable site that can make communication more effective and reliable.

Integrating Facebook login in any application can make it more visible. To make integration easy, you just have to follow these simple steps to really get you going. To start with, you have to setup a Facebook testing account that will be used in the Android App so that you can login to Facebook and be able to make status updates. You should also have a Facebook application which is used to inform the user in the Android application that the application is asking you to login to Facebook.

The Facebook Test Account

A Facebook test account should be created because it will be used in the Android application. You have to sign up for Facebook new user account. If you already have an existing Facebook account, you still have to sign up for a new account using the name that you have registered in your existing account,  but you have to use another email address. After the account created, it will be converted to a testing account of Facebook. You have to remember always that the steps previously performed should not be done with your official Facebook account because converting an existing Facebook account into a test account can no longer be changed or altered. Hence, you have to see to it that the conversion should only be done with your new account or “dummy” account.

 Facebook Application

As soon as the “dummy” account is created, you have to create a Facebook application because integration with Facebook is based on OAuth 2.0 and will ask you to register a Facebook application. The Facebook application that will be created will have its own application ID that will be used in the Android application. Another important thing to remember is that an application cannot be created using a Facebook test account; hence, you should make a proper Facebook account to be able to make the application.

After creating the Facebook account and Facebook application, the next thing to do is to start coding the Android application. On the android Front, you can use the Facebook Android SDK, which can be found at http://github.com/facebook/facebook-android-sdk. The Facebook Android SDK is under the Apache License and is an open source.

The project has many samples in the examples folder; however, the center of the SDK is located in the Facebook folder. The sample application which is included in the Facebook SDK repository will allow the user to post a message on the wall by using a custom dialog. It also allows the user to send some text. The primary purpose why the sample application was made is to send an automated message to the wall. The application will produce a text which will be posted on the wall without displaying the dialog to the user. The user will just press the button to be able to view the generated message that will appear on her wall. Before you can start the said procedure you have to start by importing the Facebook project (which contains the actual Facebook SDK) into Eclipse. After which, the Eclipse Project will already be in your workspace. Considering that this is going to be the library project that will be used to do the actual Facebook integration, there is a need to create a reference in our own project to the Facebook Android SDK project. This can be performed by going to the project properties where you can select Android from the menu, then go to the library section and click Add. As you follow and view the menu, you can choose the Facebook Android SDK library project. After the selection has been made, it is now available in your own project. You will also have a view of the Eclipse Package explorer where the sample project also contains a reference to the Facebook Android SDK project.

Facebook SDK project works around the heart of com.facebook.android.Facebook.Facebook class and allows you to do different activities to Facebook. With the official Facebook app, you will be able to login/logoff without any difficulty. It will handle the OAuth integration and will provide a generic API that will enable you to perform requests to the different Facebook APIs.

The Facebook Connector will produce a rougher grains API compared to the finer or smoother grained that the Facebook class will provide. The Facebook class designed is broad or general. It allows you to perform different calls to Facebook. This design produces a fine grained API where you can get more information from the application by using the API. On the other hand the Facebook Connector will give you the coarse-grained method which is called the Post Message on Wall through the post button. You just have to provide the actual message that you want to post. The Post Message on Wall method will verify if you have a legal Facebook session.

In the event that the user has no valid Facebook session, the login method will be implemented by calling the Facebook, authorize to start the Facebook OAuth flow. Considering that the Facebook SDK has a single-sign-on option, you will then be able to control your existing Facebook session with the official Facebook Android application. You can also add the Facebook. FORCE_DIALOG_AUTH restriction so that the Facebook SDK will pop the login dialog. When you use this method the Facebook login dialog will be displayed. You will notice how the login dialog indicates the Facebook app that you have earlier created. Using the application ID to the Facebook object, the user will already be informed that the Test Android Integration application starts the Facebook login. At this point, the user can already decide if he really wants to login to his Facebook account.

The moment the user login he will be presented with another dialog. You have to remember that even if the user has already logged in, he still has not given his permissions to the application to post messages on his wall. A dialog will pop requesting the user for permissions. It allows the user to allow the Test Android Integration application to get basic information and post to your wall. Getting basic information is the default permission given to the user by using this method to login. Additional permission is requested to post on the wall. To be able to post something on the wall the publish_stream permission is needed, hence it is forwarded to the Facebook Connect. When the user gives his permission, the Facebook API can now start doing the requests (like posting on the wall) in behalf of the user.

To post messages on the wall, all you need to do is to construct or make a message and pass it to a Facebook request method. You can use the “me/feed”ID on the Facebook Graph API to point out that you will post messages on the status field.

The Post Method on The Wall is being called from the main activity in the background thread. A notification will be sent to the user that his message has been posted. The handler shows the message on the main UI thread after the background processing has been completed.

Another important thing to remember is that when the user hits the Post Message button, the message that you have made will be sent immediately to the Facebook Wall. After logging in you can send a message by adding an Authentication Listener (SessionEvents.AuthListener) to the Sessions Events.

To illustrate the wrapper method, you can check on this code:

·         For logged in user, messages can be posted in the background thread

·         The unknown user should wait for the authentication/ authorization to be successful before you can continue posting the message in the background thread.

The Clear Credentials Method takes care of the logging off. All credentials or data are cleared from the system and each interaction with Facebook will cause you to login again.

The Android Facebook SDK offers an easy and quick way to integrate Facebook into the Android application. It also offers single sign on functionality which spares the user to login again if the session was already recognized by Facebook. Another important thing to note is that the OAuth 2 implements that Facebook will allow a simpler authentication/authorization mechanism as compared to the traditional OAuth based providers. After setting up test account where you can do your testing activities, you can already use your Facebook application right then and there. Though this article is useful only for Facebook OAuth 2.0.

If you face any troubles in development you can also hire android app developer and Iphone app developer from Hyperlink Infosystem company to assist you.

Get Free Quote Now !