8

I have a Facebook login option in my Android app and I am trying to get the user's email unsuccessfully. After the user is logged in, and added to the Parse.com User table (with a valid access token), I request the Facebook user info, but only get it's id and name (even though the email is set as one of the permissions).

Here's my code (inside OnClickListener of my button):

ParseFacebookUtils.logInWithReadPermissionsInBackground(
    getActivity(), Arrays.asList("email", "user_friends"), new LogInCallback() {
        @Override
        public void done(ParseUser user, ParseException err) {
            //err is null

            if (user == null) {
                Log.d(TAG, "Uh oh. The user cancelled the Facebook login.");
            } else {
                GraphRequest.newMeRequest(
                    AccessToken.getCurrentAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject user, GraphResponse response) {

                            Log.d(TAG, "user = " + user.toString());
                            //prints -> user = {"name":"my_fb_name","id":"my_fb_id"}
                        }
                    }
                ).executeAsync();                
           }
       });
cordeiro
  • 121
  • 1
  • 12

2 Answers2

27

I guess this is related to a change in the Graph API v2.4 which makes it necessary that you specify every field you want to have returned from the Graph API.

Have a look at my answer at

regarding this

Concerning your actual problem, see

for an example on how to specify the fields for a GraphRequest

GraphRequest request = GraphRequest.newMeRequest(
        accessToken,
        new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(
                   JSONObject object,
                   GraphResponse response) {
                // Application code
            }
        });
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email");
request.setParameters(parameters);
request.executeAsync();
Community
  • 1
  • 1
Tobi
  • 31,405
  • 8
  • 58
  • 90
  • 2
    That solved my problem! It was in fact because of the changes in the Graph API and the missing "email" field. Thank you for your help – cordeiro Aug 26 '15 at 13:54
  • 1
    What the reason of JSONObject object parameter is being null? I have such case – Lester Aug 21 '18 at 10:49
1

Kotlin

Also, avoids the error of Interface GraphJSONObjectCallback does not have constructors, which may pop up if you automatically covert the Java version to Kotlin.

        val request = GraphRequest.newMeRequest(
          token,
          object : GraphRequest.GraphJSONObjectCallback {
            override fun onCompleted(
              obj: JSONObject?,
              response: GraphResponse?
            ) {

              try {
                // Save user email to variable
                email = obj!!.getString("email")
                firstName = obj.getString("first_name")
                lastName = obj.getString("last_name")
                handleFacebookAccessToken(token)
              }
              catch (e: JSONException) {
                Toast.makeText(
                  this@MainActivity,
                  "Facebook Authentication Failed.",
                  Toast.LENGTH_LONG
                ).show()
              }
            }
          })

        val parameters = Bundle()
        parameters.putString("fields", "email,first_name,last_name")
        request.parameters = parameters
        request.executeAsync()
Ben
  • 3,346
  • 6
  • 32
  • 51