1

I am using the Facebook SDK to allow users to login using Facbook, but I am unable to fetch profile details after a successful login.

Here is my code:

fbLoginButton = (LoginButton) findViewById(R.id.login_button);
    fbLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Profile profile = Profile.getCurrentProfile();

            id = profile.getId();
            String user_fname = profile.getFirstName();
            String user_lname = profile.getLastName();
            user = user_fname.concat(" ");
            user = user.concat(user_lname);
            image = profile.getProfilePictureUri(10, 10).toString();

            try {
                send();
            }catch (Exception ex) {
                String error = ex.getMessage();
            }
        }

It gives an error that profile obj is null

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Rick Roy
  • 1,656
  • 2
  • 21
  • 47

3 Answers3

3

Use GraphRequest to get Profile Information.

@Override
public void onSuccess(LoginResult loginResult) {

    AccessToken accessToken = loginResult.getAccessToken();
    GraphRequest request = GraphRequest.newMeRequest(accessToken,
            new GraphRequest.GraphJSONObjectCallback() {
                String email;
                String firstName;
                String lastName;
                String gender;

                String country;
                String dob, name, picPath, id;

                @Override
                public void onCompleted(JSONObject object,
                        GraphResponse response) {
                    try {

                        email = object.getString("email");
                        name = object.getString("name");
                        firstName = object.getString("first_name");
                        lastName = object.getString("last_name");
                        gender = object.getString("gender");
                        country = object.getString("locale");
                        id = object.getString("id");
                        picPath = "http://graph.facebook.com/" + id + "/picture?type=large";

                        dob = object.getString("birthday");

                    } catch (JSONException e) {
                         e.printStackTrace();
                    }

                }
            });
    Bundle parameters = new Bundle();
    parameters
            .putString("fields",
                    "name,email,address,first_name,last_name,gender,location,birthday,locale");

    request.setParameters(parameters);
    request.executeAsync();

}
Emil
  • 2,786
  • 20
  • 24
0

Check the below code and try to implement this for retrieving information from facebook when logging in. Also dont forget to add the permissions for information that you require.

private Session.StatusCallback statusCallback = new Session.StatusCallback() {
        @Override
        public void call(final Session session, SessionState state,
                         Exception exception) {
            if (state.isOpened()) {
                Request mRequest = Request.newMeRequest(session, new Request.GraphUserCallback() {
                    @Override
                    public void onCompleted(GraphUser graphUser, Response response) {
                        if (response.getError() == null) {
                            String id= graphUser.getId()));
                            String name= graphUser.getName()));
                            Strign profilePic= "http://graph.facebook.com/" + graphUser.getId() + "/picture"));
                        } else {

                        }
                    }
                });
                RequestAsyncTask asyncTask=mRequest.executeAsync();
            } else if (state.isClosed()) {
            }
        }
    };

here

learner
  • 3,092
  • 2
  • 21
  • 33
Jarvis
  • 503
  • 2
  • 5
  • 17
0
public void RequestData(){
        GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject object,GraphResponse response) {

                JSONObject json = response.getJSONObject();
                try {
                    if(json != null){
                        String text = "<b>Name :</b> "+json.getString("name")+"<br><br><b>Email :</b> "+json.getString("email")+"<br><br><b>Profile link :</b> "+json.getString("link");
                        details_txt.setText(Html.fromHtml(text));
                        profile.setProfileId(json.getString("id"));
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,link,email,picture");
        request.setParameters(parameters);
        request.executeAsync();
    }
Millu
  • 45
  • 1
  • 9