如何从Facebook SDK在Android应用程序获取电子邮件ID?

问题描述:

我在我的Andr​​oid应用程序集成Facebook登录。我想获得登录用户的电子邮件ID。我将如何得到它?

I integrated Facebook login in my android application. I want to get email id of login user. How will I get it?

private void loginToFacebook() {
    Session.openActiveSession(this, true, new Session.StatusCallback() {

        @Override
        public void call(Session session, SessionState state, Exception exception) {
            if (session.isOpened()) {
                Log.i(TAG, "Access Token" + session.getAccessToken());
                Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        if (user != null) {
                            try {

                                 userID =user.getId();
                                 provider="facebook";
                                 userName=user.getUsername();
                                 firstName=user.getFirstName();
                                 lastName=user.getLastName();
                                 Log.d("****User****", "details:" + user);
                                 }catch(Exception e){

下面是我的code。我使用 Request.GraphUserCallback()方法,但有电子邮件从这个方法没有任何反应。

Here is my code. i use Request.GraphUserCallback() method but there is no response of email from this method.

在调用 Session.openActiveSession 这样做是为了获得许可补充一点:

Before calling Session.openActiveSession do this to get permissions add this:

List<String> permissions = new ArrayList<String>();
permissions.add("email");

的最后一个参数Session.openActiveSession()权限。 现在,您可以访问 user.getProperty(电子邮件)。的toString()

The last parameter in Session.openActiveSession() should be permissions. Now you can access user.getProperty("email").toString().

编辑:

这是我做的Facebook授权方式:

This is the way I am doing facebook authorization:

List<String> permissions = new ArrayList<String>();
permissions.add("email");

loginProgress.setVisibility(View.VISIBLE);

//start Facebook 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
            Log.e("sessionopened", "true");
            Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
                @Override
                public void onCompleted(GraphUser user, Response response) {
                    if (user != null) {
                        String firstName = user.getFirstName();
                        String lastName = user.getLastName();
                        String id = user.getId();
                        String email = user.getProperty("email").toString();

                        Log.e("facebookid", id);
                        Log.e("firstName", firstName);
                        Log.e("lastName", lastName);
                        Log.e("email", email);
                    }
                }
            });
         }
     }
 }, permissions);

添加此方法的活动:

private static Session openActiveSession(Activity activity, boolean allowLoginUI, Session.StatusCallback callback, List<String> permissions) {
    Session.OpenRequest openRequest = new Session.OpenRequest(activity).setPermissions(permissions).setCallback(callback);
    Session session = new Session.Builder(activity).build();
    if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI) {
        Session.setActiveSession(session);
        session.openForRead(openRequest);
        return session;
    }
    return null;
}