0

Hello i'm trying to get basic user profile information such as firstname and lastname from facebook. I am using andriod as my client device. but i am having difficulty retrieving user's information. I have already created my appId and also added the activity of facebook in my manifest. The user object is just a model that contains the users first and last name. i intend to display the information on another activity page. Please could someone tell me what i did wrong below is my code:

public class MainActivity extends AppCompatActivity {

    private CallbackManager callbackManager;
    private LoginButton loginButton;

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

        //initialize facebook sdk
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        FaceBookSetup();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void FaceBookSetup() {
        callbackManager = CallbackManager.Factory.create();
        loginButton = (LoginButton)findViewById(R.id.login_button);

        LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("user_first_name", "user_last_name"));

        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {

                Profile profile = Profile.getCurrentProfile();
                User user = new User();
                user.setFirstName(profile.getFirstName());
                user.setLastName(profile.getLastName());

                Log.d("First Name: ", user.getFirstName());

                GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
                        try {
                           /* User user = new User();
                            user.setFirstName(jsonObject.getString("first_name"));
                            user.setLastName(jsonObject.getString("last_name"));
                            Log.d("VISUAL: ", jsonObject.getString("first_name"));*/


                        }
                        catch(JSONException ex) {
                            ex.printStackTrace();
                        }
                    }
                });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "first_name,last_name");
                graphRequest.setParameters(parameters);
                graphRequest.executeAsync();
            }

            @Override
            public void onCancel() {
            }
            @Override
            public void onError(FacebookException e) {
            }
        });

    }
//I also intend to pass users last and first name to another activity.
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, requestCode, data);
    }

}

my activity.xml:

<com.facebook.login.widget.LoginButton
        android:id="@+id/login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="148dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        />

1 Answers1

0

There is no such permission as "user_first_name" and "user_last_name". Check the list of permissions available.

I would recommend changing this line of code

LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("user_first_name", "user_last_name"));

to this :

LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile"));

Change this line of code :

callbackManager.onActivityResult(requestCode, requestCode, data);

to this :

callbackManager.onActivityResult(requestCode, resultCode, data);
vaibhav
  • 816
  • 9
  • 13