0

我尝试使用带有 SDK 3.5 的 Facebook 登录,但我无法让它工作。

登录弹出窗口不显示。

当用户单击我的按钮时,我使用此代码启动登录过程:

private void onClickLogin() {

        Session currentSession = Session.getActiveSession();
        if (currentSession == null || currentSession.getState().isClosed()) {
            Session session = new Session.Builder(RegisterPage.this).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) RegisterPage.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 session = new Session.Builder(RegisterPage.this).build();
            Session.setActiveSession(session);
            session.openForPublish(op);
        }
    }

而这个 onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode,
            Intent data2)
 {


        if (Session.getActiveSession() != null)
            Session.getActiveSession().onActivityResult(this, requestCode,
                    resultCode, data2);

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

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

                @SuppressWarnings("deprecation")
                @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) {

                                         facebook_name = user.getName();
                                         facebook_id = user.getId();
                                         email = (String) user.getProperty("email");

                                         Log.i("email", email);
                                        Log.i("facebook_name", facebook_name);

                                         if(isNetworkAvailable())
                                             new GetProfilePicture().execute();

                                        }
                                    }
                                });
                    }
                }
            });
        }

 }
4

0 回答 0