1

I can login to my app then share something. This needs an OPENED session state. However, when I am not logged in, then I want to share something, I need to open the session. I am using a ViewPager so e.g. when I go from one page to another and this code

Session.openActiveSession(getActivity(), true, new StatusCallback() {
       @Override
       public void call(Session session, SessionState state, Exception exception) {
       }
});

is in the beginning of the code, then the session becomes active, and I get automatically logged in, which is wrong! That's why I put this code block into an onClickListener, so I only want to open the session if I click the share button in my app:

if (session != null && session.isOpened()) {
  publishFeedDialog();
}
else {
   Session.openActiveSession(getActivity(), true, new StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
        }
    });
   publishFeedDialog();
}

private void publishFeedDialog() {
  session = Session.getActiveSession();
  Log.i("TAG", session.getState() + ""); //OPENING


  WebDialog feedDialog = (
                    new WebDialog.FeedDialogBuilder(getActivity(),
                        Session.getActiveSession(),
                        params))
                    .setOnCompleteListener(new OnCompleteListener() {

                        @Override
                        public void onComplete(Bundle values,
                            FacebookException error) {
                             if (error == null) {
                                    final String postId = values.getString("post_id");
                                    if (postId != null) {
                                    } else {
                                        // User clicked the Cancel button
                                    }
                                } else if (error instanceof FacebookOperationCanceledException) {
                                } else {
                                    // Generic, ex: network error
                                }
                        }

                    })
                    .build();
                feedDialog.show();
}

The error:

Attempted to use a session that was not open.

So I open the session in vain, because it is still OPENING when the WebDialog should appear.

Please help.

erdomester
  • 11,789
  • 32
  • 132
  • 234

3 Answers3

5

For me it works like this:

        if (Session.getActiveSession() != null && Session.getActiveSession().isOpened()) {
              publishFeedDialog();
            }
        else {

            Session session = Session.getActiveSession();
            if (!session.isOpened() && !session.isClosed()) {

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

                session.openForRead(new Session.OpenRequest(this)
                //                .setPermissions(permissions)
                .setCallback(mFacebookCallback));
            } else {
                Session.openActiveSession(getActivity(), true, mFacebookCallback);
            }
        }`

where callback is

private Session.StatusCallback mFacebookCallback = new Session.StatusCallback() {
    @Override
    public void
    call(final Session session, final SessionState state, final Exception exception) {

        if (state.isOpened()) {
            String facebookToken = session.getAccessToken();
            Log.i("MainActivityFaceBook", facebookToken);
            Request.newMeRequest(session, new Request.GraphUserCallback() {

                @Override
                public void onCompleted(GraphUser user,
                        com.facebook.Response response) {
                    publishFeedDialog();
                }
            }).executeAsync();
            Prefs.setStringProperty(getActivity(), R.string.key_prefs_facebook_token, facebookToken);
        }
    }
};

Try to call publishFeedDialog() in CallBack onCompleted

Penzzz
  • 2,814
  • 17
  • 23
  • Best answer at StackOverFlow.com of this problem – Rafael Ruiz Muñoz Jun 28 '14 at 18:55
  • I am trying to use this code, but `Prefs` and `R.string.key_prefs_facebook_token` is undefined. Can you please help? – Adil Malik Aug 27 '14 at 15:46
  • I just commented out this line and it worked. Not sure what was the purpose of this line. I'll give you an up-vote if you please clarify this point. – Adil Malik Aug 27 '14 at 16:00
  • It was my implementation of saving session.getAccessToken()(fb token) to shared preferences.(Prefs its my manager to work with shared preferences) In my app i sent it to app's server. – Penzzz Aug 28 '14 at 12:40
  • R.string.key_prefs_facebook_token - just a key where to save token – Penzzz Aug 28 '14 at 12:40
2

Try calling publishFeedDialog() from inside call() after you check for the status of the session (do not use session.isOpened(), use state.isOpened() instead). Checking the state ensures that your Session is in an open state and that it can also be used to execute requests. The StatusCallback can be called multiple times until the Session is actually open, that is why you should check the status before sending any request.

Emmanuel
  • 13,083
  • 4
  • 39
  • 53
  • I m having the same problem but the solution u provided doesn't works ... Can you please answer this question http://stackoverflow.com/questions/24656830/com-facebook-facebookexception-attempted-to-use-a-session-that-was-not-open – Vivek Warde Jul 11 '14 at 16:20
  • Didn't realize that the status callback could be called multiple times. Thanks for clearing that up. – Austyn Mahoney Sep 26 '14 at 23:45
0

If you are opening a session from a fragment use the next openActiveSession method implementation.

openActiveSession(Context context, Fragment fragment, boolean allowLoginUI, Session.StatusCallback callback)

This works for me.

Silmood
  • 218
  • 1
  • 3
  • 11