在Android中集成Facebook SDK并检索用户的电子邮件

时间:2016-08-09 07:52:42

标签: android facebook-sdk-4.0

我想要检索登录的用户电子邮件。这是我绑定的,但没有检索电子邮件。我只能检索 ID,名字,姓氏,而不能检索电子邮件

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

            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            Log.v("LoginActivity", response.toString());

                            // Application code
                            try {
                                String userEmail = object.getString("email");
                                String userId = object.getString("id");
                                emailEditText.setText(userEmail);
                                new SignupTask(SignupActivity.this, userId, userEmail).execute();

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

                        }
                    });

            Bundle parameters = new Bundle();
            parameters.putString("fields", "id, first_name, last_name, email"); // Parámetros que pedimos a facebook
            request.setParameters(parameters);
            request.executeAsync();
        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException e) {

        }
    });

    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "email", "user_friends"));

我错过了什么......

2 个答案:

答案 0 :(得分:1)

ohk..try this code its working for me...    

LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setBackgroundResource(R.drawable.fb);
        loginButton.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        loginButton.setReadPermissions(Collections.singletonList("public_profile, email, user_birthday, user_friends"));

        callbackManager = CallbackManager.Factory.create();
        if (netWorkStatus) {
            LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    // App code
                    //Store Facebook data to webservice .
                    GraphRequest request = GraphRequest.newMeRequest(
                            loginResult.getAccessToken(),
                            new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(JSONObject object, GraphResponse response) {
                                    // Application code
                                    try {
                                        // String picture = object.getString("picture");
                                        //JSONObject jobj = new JSONObject(picture);
                                        //JSONObject dataObj = jobj.getJSONObject("data");

                                        name = object.getString("name");
                                        userEmail = object.getString("email");
                                        //String url = dataObj.getString("url").replace("\\", "");

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

                                }
                            });

                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "id,name,email,gender,birthday,picture");
                    request.setParameters(parameters);
                    request.executeAsync();

                }

                @Override
                public void onCancel() {
                    // App code

                }

                @Override
                public void onError(FacebookException exception) {
                    // App code

                }

            });
        }

答案 1 :(得分:0)

CallbackManager callbackManager ;

//in OnCreate  initialize it
callbackManager = CallbackManager.Factory.create();


//after that overide onActivityResult
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

callbackManager.onActivityResult(requestCode, resultCode, data);
        }