1

I want users to be able to register for my Android app using the Facebook SDK. To get their information, I am performing a GraphRequest with the permissions "email" and "public_profile." According to this page, the user's first and last name should be accessible in the "first_name" and "last_name" fields. However, when I click the button, I get an error saying that there is no value for first_name. The JSON array that is returned is along the lines of:

{"name":"First M. Last","id":"1234567890123456"}

I have not submitted my app for review, because it is not yet complete. Could this have something to do with this problem?

jtmaher2
  • 482
  • 5
  • 19
  • For Facebook, you have to submit a request seeking the permissions to acquire user's data like DOB,Email etc. try google-ing more info on this. – Rushi Ayyappa Nov 18 '16 at 05:48
  • Possible duplicate of [Facebook JS SDK's FB.api('/me') method doesn't return the fields i expect in Graph API v2.4+](http://stackoverflow.com/questions/32584850/facebook-js-sdks-fb-api-me-method-doesnt-return-the-fields-i-expect-in-gra) – CBroe Nov 18 '16 at 10:23

2 Answers2

6

As per official documents your login for accessing public_profile permission should look like :

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

To get Profile Info :

 private void getFbDetails(final AccessToken accessToken) {
        GraphRequest request = GraphRequest.newMeRequest(accessToken,
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(
                            JSONObject object,
                            GraphResponse response) {

                     //   Toast.makeText(Login.this, object.toString(), Toast.LENGTH_LONG).show();
                        Log.v("FB Details", object.toString());
                        if (object != null) {
                            name_fb = object.optString("name");
                            email_fb = object.optString("email");
                        }
                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,first_name, last_name, email,link");
        request.setParameters(parameters);
        request.executeAsync();
    }
Nidhi
  • 777
  • 7
  • 17
2

You can get first name and last name as separate parameter. You can concat first name & last name to pass in api. I am using this method to get the user detail while using Facebook login.

            // initalize the login button
         loginButton = (LoginButton) findViewById(R.id.login_button);
           loginButton.setReadPermissions("public_profile");
           loginButton.setReadPermissions("email");


         loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
           @Override
           public void onSuccess(LoginResult loginResult) {
      raphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {

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

                          // Get facebook data from login
                          try {
                              Bundle bFacebookData = getFacebookData(object);
                              usernamestr = fbEmail;

                          } catch (JSONException e) {
                              e.printStackTrace();
                          }
                      }
                  });
                 Bundle parameters = new Bundle();
               parameters.putString("fields", "id, first_name, last_name, email,gender, birthday, location"); // Parámetros que pedimos a facebook
               request.setParameters(parameters);
               request.executeAsync();
                }
                 @Override
           public void onCancel() {

               Toast.makeText(Login.this, "Login attempt canceled", Toast.LENGTH_SHORT).show();
           }

           @Override
           public void onError(FacebookException e) {
                              Toast.makeText(Login.this, "Login attempt failed", Toast.LENGTH_SHORT).show();
           }
       });

        private Bundle getFacebookData(JSONObject object) throws JSONException {

          Bundle bundle = new Bundle();
          String id = object.getString("id");

          try {
              URL profile_pic = new URL("https://graph.facebook.com/" + id + "/picture?width=200&height=150");

              bundle.putString("profile_pic", profile_pic.toString());

          } catch (MalformedURLException e) {
              e.printStackTrace();
              return null;
          }

          bundle.putString("idFacebook", id);

          fbEmail = object.getString("email");
          fbId = object.getString("id");
          if (object.has("first_name"))
              bundle.putString("first_name", object.getString("first_name"));
          if (object.has("last_name"))
              bundle.putString("last_name", object.getString("last_name"));
          if (object.has("email"))
              bundle.putString("email", object.getString("email"));
          if (object.has("gender"))
              bundle.putString("gender", object.getString("gender"));
          if (object.has("birthday"))
              bundle.putString("birthday", object.getString("birthday"));
          if (object.has("location"))
              bundle.putString("location", object.getJSONObject("location").getString("name"));

          return bundle;
      }
mehul
  • 83
  • 4