How to get user details using Facebook SDK v4.2 for android

949 views Asked by At

I was able to implement the the facebook login using the code :

public class MainActivity extends ActionBarActivity implements FacebookCallback<LoginResult> {

    //List<String> permissionNeeds=Arrays.asList("user_photos","friends_photos", "email", "user_birthday", "user_friends");


    TextView t;

    String get_id, get_name, get_gender, get_email, get_birthday, get_locale, get_location;


    private CallbackManager mCallbackManager;
    private LoginButton mFbLoginButton;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //init facebook sdk and 
        FacebookSdk.sdkInitialize(getApplicationContext());

        setContentView(R.layout.activity_main);



        //instantiate callbacks manager
        mCallbackManager = CallbackManager.Factory.create();


        mFbLoginButton=(LoginButton)findViewById(R.id.login_button);


        mFbLoginButton.registerCallback(mCallbackManager, this);

        mFbLoginButton.setReadPermissions(Arrays.asList("user_status","email"));




    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        //manage login result
        mCallbackManager.onActivityResult(requestCode, resultCode, data);
    }


    public void onSuccess(LoginResult loginResults) {



        Toast.makeText(getApplicationContext(), "Success ",
                   Toast.LENGTH_LONG).show();


    }
    public void onCancel() {



    }


    public void onError(FacebookException e) {


    }

}

Now how can I get the user details such as name , email ,age ,etc .I have searched the web for a few examples but i was unsuccessful.Where am i supposed to put the code for it ?

1

There are 1 answers

2
Maantje On BEST ANSWER

To do this you can use this.

//add variable names.
private static String uid,email;

public void getuserdata() {
    GraphRequest request = GraphRequest.newMeRequest(
            AccessToken.getCurrentAccessToken(),
            new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(
                        JSONObject object,
                        GraphResponse response) {
                    // Application code
                    Log.v("LoginActivity", response.toString());

                    try {
                        //and fill them here like so.
                        uid = object.getString("id");
                        email = object.getString("email")

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    getGroups();
                }
            });
    Bundle parameters = new Bundle();
    parameters.putString("fields", "id,name,email,gender");
    request.setParameters(parameters);
    request.executeAsync();
}

And to use this just call getuserdata() in your on create for example.