获取Facebook用户的电子邮件地址

时间:2014-04-07 08:29:33

标签: android facebook facebook-graph-api android-facebook

我正在申请Facebook集成:

但出现问题并且用户的电子邮件地址始终返回空值:

这是我的代码:

    @Override
public void onCreate(Bundle savedInstanceState) {
..
..
        //initiate session
        Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
        Session session = Session.getActiveSession();
        if (session == null) {
            if (savedInstanceState != null) {
                session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
            }
            if (session == null) {
                session = new Session(this);
            }
            Session.setActiveSession(session);
            if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
                Session.OpenRequest op = new Session.OpenRequest(this);
            op.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
            op.setCallback(null);

            List<String> permissions = new ArrayList<String>();
            permissions.add("publish_stream");
            permissions.add("user_likes");
            permissions.add("email");
            permissions.add("user_birthday");
            op.setPermissions(permissions);
            Session.setActiveSession(session);
            session.openForPublish(op);
        }
    }
}

@Override
public void onStart() {
    Logger.d(TAG,"onStart");
    super.onStart();
    Session.getActiveSession().addCallback(statusCallback);
}

@Override
public void onStop() {
    Logger.d(TAG,"onStop");
    super.onStop();
    Session.getActiveSession().removeCallback(statusCallback);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Logger.d(TAG,"onActivityResult");
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    Logger.d(TAG,"onSaveInstanceState");
    super.onSaveInstanceState(outState);
    Session session = Session.getActiveSession();
    Session.saveSession(session, outState);
}


private void getUserDetails(Session session) {
    Logger.d(TAG,"getUserDetails");
    Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
        @Override
        public void onCompleted(GraphUser user, Response response) {
            Log.d(TAG,"newMeRequest:onCompleted");
            if (user != null) {
                fbCurrentUser = user;
                Log.d(TAG,"Already logged-in : "+ user.getId());
                Log.d(TAG,"User First Name: "+ user.getFirstName());
                Log.d(TAG, "User Email: " + user.getProperty("email"));
                Log.e(TAG, "response: " + response);
                Log.e(TAG, "user: " + user.toString());
                //check if new user [get his friends list]
                getFriendsList();
            } else {
                Log.e(TAG,"No User Found");
            }
        }

    });
    Request.executeBatchAsync(request);
}

请帮忙! 提前谢谢!

2 个答案:

答案 0 :(得分:0)

尝试使用此功能获取电子邮件,

String email = user.asMap().get("email").toString();
Log.d(TAG, "User Email: " + email);
TextView mEmail = (TextView) findViewById(R.id.textView1);
mEmail.setText(email);

或尝试下面的代码

Log.d(TAG, "User Email: " + user.getProperty("email").toString()); 

只需在日志末尾添加toString()以获取更多参考,请查看此link

通过上面这段代码,我成功地收到了用户的电子邮件。

修改

Session.openActiveSession(this, true, new Session.StatusCallback() {

            @Override
            public void call(Session session, SessionState state,
                    Exception exception) {
                if (session.isOpened()) {
                    // make request to the /me API
                    Request.executeMeRequestAsync(session,
                            new Request.GraphUserCallback() {
                                @Override
                                public void onCompleted(GraphUser user,
                                        Response response) {
                                    if (user != null) {
                                        String email = user.asMap()
                                                .get("email").toString();
                                        mEmail.setText(email);
                                    }
                                }
                            });
                } else {
                    Toast.makeText(getApplicationContext(), "Error...",
                            Toast.LENGTH_LONG);
                }
            }
        });

答案 1 :(得分:0)

检查响应是否给出任何错误

JSONObject json = com.facebook.android.Util.parseJson(response);
  if(json != null){
        final String id = json.getString("id");
    final String email = json.getString("email");
  }