Game Changing Mobility

Based on my experience, here is a step by step guide for implementing Facebook login in your app using Facebook SDK v4.0

With the advancement in technology research. New SDK’s are getting periodically released, while older ones get deprecated. This is the sole reason why, inspite of a lot many tutorials and blogs out there, I have decided to write yet another tutorial which serves the same purpose but with a major difference – This is based on my own experience as an Android developer and I’ve provided a detailed and updated description on how to implement FB login while using their latest SDK. Adding Facebook login in your apps is even simpler with the new SDK than with the last although the first time around, it can be hard, thanks to the lack of a proper updated guides/tutorials.

Be advised, this tutorial quite rudimentary and is intended for those who have a hard time following the official Facebook documentation.

Prerequisites:

Before we move further with this tutorial, you need to make sure the following are taken care of:

Facebook App – Configured and linked to your app, with Single Sign On enabled.

The LoginButton class – Which provides a button you can add to your UI (inserting in the xml). It follows the current access token and can log people in and out. This is the easiest way to implement Facebook login and is readily available in the Facebook SDK. The LoginButton class is nothing but a custom implementation of a Button.

LoginManager class – To initiate login without using a UI element.

In this tutorial, I will only be demonstrating the use of the LoginButton Class. As I’ve mentioned above, this is the easiest and most effective way of implementing Facebook login as the LoginButton Class takes care of the login/logout status of the user.

You can customize the properties of Login button and register a callback in your onCreateView()method. Properties you can customize include LoginBehavior, DefaultAudience, ToolTipPopup.Style and permissions on the LoginButton.

Permissions Review:

When implementing Facebook Login, your app can optionally ask the user for permissions on a subset of that his/her data. If your app asks for more than public_profile, email and user_friends, Facebook must review it before you release it. See the Facebook Review Guidelines .for more details.

If your app uses a backend server to populate data and requires a login, Facebook login provides an easy and secure way to verify a user using his Facebook account by use of Access Tokens. For those of you who don’t have a good idea of what the Access token is used for, let me explain; when a user connects with an app using Facebook Login, the app will be able to obtain an “access token” which provides temporary, secure accessto Facebook APIs. An access token is an opaque string that identifies a user, app, or page and can be used by the app to make graph API calls.

Once the access token is received in your app, you can send that over to your server, where the necessary verification can be done and provide login access on successful verification of the user.

Now that we have a clear understanding of how Facebook login can be used in tandem with your backend server, let’s see how we can obtain the Access Token once the user has successfully logged in.

For simplicity, in this tutorial, I will be using the LoginButton inside an activity and will only be using the “public_profile” and “email” permissions for the LoginButton.

Once the LoginButton Widget is added to your Layout XML, build your project if need be and in the Activity where you intend to use Facebook Login,

Step 2:

add the following code to your LoginActivity:

private LoginButton fbLoginButton;

public CallbackManager callbackManager;

public String facebook_accessToken;

The object of the CallbackManager class is used to handle all the results from the login response. We will see how to use this soon. Use a simple string to store the user’s FB Access Token.

The onSuccess method is invoked when the the user successfully logs in using his facebook account. The onCancel method is invoked if the user cancels the facebook login. OnError is invoked if the facebook login fails.

If you are using multiple Social networking SDK’s like twitter, google+ etc, the result from each callback method can be filtered by using the correct requestCode inside a swicth() statement.

Now you can see why using the Facebook login is pretty straight forward. Hope this tutorial was helpful. The facebook SDK provides various permissions and allows access to a user’s facebook profile using the graph API. The graph API is a whole tutorial in itself and I will help you to tackle this in the tutorials to come.