scientiffic
scientiffic

Reputation: 9415

Android Facebook GraphRequest Not Returning Email

I have looked at several posts regarding this issue and have not found a solution.

     facebookLoginButton = (LoginButton) findViewById(R.id.facebook_login_button);
        facebookLoginButton.setReadPermissions(Arrays.asList("read_stream", "user_photos", "email", "user_location"));

// create callback manager for facebook
        callbackManager = CallbackManager.Factory.create();

        facebookLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Log.d(TAG, "LOGGED IN");

                GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {
                        Log.d(TAG, "Facebook graph response: " + response.toString());
                        try {
                            // get only the part before the @ symbol
                            String email_username = object.getString("email").substring(0, object.getString("email").indexOf("@"));
                            editor = preferences.edit();
                            editor.putString("username", email_username);
                            editor.commit();

                            Log.d(TAG, "logging in user " + preferences.getString("username", "") + " with userID: " + preferences.getString("userID", ""));
                            SpinLoginTask spinLoginTask = new SpinLoginTask(LoginActivity.this);
                            spinLoginTask.execute("facebook");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });
                request.executeAsync();
            }

I am getting the following response:

Facebook graph response: {Response:  responseCode: 200, graphObject: {"name":"Spin Tester","id":"xxxxx"}, error: null}
org.json.JSONException: No value for email

Since I set read permissions on the button to provide all information, why is none of it appearing in the Graph request response?

Upvotes: 2

Views: 1449

Answers (2)

Eenvincible
Eenvincible

Reputation: 5626

Try this code I have always used: EDIT

Actually, you need to change a small part of your code:

    GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
        @Override
        public void onCompleted(JSONObject object, GraphResponse response) {
               Log.d(TAG, "Facebook graph response: " + response.toString());
               try {
                      // get only the part before the @ symbol
                      String email_username = object.getString("email").substring(0, object.getString("email").indexOf("@"));
                        editor = preferences.edit();
                        editor.putString("username", email_username);
                        editor.commit();

                        Log.d(TAG, "logging in user " + preferences.getString("username", "") + " with userID: " + preferences.getString("userID", ""));
                        SpinLoginTask spinLoginTask = new SpinLoginTask(LoginActivity.this);
                        spinLoginTask.execute("facebook");
               } catch (Exception e) {
                      e.printStackTrace();
              }

           }
       });

    //ADD THIS LINES PLEASE
    Bundle parameters = new Bundle();
    parameters.putString("fields", "id,name,email");
    request.setParameters(parameters);
    request.executeAsync();
 }

Upvotes: 5

Pablo Baxter
Pablo Baxter

Reputation: 2234

Users have the option to deny access to their email address on first login via the Facebook provided UI. I would do a check to ensure that the email is in the JSON structure returned. If you require the email address, bring an AlertDialog or some other UI to ask them for their email. Make sure you have logic in place in case they refuse to provide email, since that is always a possibility too.

Upvotes: 1

Related Questions