Facebook Email not found via Graph API
While
providing Login with facebook option we came across the Graph API. Development
goes well with SDK integration and further coding part. But problem arises when
we start to realize that we are not getting the Email id of
some users. which is either null or empty string.
So basically
there are two main reasons why this happens.
1) If the user has an unconfirmed email in Facebook (i.e. Facebook sent him a validation mail to the user's email address but he didn't respond) Facebook WILL NOT pass that email to your app even if he gave you the email permissions.
1) If the user has an unconfirmed email in Facebook (i.e. Facebook sent him a validation mail to the user's email address but he didn't respond) Facebook WILL NOT pass that email to your app even if he gave you the email permissions.
So what we can do is use his or her Facebook email if the user has a
user name (i.e. userName@facebook.com).
2) Another
reason is if the user declines the Permission to an email at the state
of accepting permissions from app then it would return with Empty string.
for this we have to simply check the declined
permissions on the AccessToken object response.
accessToken.getDeclinedPermissions().contains("email")
Here is the full implementation of the code.
private void performFblogin() { AccessToken accessToken = AccessToken.getCurrentAccessToken(); if (accessToken == null) { /* * * set permissions * * */ fbLoginButton.setReadPermissions(Arrays.asList("email", "user_friends", "user_birthday", "user_work_history", "user_education_history")); // location and hometown callbackManager = CallbackManager.Factory.create(); fbLoginButton.performClick(); // Callback registration fbLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { AccessToken accessToken = loginResult.getAccessToken(); Log.i("accessToken", accessToken.toString()); if (accessToken.getDeclinedPermissions().contains("email")) { //user has not granted permission for email // Toast.makeText(mCon,"Email not found",Toast.LENGTH_SHORT).show(); showEmailNotFoundDialog(); } else { GraphRequest request = GraphRequest.newMeRequest( loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted( JSONObject object, GraphResponse response) { processResponse(object); Log.v("LoginActivity", response.toString()); } }); Bundle parameters = new Bundle(); parameters.putString("fields", "id,name,birthday,email,gender,link,work,education,friends{first_name,last_name}"); request.setParameters(parameters); request.executeAsync(); } } @Override public void onCancel() { Toast.makeText(mCon, "Cancelled!", Toast.LENGTH_SHORT).show(); } @Override public void onError(FacebookException exception) { Toast.makeText(mCon, "Failed", Toast.LENGTH_SHORT).show(); } }); } else { LoginManager.getInstance().logOut(); performFblogin(); } }