Skip to main content

Facebook Login in Android App





Facebook Login in Android App





Step#1 : Add this line to your project’s build.gradle file in dependencies section

compile ‘com.facebook.android:facebook-android-sdk:4.+’

Step#2 : Now generate your Key Hash for Debug and Release builds

Open Android Terminal :
 
For Debug Build(Go to your debug.keystore path in the terminal and then run this command):
 
keytool -exportcert -alias androiddebugkey -keystore debug.keystore | openssl sha1 -binary | openssl base64
For Release Build:
keytool -exportcert -alias <aliasName> -keystore <keystoreFilePath> | openssl sha1 -binary | openssl base64
NOTE : Make sure In Both case it must ask for password. If it does not asks for password that means something is wrong in command. Default password is android.

Step#3 : Add this code to your AndroidManifest.xml file

<activity
 android:name=”com.facebook.FacebookActivity” android:configChanges=”keyboard|keyboardHidden|screenLayout|screenSize|orientation”
 android:label=”@string/app_name” />
<provider
android:name=”com.facebook.FacebookContentProvider”
android:authorities=”com.facebook.app.FacebookContentProvider1234567890xxxx"
android:exported=”true” />
<meta-data
 android:name=”com.facebook.sdk.ApplicationId”
 android:value=”@string/facebook_app_id” />
Note: facebook_app_id we’ll be getting in our next steps.

Step#4 : Now go to your Facebook Account and get logged in

then open this link : https://developers.facebook.com/apps/

Step#5 : Now, Create a New App ID and hit Submit.





Step#6 : Copy that APP ID and paste it in the Step-3 code




Step#7 : Now Go to Settings from Left-Hand Side Menu and Click ‘+Add Platform’ button and choose Android





Note:
— Enter Package-Name of your App
— Enter Activity from where you want to Launch Facebook
— Enter Key Hashes generated in Step 2
— Turn ‘Single Sign On’ to Yes

Step#8 : Now Go to App Review from clicking Left-Hand Side Menu and ‘Make your App Public’ switch to ‘Yes’ and Choose the App Category and hit Submit





Step#9 : Now come all over to your Android Project and place this code in your desired XML file

        <FrameLayout
            android:id="@+id/FrameLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_10sdp">

            <com.facebook.login.widget.LoginButton
                android:id="@+id/login_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone" />

            <com.example.tasol.myrowitems.KudosButton
                android:id="@+id/fb"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#416BC1"
                android:drawableLeft="@drawable/indo_facebook"
                android:paddingBottom="@dimen/_10sdp"
                android:paddingLeft="@dimen/_10sdp"
                android:paddingTop="@dimen/_10sdp"
                android:text="Log in with Facebook"
                android:textAllCaps="false"
                android:textColor="#ffffff"
                android:textStyle="bold" />
        </FrameLayout>
Note: This is a trick layout to use Facebook Default Login Button for customization, you can use default LoginButton if you want to implement it normally.

Step#10 : Now go to your respective java file and add this bunch of code on the button click

private LoginButton txtFbLogin;
private AccessToken mAccessToken;
private CallbackManager callbackManager;
txtFbLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
 @Override
 public void onSuccess(LoginResult loginResult) {
       mAccessToken = loginResult.getAccessToken();
       getUserProfile(mAccessToken);
 }
@Override
 public void onCancel() {
     
 }
@Override
 public void onError(FacebookException error) {
 
 }
});
private void getUserProfile(AccessToken currentAccessToken) {
 GraphRequest request = GraphRequest.newMeRequest(
 currentAccessToken,
 new GraphRequest.GraphJSONObjectCallback() {
 @Override
 public void onCompleted(JSONObject object, GraphResponse  response) {
 try {
        //You can fetch user info like this…
       //object.getJSONObject(“picture”).
         getJSONObject(“data”).getString(“url”);
        //object.getString(“name”);
        //object.getString(“email”));
        //object.getString(“id”));
 } catch (JSONException e) {
     e.printStackTrace();
 }
}
 });
 Bundle parameters = new Bundle();
 parameters.putString(“fields”, “id,name,email,picture.width(200)”);
 request.setParameters(parameters);
 request.executeAsync();
 }
@Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      callbackManager.onActivityResult(requestCode, resultCode,  data);
 }
Note : Use this line to LogOut from current facebook login :
 LoginManager.getInstance().logOut();

This is it from my side and now you can do whatever you want to implement of your own with whatever facilities you wanna give your users using this Facebook Login.

Please Like, Share and Recommend with your buddies to help them out with this easy yet tricky process.

Kudosss To Everyone on Facebook

Comments

Popular posts from this blog

Let’s keep it short….again

Let’s keep it short….again The world is full of people who are full of Fear of Failure which doesn’t allow them to move steadily and think positively. But many of them don’t know that Fear is a Big Liar. Because Fear shows you fake reality about your own self that — ”You can’t do this thing”. Instead people should fake their fear by repeatedly saying this — ”At least I can try!!!” Because there is no sense at all to not do the particular thing just because you have never done it earlier. Try Again…Fail Again…Fail Better!!!😎 Kudossss To Fearless You…

Grateful 24/7/365

Grateful 24/7/365 The super basic problem with most of us is that we are not  grateful  for the life we all are living in currently. And the super duper solution for all of our this problem is to be  grateful enough for the life that we all got. Now to do that we can follow this simple phenomenon called -  Attitude of Gratitude. Many of us don’t even know How to be Grateful? So the best answer for this is: Becoming grateful and thankful for the smallest of things in your life is the best way to achieve the  Attitude of Gratitude. What to do with so much of Attitude of Gratitude? Many people even ask this silly but interesting question - that what we will get in return or what is the outcome of showing so much  gratefulness . So the best and interesting answer for this is: Keep yourself busy and focussed on your  attitude of gratitude , which will eventually make way for disappointments ...

That Winning Moment!!!

That Winning Moment!!! You are just a thought away from the fact that the penultimate thing we all want in our career is that winning moment and the motivation to carry that winning momentum all through outside and inside our career span. Let’s take a sneak peek of what can be some of the greatest winning moments. * That Bird who assembles the Nest: Have you ever thought about that little bird who is continuously collecting those tiny little wooden sticks to assemble or make home for her young ones to keep there and feed there. How great that  moment  will be for that bird, when she place that final wood stick on her assembled nest to finish that small yet hardworking activity. The bird can die for that type of winning moment!!! * That college boy who got placed in an interview: Everyone of you have definitely been through that phase of your graduation or school-life where you got selected for sitting in ...