繁体   English   中英

在Android中登录Facebook后,会话在OnActivityResult方法中为空

[英]Session is becoming null in OnActivityResult Method after login to Facebook in Android

我正在尝试使用Facebook实现登录功能,成功登录后,我必须获取用户的一些信息。 现在登录,我写了一些代码。 当我按“登录”按钮时,它正在打开Facebook,从facebook本地应用程序中获取登录信息,并且登录时也没有错误。 但是之后,它又返回到我的应用程序的登录页面,并在OnActivityResult()方法中,会话变为空。 所以我无法获得详细信息。 我写了下面的代码:

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

                if (currentSession.isOpened()) {
                    // Do whatever u want. User has logged in
                     Log.d("Testing", "current session is opened already  ");

                } else if (!currentSession.isOpened()) {
                    // Ask for username and password

//                  currentSession.openForRead(new Session.OpenRequest(this).setCallback(statusCallback);
                    OpenRequest op = new Session.OpenRequest((Activity) this);

                    op.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
                    op.setCallback(null);

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

                    Session s = new Session(LoginScreen.this);//new Builder(LoginScreen.this).build();
                    Session.setActiveSession(s);
                    s.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);
                Log.d("Testing", "in onActivityResult");
                if (Session.getActiveSession() != null)
                    Session.getActiveSession().onActivityResult(this, requestCode,
                            resultCode, data);

                Session currentSession = Session.getActiveSession();
                if (currentSession == null || currentSession.getState().isClosed()) {
                    Log.d("Testing", "current session null");
                    Session session1 = new Session.Builder(getBaseContext()).build();
                    Session.setActiveSession(session1);
                    currentSession = session1;
                }

                if (currentSession.isOpened()) {
                    Log.d("Testing", "current session already opened");
                    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) {

                                                mFbUser = user;

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

我该如何解决这个问题。 请帮忙。

谢谢,阿林丹。

我已经解决了这个问题。 基本上我使用的是旧版本的SDK,但设备中的FB App是最新的。 因此,它给了问题。 感谢Akhil指出可以帮助我解决的SDK版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM