How to integrate facebook login with android app

 

In this article, I am going to explain how to integrate facebook with Android application, the first thing which you need to do is generate a signature for your android app and register it into the facebook application setting’s

Generate signature

To generate a signature you need to have keytool and openssl, you can find keytool in your local JDK “C:\Program Files\Java\jdk1.7.0_17\jre\bin” folder and if you are not already installed openssl on your local machine then you can download it using this openssl download link and setup openssl path in environment variables

Next, go to the command prompt of jdk bin folder and run the below command, the below command generates development keyhash for you, but you need to generate release key if you are planning to upload into playstore

keyhash

You are successfully generated keyhash, now what you can do is copy and paste keyhash in a notepad for later use

Create Facebook Application

Once the signature is generated then we need to create a facebook application by going to Facebook Developers dashboard for this you need to login to facebook developers dashboard

Once you logged into facebook developers dashboard, on left side you will see create new app link, use this link to create new facebook application then fill up below fields, which appears on new facebook application id dialog

facebookintegration

Once the facebook application is created then the next step is go to the settings of recently created your facebook application, refer below screen shot

facebookintegrationone

Then the next step is at bottom you will see “Add Platform” link, click on this and select “Android Platform” then fill up below fields

  • Google Play Package Name is your android app package name
  • Class Name is your activity which you want facebook to launch
  • The important one is Key Hashes, what ever keyhash you generated using keytool you need to paste it here

facebookintegrationtwo

Creating Android Facebook App

I am going to use “Android Studio 2.2”  and Facebook Android SDK 4.14 APIs to develop a facebook mobile application, the first thing I am going to do is create a project using Android Studio 2.2 by going to File ->  New -> New Project and configure all mandatory fields, then go to your project and configure settings to import and connect to facebook

Facebook Dependencies

Add facebook android sdk in the dependencies section of build.gradle file along with the mavenCentral() repositories

 Update AndroidManifest.xml file

Open androidmanifest.xml file and add below entries

In the string.xml file add below entry

Update activity_main.xml file

MainActiviy.java

Once the user click on facebook login button, the facebookcallback method executes and onsuccess method we are making a call to GraphRequest to fetch logged in user email address and gender

The GraphRequest returns JSONObject, so we need to use getString() method to read email address and pass this email as a bundle argument to the profile activity

Profile Activity

On create method of profile activity read user values from the bundle and assign to the text field’s

activity_profile.xml

deviceone device-2016-12-28-111325 device-2016-12-27-224248

Download Code

 

Leave a Reply

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