How i can fetch all of user profile information from facebook (like first name, last name, email etc.)
I have downloaded the FB SDK but there is no example for getting t
Here is a quickest way that worked for me
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Toast;
import com.facebook.Request;
import com.facebook.Request.GraphUserCallback;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.Session.StatusCallback;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;
import com.x.y.android.R;
public class FBConnect extends FragmentActivity {
private static final String TAG = "FacebookConnect";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.facebook_connect);
if (Session.getActiveSession() == null
|| Session.getActiveSession().isClosed()) {
Session.openActiveSession(this, true, new StatusCallback() {
@Override
public void call(Session session, SessionState state,
Exception exception) {
System.out.println("State= " + state);
if (session.isOpened()) {
System.out.println("Token=" + session.getAccessToken());
Request.executeMeRequestAsync(session,
new GraphUserCallback() {
@Override
public void onCompleted(GraphUser user,
Response response) {
if (user != null) {
System.out.println("User=" + user);
}
if (response != null) {
System.out.println("Response="
+ response);
Toast.makeText(FBConnect.this,
response.toString(),
Toast.LENGTH_LONG).show();
}
}
});
}
if (exception != null) {
System.out.println("Some thing bad happened!");
exception.printStackTrace();
}
}
});
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
}
}
// Make an API call to get user data and define a
// new callback to handle the response.
Request request = Request.newMeRequest(session,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
// If the response is successful
if (session == Session.getActiveSession()) {
if (user != null) {
// Set the id for the ProfilePictureView
// view that in turn displays the profile picture.
profilePictureView.setProfileId(user.getId());
// Set the Textview's text to the user's name.
userNameView.setText(user.getName());
}
}
if (response.getError() != null) {
// Handle errors, will do so later.
}
}
});
request.executeAsync();
This just gets the user data! You can find all corresponding details according to the 1st answer which links to a FB doc which has all the related fields that can be retrieved!
There's an example in the folder facebook-android-sdk/examples/simple. It shows how to make an async request for the basic data of the user. You'll be able to find these data.
Cheers
First You create your application and get application id then pass this id to your code like this. Initialize globally
public static final String mAPP_ID = "Your APP ID";
Facebook mFacebook= new Facebook(mAPP_ID);
and setOnClickListener on your button in On Create()
// facebook login button click event
try{
//mFacebook.logout(LoginActivity.this);
((Button)findViewById(R.id.loginPageFaceBookButton)).setOnClickListener(loginButtonListener);
SessionStore.restore(mFacebook,LoginPage.this);
} catch (Exception e) {
Toast.makeText( LoginPage.this,"Exception"+e.toString(), Toast.LENGTH_SHORT).show();
}
// loginButtonListener
//----------------------------------------------
private OnClickListener loginButtonListener = new OnClickListener()
{
public void onClick( View v )
{
if(!mFacebook.isSessionValid() )
{
mFacebook.authorize(LoginPage.this, new String[] {"publish_stream","email","user_groups","read_stream","user_about_me","offline_access"},Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
}
else
{
try
{
JSONObject json = Util.parseJson(mFacebook.request("me"));
facebookID = json.getString("id");
facebookEmail = json.getString("email");
faceBooklastName=json.getString("last_name");
faceBookFirstName=json.getString("first_name");
}
catch (Exception e)
{
// TODO: handle exception
//Toast.makeText( LoginActivity.this,"Exception FB "+e.toString(), Toast.LENGTH_SHORT).show();
}
catch( FacebookError error )
{
Toast.makeText( LoginPage.this,error.toString(), Toast.LENGTH_SHORT).show();
}
}
}
};
//onActivityResult
//***********************************************************
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
// DialogListener CLASS STATRT HERE.
public final class LoginDialogListener implements DialogListener
{
public void onComplete(Bundle values)
{
try
{
JSONObject json = Util.parseJson(mFacebook.request("me"));
facebookID = json.getString("id");
facebookEmail = json.getString("email");
SessionStore.save(mFacebook, LoginPage.this); Toast.makeText( LoginPage.this,"facebookID :"+facebookID+" \n "+"facebookEmail : "+facebookEmail, Toast.LENGTH_SHORT).show();
}
catch( Exception error )
{
Toast.makeText( LoginPage.this, error.toString(), Toast.LENGTH_SHORT).show();
}
catch( FacebookError error )
{
Toast.makeText( LoginPage.this, error.toString(), Toast.LENGTH_SHORT).show();
}
}
public void onFacebookError(FacebookError error) {
Toast.makeText( LoginPage.this, "Something went wrong. Please try again.1"+error.toString(), Toast.LENGTH_LONG).show();
}
public void onError(DialogError error) {
Toast.makeText( LoginPage.this, "Something went wrong. Please try again.2"+error.toString(), Toast.LENGTH_LONG).show();
}
public void onCancel() {
Toast.makeText( LoginPage.this, "Something went wrong. Please try again.3", Toast.LENGTH_LONG).show();
}
/****** Facebook Login End *******/
}