android – 设置从Facebook登录获取用户电子邮件ID的权限

我正在使用Facebook 3.0 SDK for Android.我必须实现Facebook登录.我正在访问用户的基本信息,如name,userID.但是我想要访问用户的电子邮件.我已经经历了许多博客和论坛,但无法弄清楚如何做到这一点.

我正在使用自己的android按钮登录,而不是com.facebook.widget.LoginButton.如果我使用Facebook登录按钮,只需使用以下行:

authButton.setReadPermissions(Arrays.asList("basic_info","email"));

但是,我有自己的要求,必须使用默认的android按钮.这是我到目前为止所做的

Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
        this, Arrays.asList("email"));

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

    mLogin = (Button) findViewById(R.id.logIn);
    mLogin.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // start Facebook Login
    Session.openActiveSession(this, true, new Session.StatusCallback() {

        // callback when session changes state
        @Override
        public void call(final Session session, SessionState state,
                Exception exception) {
            if (session.isOpened()) {
                session.requestNewReadPermissions(newPermissionsRequest);
                // make request to the /me API
                Request.executeMeRequestAsync(session,
                        new Request.GraphUserCallback() {

                            // callback after Graph API response with user
                            // object
                            @Override
                            public void onCompleted(GraphUser user,
                                    Response response) {
                                if (user != null) {                         
                                    System.out
                                            .println("Facebook Response: "
                                                    + response.toString());
                                    access_token = session.getAccessToken();
                                    firstName = user.getFirstName();
                                    fb_user_id = user.getId();


                                    System.out
                                            .println("Facebook Access token: "
                                                    + access_token);
                                    System.out.println("First Name:"
                                            + firstName);
                                    System.out.println("FB USER ID: "
                                            + fb_user_id);


                                }
                            }
                        });
            }
        }
    });

}

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

我能够得到我正在日志中打印的所有东西,但无法从用户配置文件中检索电子邮件.

我该怎么办?

任何一种帮助将不胜感激.

最佳答案
解决了:这是我做了什么…

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

    mLogin = (Button) findViewById(R.id.logIn);
    mLogin.setOnClickListener(this);

}

@Override
public void onClick(View v) {

    Session currentSession = Session.getActiveSession();
    if (currentSession == null || currentSession.getState().isClosed()) {
        Session session = new Session.Builder(context).build();
        Session.setActiveSession(session);
        currentSession = session;
    }

    if (currentSession.isOpened()) {
        // Do whatever u want. User has logged in

    } else if (!currentSession.isOpened()) {
        // Ask for username and password
        OpenRequest op = new Session.OpenRequest((Activity) context);

        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 session = new Builder(MainActivity.this).build();
        Session.setActiveSession(session);
        session.openForPublish(op);
    }
}

public void call(Session session, SessionState state, Exception exception) {
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (Session.getActiveSession() != null)
        Session.getActiveSession().onActivityResult(this, requestCode,
                resultCode, data);

    Session currentSession = Session.getActiveSession();
    if (currentSession == null || currentSession.getState().isClosed()) {
        Session session = new Session.Builder(context).build();
        Session.setActiveSession(session);
        currentSession = session;
    }

    if (currentSession.isOpened()) {
        Session.openActiveSession(this, true, new Session.StatusCallback() {

            @Override
            public void call(final Session session, SessionState state,
                    Exception exception) {

                if (session.isOpened()) {

                    Request.executeMeRequestAsync(session,
                            new Request.GraphUserCallback() {

                                @Override
                                public void onCompleted(GraphUser user,
                                        Response response) {
                                    if (user != null) {

                                        TextView welcome = (TextView) findViewById(R.id.welcome);
                                        welcome.setText("Hello "
                                                + user.getName() + "!");

                                        access_token = session
                                                .getAccessToken();
                                        firstName = user.getFirstName();
                                        fb_user_id = user.getId();

                                        System.out
                                                .println("Facebook Access token: "
                                                        + access_token);
                                        System.out.println("First Name:"
                                                + firstName);
                                        System.out.println("FB USER ID: "
                                                + fb_user_id);

                                    }
                                }
                            });
                }
            }
        });
    }
}

现在我可以通过其余的发布权限获得电子邮件权限.

标记为接受的答案,以便它可以帮助他人.

转载注明原文:android – 设置从Facebook登录获取用户电子邮件ID的权限 - 代码日志