TABLE OF CONTENTS
What is Facebook Integration?
The Facebook SDK for Android enables people to sign in to your app with Facebook Login. When people log into your app with Facebook, they can grant permissions to your app so you can retrieve information or perform actions on Facebook on their behalf.
In this blog, we will learn about how to integrate Facebook login feature in Android application. In some situations, we will need the app user to login to the android app for further process. Generally, Facebook Login and Google Login are used in most of the Applications.
Steps to Integrate Facebook in Android Application:
- First, create a new project in the android studio and name it as “MySocialapp”.
- Now, go to the Facebook Developer console and create a new App. Follow the below link for login and create a new app. https://developers.facebook.com/
You will find this screen
Now click on Log In and Log In with your Developer account of Facebook.
After successfully Logging In, you will find this below screen where you have to click on My Apps from the menu bar and click on Create App or Go to Docs>> Facebook Login>>Android. Select the newly created app from the ‘Select an App’ section in the document.
Then Create A New App ID
Give a proper name, Display Name and Your Email-id.
Now after clicking on Android, you will find this below screen
Find your newly created app and select it from the Dropdown list.
The next step is adding the Facebook SDK to Android Studio Project. Open MySocialapp >> Gradle Scripts >> build.Gradle (Project) and add these libraries in buildscript>>repositories.
In your project open MySocialapp>>Gradle Scripts>> build.Gradle (Module App) and add the following implementation statements to the dependencies section.
For this application, we need a circle image view to display the Facebook profile pic. An image loading library to load an image into Image view from URL.
In this project, we use the Glide image loading library and a third party Circle Image View library. Add the dependency for using them as given below. Here I have used ready GitHub libraries.
From the Facebook Login Configuration page, copy the facebook_app_id and fb_login_protocol_scheme string resources. Add it to the Strings.xml file in the android studio project.
Open string.xml
-
Open the AndroidManifest.Xml file and add the following statements in it.
Internet Permission.
Meta-data element that mansion your app Id.
Facebook Activity and intent filter for Chrome Custom Tabs.
Associate Your Package Name and Default Class with Your App.
- Provide development and Release Key hashes for your App. For generating the key hash you need a key tool that is available with java SDK, your release or debugs key-store and in windows platform, you need the OpenSSL library.
- Download OpenSSL – for – windows from Google code Archive. You will find this below mentioned screen. I have downloaded OpenSSL-0.9.8e_X64.zip.
Extract the folder into C Drive and copy the path of that folder.
C:\openssl
Now, find your Java SDK path. In my PC this is a path for Java bin folder.
C:\Program Files\Java\jdk1.8.0_144\bin
Now, find debug. keystore path from your This PC and copy it.
In Users>>User_Name>>.android>>debug.keystore, Here this is the path in my PC.
C:\Users\HINAL\.android
Now Go to “C:\Program Files\Java\jdk1.8.0_144\bin” bin folder and start the command prompt.
Search for CMD
Copy path from Step 6 on developers.facebook.com
(keytool -exportcert -alias androiddebugkey-Keystore (Your Keystore path)\debug.keystore | (Your OpenSSL path)\openssl sha1 -binary | (Your OpenSSL path)\openssl base64)
(Your Keystore path) in place of this add your debug.keystore path.
(Your OpenSSL path) in place of this add your OpenSSL path.
After adding all paths my path will be:
keytool -exportcert -alias androiddebugkey -Keystore “C:\Users\HINAL\.android\debug.keystore” | “C:\openssl\bin\openssl” sha1 -binary | “C:\openssl\bin\openssl” base64
Add this path into a command prompt and get hash key.
Add the hash key into step 6 and save it.
Now, Enable Single Sign-On for your App.
-
Add this code to your activity_main.xml file
I have added static values to show this Component. Now the screen will look like this.
-
Add this code to your MainActivity.java file.
- Request email and public profile permissions on Facebook Login Button.
- For handle login response, create a CallbackManager object by using Factory.create
- To respond to the login result we register a callback with LoginButton.
- If the login succeeds, the LoginResult parameter of the onSuccess() callback method has a new AccessToken and the most recent granted and denied permissions.
- In this example for getting a login response, we are not going to use the callback methods with LoginButton, instead, we use the AccessTokenTracker class.
- Override the onActivityResult method to pass the login result to the LoginManager using the callback manager.
- For checking Login status when app load, call getCurrentAccessToken() method using AccessToken class and if the method return null means user logged out.
- For read user information from AccessToken, we use Graph API. Facebook uses a special API called Graph API to read and write information on the Facebook platform. Graph API brings the result in the form of a JSON object or JSON array.
Output:
Login Screen:
Logged In Screen:
Log Out Screen:
Logged Out Screen:
Conclusion
With the end of this tutorial, we hope we were able to fulfill our purpose of helping you to integrate Facebook login feature in an Android application. We tried to explain the whole process with the help of screenshots with proper descriptions. If you follow the above steps without any error, you will be able to integrate Facebook login into your Android application successfully.
If you still have any doubts, don’t worry. You can always get in touch with us for any kind of query.