Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - how to get user email with facebook sdk 4+ [duplicate]

I'm trying to get the Facebook user email from and app I'm developing. The thing is that i can get the name and everything else, but not the email. I already looked for all the common questions over here and i saw many many ways to do it but no one works on my app. I hope you can give me a hand.

LoginActivity.java (On create)

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_login);
        // Set up the login form.

//        AppEventsLogger.activateApp(this);
        mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
        populateAutoComplete();

        loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions(Arrays.asList(
                "public_profile", "email", "user_birthday", "user_friends"));
        // Callback registration
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                // App code
                GraphRequest.newMeRequest( loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject me, GraphResponse response) {
                                if (response.getError() != null) {
                                    // handle error
                                } else {
                                    String email = me.optString("email");
                                    String id = me.optString("id");
                                    String name = me.optString("name");
                                    Toast.makeText(getApplicationContext(),

                                            email+" / "+name,

                                            Toast.LENGTH_LONG).show();
                                    // send email and id to your web server
                                }
                            }
                        }).executeAsync();


            }

            @Override
            public void onCancel() {
                // App code
                Toast.makeText(getApplicationContext(),

                        "Cancelado",

                        Toast.LENGTH_LONG).show();
            }

            @Override
            public void onError(FacebookException exception) {
                // App code
                Toast.makeText(getApplicationContext(),

                        "Error",

                        Toast.LENGTH_LONG).show();
            }


        });

//        loginButton.setReadPermissions("user_friends");



        mPasswordView = (EditText) findViewById(R.id.password);
        mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
                if (id == R.id.login || id == EditorInfo.IME_NULL) {
                    attemptLogin();
                    return true;
                }
                return false;
            }
        });

        Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
        mEmailSignInButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                attemptLogin();
            }
        });

        mLoginFormView = findViewById(R.id.login_form);
        mProgressView = findViewById(R.id.login_progress);
    }

OnActivityResult

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

    @Override
    public void onDestroy() {
        super.onDestroy();
        profileTracker.stopTracking();
    }
like image 814
Andres Rodriguez Avatar asked Jan 06 '23 05:01

Andres Rodriguez


1 Answers

Try This

protected void connectToFacebook() {
ArrayList<String> list = new ArrayList<String>();
list.add("email");
// LoginManager.getInstance().logInWithReadPermissions(this, list);
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "user_photos", "public_profile"));

LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        // App code
        GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject json, GraphResponse response) {
                // Application code
                if (response.getError() != null) {
                    System.out.println("ERROR");
                } else {
                    System.out.println("Success");
                    String jsonresult = String.valueOf(json);
                    System.out.println("JSON Result" + jsonresult);

                    String fbUserId = json.optString("id");
                    String fbUserFirstName = json.optString("name");
                    String fbUserEmail = json.optString("email");
                    String fbUserProfilePics = "http://graph.facebook.com/" + fbUserId + "/picture?type=large";
                    callApiForCheckSocialLogin(fbUserId, fbUserFirstName, fbUserEmail, fbUserProfilePics, "fb");
                }
                Log.v("FaceBook Response :", response.toString());
            }
        });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,email,gender, birthday");
        request.setParameters(parameters);
        request.executeAsync();


    }

    @Override
    public void onCancel() {
        // App code
        Log.v("LoginActivity", "cancel");
    }

    @Override
    public void onError(FacebookException exception) {
        // App code
        // Log.v("LoginActivity", "" + exception);
        Utilities.showToast(mActivity, "" + exception);
    }
   });
}
like image 164
mdDroid Avatar answered Mar 04 '23 02:03

mdDroid