I used facebook sdk 4.10.0 to integrate login into my android app. Next tutorial:
Facebook integration of Android studio.
You can get first name, last name, email address, gender, facebook id and date of birth from facebbok.
The above guide also explains how to create an application in the facebook developer console via video.
Gradle.build
apply plugin: 'com.android.application'
android {compileSdkVersion 23 buildToolsVersion "23.0.3"
defaultConfig { applicationId "com.demonuts.fblogin" minSdkVersion 16 targetSdkVersion 22 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } repositories { mavenCentral() }
}
dependencies {compile fileTree (dir: 'libs', include: [' * .jar ']) testCompile' junit: junit: 4.12 'compile' com.android.support:appcompat-v7:23.4.0 'compile' com.facebook .android: facebook-android-sdk: 4.10.0 'compile' com.github.androidquery: androidquery: 0.26.9 '}
Source code for activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.demonuts.fblogin.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000" android:layout_marginLeft="10dp" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/text"/> <ImageView android:layout_width="300dp" android:layout_height="300dp" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:id="@+id/ivpic" android:src="@mipmap/ic_launcher"/> <com.facebook.login.widget.LoginButton android:id="@+id/btnfb" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
Code for MainActivity.java
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.ImageView; import android.widget.TextView; import com.androidquery.AQuery; import com.facebook.AccessToken; import com.facebook.AccessTokenTracker; 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.Profile; import com.facebook.ProfileTracker; import com.facebook.login.LoginResult; import com.facebook.login.widget.LoginButton; import org.json.JSONException; import org.json.JSONObject; import java.util.Arrays; public class MainActivity extends AppCompatActivity { private AQuery aQuery; private ImageView ivpic; private TextView tvdetails; private CallbackManager callbackManager; private AccessTokenTracker accessTokenTracker; private ProfileTracker profileTracker; private LoginButton loginButton; private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { GraphRequest request = GraphRequest.newMeRequest( loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted(JSONObject object, GraphResponse response) { Log.v("LoginActivity", response.toString()); // Application code try { Log.d("tttttt",object.getString("id")); String birthday=""; if(object.has("birthday")){ birthday = object.getString("birthday"); // 01/31/1980 format } String fnm = object.getString("first_name"); String lnm = object.getString("last_name"); String mail = object.getString("email"); String gender = object.getString("gender"); String fid = object.getString("id"); tvdetails.setText("Name: "+fnm+" "+lnm+" \n"+"Email: "+mail+" \n"+"Gender: "+gender+" \n"+"ID: "+fid+" \n"+"Birth Date: "+birthday); aQuery.id(ivpic).image("https://graph.facebook.com/" + fid + "/picture?type=large"); //https://graph.facebook.com/143990709444026/picture?type=large Log.d("aswwww","https://graph.facebook.com/"+fid+"/picture?type=large"); } catch (JSONException e) { e.printStackTrace(); } } }); Bundle parameters = new Bundle(); parameters.putString("fields", "id, first_name, last_name, email, gender, birthday, location"); request.setParameters(parameters); request.executeAsync(); } @Override public void onCancel() { } @Override public void onError(FacebookException error) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FacebookSdk.sdkInitialize(this); setContentView(R.layout.activity_main); tvdetails = (TextView) findViewById(R.id.text); ivpic = (ImageView) findViewById(R.id.ivpic); loginButton = (LoginButton) findViewById(R.id.btnfb); aQuery = new AQuery(this); callbackManager = CallbackManager.Factory.create(); accessTokenTracker= new AccessTokenTracker() { @Override protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) { } }; profileTracker = new ProfileTracker() { @Override protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) { } }; accessTokenTracker.startTracking(); profileTracker.startTracking(); loginButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday", "user_friends")); loginButton.registerCallback(callbackManager, callback); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); callbackManager.onActivityResult(requestCode, resultCode, data); } @Override public void onStop() { super.onStop(); accessTokenTracker.stopTracking(); profileTracker.stopTracking(); } @Override public void onResume() { super.onResume(); Profile profile = Profile.getCurrentProfile(); } }