I\'m using the following code to get email and name of the user from facebook
protected void onCreate(Bundle savedInstanceState) {
//Display page
setCon
My working method like :
public void getUserDetailsFromFB(AccessToken accessToken) {
GraphRequest req=GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Toast.makeText(getApplicationContext(),"graph request completed",Toast.LENGTH_SHORT).show();
try{
String email = object.getString("email");
String birthday = object.getString("birthday");
String gender = object.getString("gender");
String name = object.getString("name");
String id = object.getString("id");
String photourl =object.getJSONObject("picture").getJSONObject("data").getString("url");
}catch (JSONException e)
{
Toast.makeText(getApplicationContext(),"graph request error : "+e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday,picture.type(large)");
req.setParameters(parameters);
req.executeAsync();
}
Then in your LoginButton :
loginButton.registerCallback(callbackManager, new FacebookCallback() {
@Override
public void onSuccess(final LoginResult loginResult) {
getUserDetailsFromFB(loginResult.getAccessToken());
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(),"fb user canceled",Toast.LENGTH_SHORT).show();
}
@Override
public void onError(FacebookException e) {
Toast.makeText(getApplicationContext(),"fb error",Toast.LENGTH_SHORT).show();
}
});
Init like :
LoginButton loginButton =(LoginButton)findViewById(R.id.facebook_login_button);