Last updated on July 31st, 2016 at 11:13 am
In this post i am going to share your how to integrate a facebook login system in which we get some basic information of user like basic profile, Email, Date of Birth etc. Now before we start creating our Facebook integration in our android application we need to create App in Facebook Developer page, sol let get started.
Create Facebook App
Step 1. Go to Facebook developer site and create new app select android platform
Step 2. Name your app whatever you like
Step 3. Create an App Id for your app
Step 4. Now follow step of setup and then you need to add your application package name
Now when you click on next button popup as you for package use you just click on blue button and continue.
Step 5. Now all setup done.
Step 6. Now last step is to make your app available for all user.
Setup Facebook SDK in your android application
Step 1. Create new Android Project in Android Studio (My Studio Version: 1.3.2 )
Step 2. Now open your build.gradle and add repositories
repositories { mavenCentral() }
And add dependencies in your
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.facebook.android:facebook-android-sdk:4.6.0' }
And Sync your project with Gradle, Once your project is sync your Facebook SDK integration is done.
Integrate Facebook login In your App
Step 1. Open your activity_main.xml and add facebook login button
<com.facebook.login.widget.LoginButton xmlns:facebook="http://schemas.android.com/apk/res-auto" android:id="@+id/login_facebook_button" android:layout_width="34dp" android:layout_height="34dp" android:visibility="gone" facebook:com_facebook_login_text="" facebook:com_facebook_logout_text="" />
Step 2. Now open MainActivity.java and add following code
public class MainActivity extends AppCompatActivity { LoginButton login_facebook_button; CallbackManager callbackManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FacebookSdk.sdkInitialize(getApplicationContext()); callbackManager = CallbackManager.Factory.create(); setContentView(R.layout.activity_main); callbackManager = CallbackManager.Factory.create(); // Social Buttons login_facebook_button = (LoginButton) findViewById(R.id.login_facebook_button); login_facebook_button.setReadPermissions(Arrays.asList("public_profile", "user_friends", "email", "user_birthday")); login_facebook_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { // App code Log.e("onSuccess", "--------" + loginResult.getAccessToken()); Log.e("Token", "--------" + loginResult.getAccessToken().getToken()); Log.e("Permision", "--------" + loginResult.getRecentlyGrantedPermissions()); Log.e("OnGraph", "------------------------"); GraphRequest request = GraphRequest.newMeRequest( loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted( JSONObject object, GraphResponse response) { // Application code Log.e("GraphResponse", "-------------" + response.toString()); } }); Bundle parameters = new Bundle(); parameters.putString("fields", "id,name,link,gender,birthday,email,first_name,last_name,location,locale,timezone"); request.setParameters(parameters); request.executeAsync(); Log.e("Total Friend in List", "----------------------"); new GraphRequest( loginResult.getAccessToken(), "/me/friends", null, HttpMethod.GET, new GraphRequest.Callback() { public void onCompleted(GraphResponse response) { /* handle the result */ Log.e("Friend in List", "-------------" + response.toString()); } } ).executeAsync(); } @Override public void onCancel() { // App code } @Override public void onError(FacebookException exception) { // App code } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); callbackManager.onActivityResult(requestCode, resultCode, data); } }
Now using above code you can get Email, DOB and Basic profile info of user
All done now you can use user info and token in your application.
🙂