简体   繁体   English

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

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

I am trying to implement the login functionality using facebook and after successful login i have to fetch couple of information of the user. 我正在尝试使用Facebook实现登录功能,成功登录后,我必须获取用户的一些信息。 Now for login i wrote some code. 现在登录,我写了一些代码。 When i am pressing log in button, it is opening facebook, fetching the login info from the facebook native app and login also without error. 当我按“登录”按钮时,它正在打开Facebook,从facebook本地应用程序中获取登录信息,并且登录时也没有错误。 But after that it is coming back to my application's login page and in the OnActivityResult() method, session is becoming null. 但是之后,它又返回到我的应用程序的登录页面,并在OnActivityResult()方法中,会话变为空。 So i am not able to get the details. 所以我无法获得详细信息。 I have written the below code: 我写了下面的代码:

      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;

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

How can i resolve this issue. 我该如何解决这个问题。 Please help. 请帮忙。

Thanks, Arindam. 谢谢,阿林丹。

I have resolved the issue. 我已经解决了这个问题。 Basically i am using the SDK with older version but my FB App in device is latest. 基本上我使用的是旧版本的SDK,但设备中的FB App是最新的。 So it is giving the issue. 因此,它给了问题。 Thanks Akhil to point out the version of SDK which helped me to resolve. 感谢Akhil指出可以帮助我解决的SDK版本。

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

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