11

我用Facebook 3.0 SDK的是安卓。我必须实现Facebook登录。我正在访问用户的基本信息,如姓名、用户 ID。但我也想访问用户的电子邮件。我浏览了许多博客和论坛,但不知道该怎么做。

我正在使用我自己的 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);
}

我能够获取我在日志中打印的所有内容,但无法从用户配置文件中检索电子邮件。

我能为此做些什么?

任何形式的帮助将不胜感激。

4

2 回答 2

15

解决了它:这是我所做的......

@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);

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

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

将此标记为已接受的答案,以便它可以帮助其他人。

于 2013-05-03T10:36:13.300 回答
3

TL; DR:使用这样的东西来获取电子邮件:

String email = (String) user.getProperty("email");

我对您的代码有一条评论。由于“电子邮件”是读取权限,因此您无需创建单独的权限请求即可获得它。你可以做这样的事情(代码未经测试,所以可能会有轻微的错误):

Session s = new Session(this);
Session.setActiveSession(s);
Session.OpenRequest request = new Session.OpenRequest(this);
request.setPermissions(Arrays.asList("basic_info","email"));
request.setCallback( /* your callback here */ );
s.openForRead(request);

您可以在回调中摆脱新的权限请求。

于 2013-05-02T16:20:06.827 回答