0

How to get email id from facebook Login

permission added are

mFBLoginButton.setReadPermissions("user_friends");
mFBLoginButton.setReadPermissions("public_profile");
mFBLoginButton.setReadPermissions("email");
mFBLoginButton.setReadPermissions("user_birthday");

I am going through Facebook tutorial for login and to access email I am using GraphRequest code is as below

new GraphRequest(
        AccessToken.getCurrentAccessToken(),
        "/me?fields=email",
        null,
        HttpMethod.GET,
        new GraphRequest.Callback() {
            public void onCompleted(GraphResponse response) {
                if (response != null)
                    Log.d(TAG, " response " + response.toString());
            }
        }
).executeAsync();

but I am not getting email in the response. could some one help me please

Sharanabasu Angadi
  • 4,304
  • 8
  • 43
  • 67

3 Answers3

0

As 'Md Abdul Gafur' mentioned make sure your facebook account is properly setup.

Add the dependency to your build.gradle:

compile 'com.facebook.android:facebook-android-sdk:4.2.0'

Add the facebook activity to your AndroidManifest.xml:

    <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />

Then in your actvitiy override onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    mCallbackManager.onActivityResult(requestCode, resultCode, data);
}

Login from your activity with:

FacebookSdk.sdkInitialize(mContext.getApplicationContext());
    FacebookSdk.setApplicationId(mApiKey);
    mCallbackManager = CallbackManager.Factory.create();
    LoginManager.getInstance().registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject user, GraphResponse response) {
                    if (response.getError() != null) {
                        // handle error
                    } else {
                        if (user != null) {
                            // Get the data you need 
                            String email = user.optString("email", "");
                        } else {
                            //handle null user
                        }
                    }
                }
            });
            request.executeAsync();
        }

        @Override
        public void onCancel() {
           //handle cancel
        }

        @Override
        public void onError(FacebookException exception) {
           // handle exception
        }
    });
    LoginManager.getInstance().logInWithReadPermissions(mContext, Collections.singletonList("public_profile"));
Andre Classen
  • 3,868
  • 3
  • 26
  • 36
0

you need to asking for the permissions` using the parameter while logging in to grab permissions, you must also consider the fact that not every logged in user has an email assigned to their account also permissions to access it.

one can open and verify their facebook accounts using their Mobile numbers, hence the probability that no email exists in your account.

hope this info helps!

Md Abdul Gafur
  • 6,213
  • 2
  • 27
  • 37
0

Use this code. it's works successfully. You can get all user data by this.

loginButton = (LoginButton) findViewById(R.id.login_button);

List < String > permissionNeeds = Arrays.asList("user_photos", "email",
 "user_birthday", "public_profile", "AccessToken");
loginButton.registerCallback(callbackManager,
new FacebookCallback < LoginResult > () {@Override
 public void onSuccess(LoginResult loginResult) {

  System.out.println("onSuccess");

  String accessToken = loginResult.getAccessToken()
   .getToken();
  Log.i("accessToken", accessToken);

  GraphRequest request = GraphRequest.newMeRequest(
  loginResult.getAccessToken(),
  new GraphRequest.GraphJSONObjectCallback() {@Override
   public void onCompleted(JSONObject object,
   GraphResponse response) {
    Log.i("LoginActivity", response.toString());
    try {
     id = object.getString("id");
     try {
      URL profile_pic = new URL(
       "http://graph.facebook.com/" + id + "/picture?type=large");
      Log.i("profile_pic",
      profile_pic + "");

     } catch (MalformedURLException e) {
      e.printStackTrace();
     }
     name = object.getString("name");
     email = object.getString("email");
     gender = object.getString("gender");
     birthday = object.getString("birthday");
    } catch (JSONException e) {
     e.printStackTrace();
    }
   }
  });
  Bundle parameters = new Bundle();
  parameters.putString("fields",
   "id,name,email,gender, birthday");
  request.setParameters(parameters);
  request.executeAsync();
 }

 @Override
 public void onCancel() {
  System.out.println("onCancel");
 }

 @Override
 public void onError(FacebookException exception) {
  System.out.println("onError");
  Log.v("LoginActivity", exception.getCause().toString());
 }
});
Harvi Sirja
  • 2,472
  • 2
  • 18
  • 19
  • If any other problem than check my answer here for facebook login code reference. http://stackoverflow.com/questions/31327897/custom-facebook-login-button-android/31332928#31332928 It's works successfully. – Harvi Sirja Jul 30 '15 at 08:50