-1

I am new for facebook sdk. Does any know how to get firstname, lastname, email, id, birthday, gender, hometown, age and etc from facebook sdk in android platform?

Please show me the code?

below is what I have tried

callbackManager = CallbackManager.Factory.create();
    LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
    loginButton.setReadPermissions( "public_profile", "email", "user_birthday", "user_friends");


loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = AccessToken.getCurrentAccessToken();
            boolean isLoggedIn = accessToken != null && !accessToken.isExpired();

            GraphRequest request = GraphRequest.newMeRequest(
                    accessToken,
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            // Insert your code here
                            try {
                                if (object != null) {
                                    JSONObject obj = response.getJSONObject();
                                     id = obj.getString("id");
                                     name = obj.getString("name");
                                     birthday = obj.getString("birthday");
                                     email = obj.getString("email");
                                    String gender = obj.getString("gender");
                                     showUserInfoToast();

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

                        }
                    });

            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,email,first_name,last_name,gender");
            request.setParameters(parameters);
            request.executeAsync();


        }

        @Override
        public void onCancel() {
            // App code
        }

        @Override
        public void onError(FacebookException exception) {
            // App code
        }
    });

I tried but it does not work.... please help

freeman
  • 92
  • 11

3 Answers3

1

Please try this code .Hope it will fetch all the data you needed.Thanx

 public void onSuccess(final LoginResult loginResult) {
    Log.e("bug", "facebook sign in success");

    if (loginResult != null) {

        Profile profile = Profile.getCurrentProfile();

        if (profile != null) {
            firstName = profile.getFirstName();
            lastName = profile.getLastName();
            social_id = profile.getId();
            profileURL = String.valueOf(profile.getProfilePictureUri(200, 200));
            Log.e(TAG, "social Id: " + profileURL);
        }

        GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject object, GraphResponse response) {
                Log.e("bug", "facebook social_id=>" + social_id + " toeken" + loginResult.getAccessToken().getToken());
                Log.e("bug", "graph response=>" + object.toString());
                try {
                    if (object.has("name")) {
                        String[] name = object.getString("name").split(" ");
                        firstName = name[0];
                        lastName = name[1];
                    }
                    email = object.has("email") ? object.getString("email") : "";
                    social_id = object.has("id") ? object.getString("id") : "";
                    socialSignUp("facebook");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,first_name,name,last_name,email,gender,birthday");
        request.setParameters(parameters);
        request.executeAsync();
    }
Kartik Shah
  • 866
  • 5
  • 19
0

The data you access depends on the permissions someone grants your app and data they chose to share with apps (Link)

These fields can be null

Liar
  • 1,235
  • 1
  • 9
  • 19
0

To get any user-specific data other than userId, nameand email you have to submit your app for review to facebook console so that facebook will have clarity that what you want to do with user personal data.

If it is some sort of useful thing your app is doing with this data then facebook will allow you to access that data like birthday date, user_friends and user gender. To do that go to facebook developer console and follow there procedure. facebook flow

Hope this will help you to achieve what you want to achieve. See this

Nouman Ch
  • 4,023
  • 4
  • 29
  • 42