0

I'm trying to learn about Facebook login. I'm able to successfully login in my app using Facebook sdk. But while fetching the email id of the user sometimes I get null value when user have hidden their email in Facebook website settings.In that case I am asking user to enter their Facebook email id.After that I want to check if the email id is register to Facebook or not. Is there any way to check if the entered email is registered with Facebook?

public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<Cursor> {

  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

       
        facebookOncreateCalling();

       
        ImageView facebookLogin = (ImageView) findViewById(R.id.facebook_login_imageView2);
        facebookLogin.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

              


               try {
                   LoginManager.getInstance().logOut();

                } catch (Exception e) {
                    e.printStackTrace();
                }

                LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this, Arrays.asList("public_profile", "user_friends", "email" /*, "user_mobile_phone", "email", "user_birthday"*/));
            }
        });

    }

   
   
    public void hideSoftKeyboard(EditText inputtext) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(inputtext.getWindowToken(), 0);
    }

    
    CallbackManager callbackManager;
    AccessTokenTracker accessTokenTracker;
    AccessToken accessToken;

    private void facebookOncreateCalling() {

        try {
            PackageInfo info = getPackageManager().getPackageInfo(
                    getApplicationContext().getPackageName(),
                    PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                Log.d("Facebook KeyHash:", "KeyHash:-> " + Base64.encodeToString(md.digest(), Base64.DEFAULT));

               
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        FacebookSdk.sdkInitialize(getApplicationContext());
        AppEventsLogger.activateApp(this);

        callbackManager = CallbackManager.Factory.create();

        LoginManager.getInstance().registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        callGraphApi(loginResult);
                    }

                    @Override
                    public void onCancel() {
                        Log.e("====Login Activity===","Cancel");
                    }

                    @Override
                    public void onError(FacebookException exception) {
                        Log.e("====Login Activity===","Error"+exception);
                    }
                });

        
        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(
                    AccessToken oldAccessToken,
                    AccessToken currentAccessToken) {
                accessToken = currentAccessToken;
            }
        };
        accessToken = AccessToken.getCurrentAccessToken();
    }

   
    private void callGraphApi(LoginResult loginResult) {
        GraphRequest request = GraphRequest.newMeRequest(
                loginResult.getAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(
                            JSONObject object,
                            GraphResponse response) {
                        try {
      
                            Log.e("LoginActivity", "GraphRequest:" + object);

                           

                            String fbUserId = object.optString("id");
                            String firstNameString = object.optString("first_name");
                            String lastNameString = object.optString("last_name");
                            String email = object.optString("email");

                            String name = object.optString("name");

                            String url = AppDataUtill.APP_BASE_URL + "facebookSignup";
                            HashMap<String, String> map = new HashMap<String, String>();
                            map.put("fname",firstNameString);
                            map.put("lname",lastNameString);
                            map.put("email",email);
                            map.put("fb_user_id", fbUserId);

                            callFbSignupApi(url, map, 1, fbUserId);
                           


                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,first_name,last_name,link,birthday,gender,email");
        request.setParameters(parameters);
        request.executeAsync();
    }


   


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


        
    }

   
    private void validation(){

        AlertDialog.Builder alert = new AlertDialog.Builder(getApplicationContext());
        alert.setMessage("Enter Your Email"); 

       
        final EditText input = new EditText(getApplicationContext());
        alert.setView(input);

        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                
                String srt = input.getEditableText().toString();
                if (srt.matches("")) {
                    Toast.makeText(getApplicationContext(), "Enter Your Email", Toast.LENGTH_LONG).show();
                    return;
                }

            } 
        }); 
        alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                
                dialog.cancel();
            }
        }); 
        AlertDialog alertDialog = alert.create();
        alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        alertDialog.show();



    }



}
Ushasi
  • 13
  • 3
  • Its not necessary that each fb user has registered his/her email id as fb allows user to signup with mobile no too. – Dhruvi Jul 22 '16 at 08:09
  • Let's suppose user has registered with email id. Then how to check it? – Ushasi Jul 22 '16 at 08:19
  • If you were getting mail id from facebook, you would obviously do autologin but you are specifying that you are not getting fb email id, then with what you will compare your user entered email id? – Dhruvi Jul 22 '16 at 08:31
  • That's why I want to know that is there any way to check if the entered email in edittext is registered with Facebook or not? – Ushasi Jul 22 '16 at 08:43
  • Of course it is not, that would be a huge violation of privacy. – CBroe Jul 22 '16 at 10:13

1 Answers1

0

It is not possible to search for users by email, there is no way to check if a specific email is registered on Facebook. More information: Find Facebook user (url to profile page) by known email address

You can only authorize users with the email permission, if you don´t get their email then there´s nothing you can do.

Community
  • 1
  • 1
andyrandy
  • 72,880
  • 8
  • 113
  • 130