In this tutorial we will learn how to implement facebook login in android application.The main idea behind using google and facebook login in mobile and web application is for making the signin process easier for users. We can uniquely identify a user only if he is having a validated email id or mobile number. Using the help of Facebook SignIn we can get the validated email by facebook and some extra information about user in a single button click.
Steps
- Create an android studio project.
- Create and configure an application on facebook developers.
- Add key hash to facebook app.
- Implementing android code
Step 1
The first step of this tutorial is to create an android application. If you are a beginner in android please have a look Android beginners app development guide which will help you to learn how to create a project in android studio.
Step 2
Create and configure an application on facebook developers
- Go to developers.facebook.com
- Follow the below instructions
Click on MyApps Link on the top right corner and you will get a drop down . From the click on Add a new app. It will take you to the following screen.
Fill the app name and email and click on Create App ID button which will take you to the following screen.
Here click on Get started button of Facebook Login and you will get the following screen which you should select Android.
Step 3
Add key hash to facebook App
After Clicking Android ,Facebook will take you to a screen which you need to provide more details about your application.
First you need to go to Tell us about your project section, and fill your package name and your activity package. After saving that it will tell you to add your development and release key hashes.
For creating a hash key you need to execute the following command in your command prompt window.
keytool -exportcert -alias androiddebugkey -keystore "C:\Users\admin\.android\debug.keystore" | "D:\openssl\bin\openssl" sha1 -binary |"D:\openssl\bin\openssl" base64
It will give you a hash key and you can add it at facebook and click save button.
Finally just enable the single signin on next step.
Step 4
Implementing android code
Now we are done with configuring app at developers and its time to do some coding in android end. Let us start by compiling facebook sdk in apps build.gradle file.
Add the following code above dependencies in gradle file
repositories { mavenCentral() }
Now add the following dependency
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
Next step is to add the following strings to your strings.xml file under res/values/
<string name="facebook_app_id">Replace this with your id</string> <string name="fb_login_protocol_scheme">Replace this with your protocol scheem</string
You can find both of these values for your application on developers.
Now Add the following permission in your AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
and the following code also under the application tag
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id" /> <activity android:name="com.facebook.FacebookActivity" android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation" android:label="@string/app_name" /> <activity android:name="com.facebook.CustomTabActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="@string/fb_login_protocol_scheme" /> </intent-filter> </activity>
Next you need to add the button to your layout using the below code.
<com.facebook.login.widget.LoginButton android:id="@+id/fb_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="15dp" android:paddingTop="15dp" android:text="Login " />
Now Simply Replace the following code in your Activity file.
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; import com.facebook.CallbackManager; import com.facebook.FacebookCallback; import com.facebook.FacebookException; import com.facebook.FacebookSdk; import com.facebook.GraphRequest; import com.facebook.GraphResponse; import com.facebook.appevents.AppEventsLogger; import com.facebook.login.LoginManager; import com.facebook.login.LoginResult; import com.facebook.login.widget.LoginButton; import org.json.JSONObject; public class MainActivity extends AppCompatActivity { private CallbackManager callbackManager; private LoginButton fb_login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FacebookSdk.sdkInitialize(getApplicationContext()); AppEventsLogger.activateApp(this); callbackManager = CallbackManager.Factory.create(); setContentView(R.layout.activity_main); fb_login = (LoginButton) findViewById(R.id.fb_login); fb_login.setReadPermissions("email"); fb_login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { String access_token = loginResult.getAccessToken().toString(); GraphRequest request = GraphRequest.newMeRequest( loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted( JSONObject object, GraphResponse response) { // Application code String email = object.optString("email"); if(email.equals("")){ LoginManager.getInstance().logOut(); Toast.makeText(getApplicationContext(),"You dont have an authorized email",Toast.LENGTH_SHORT).show(); }else{ //Save the email in your remoter server and let the user login } } }); Bundle parameters = new Bundle(); parameters.putString("fields", "id,name,email"); request.setParameters(parameters); request.executeAsync(); } @Override public void onCancel() { Toast.makeText(getApplicationContext(),"canceled",Toast.LENGTH_SHORT).show(); } @Override public void onError(FacebookException exception) { String err = exception.toString(); Toast.makeText(getApplicationContext(),err,Toast.LENGTH_SHORT).show(); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); callbackManager.onActivityResult(requestCode, resultCode, data); } }
In the above code inside register callback method you will get email and few other details which facebook will provide about the user if the login is success. Using email you can authorize user and let him login to your application.
I need to start music player using recycler-view concept and need to view every files and to take a .mp3 files alone in a external storage.
Sir in facebook login, login page appear and on that time it work fine but after few hour or next day if again login using facebook then Massage will appear on top :
This app has no Android key hashes configured .configure your app key hashes at http://developer…so on
Hi sajid,
You need to add key hash to your app at developers. Refer Step 3 of the tutorial which explains how to create key hash where to add it in developers. Once you add that everything will work fine..cheers…