I am posting to a Facebook Fan page in my application but I am getting the error Which i am not able to figure it out.

The Error is:

java.lang.UnsupportedOperationException: Session: an attempt was made to request new permissions for a session that has a pending request.

I am able to post the data on my wall but when i want to post the data on the Facebook Fan page in my application i am getting the above error.
The code snippets are :

public class FacebookShareFragment extends Fragment {

    private ProgressDialog progressDialog;
    private UiLifecycleHelper uiHelper;
    private static final List<String> PERMISSIONS = Arrays.asList("publish_actions","manage_pages","publish_stream");
    private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";
    private boolean pendingPublishReauthorization = false;

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

 @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.facebook_share_fragment, container, false);
        if (savedInstanceState != null) {
            pendingPublishReauthorization =
                    savedInstanceState.getBoolean(PENDING_PUBLISH_KEY, false);
        }
 shareOnFanPage();
 return view;
}

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        uiHelper = new UiLifecycleHelper(getActivity(), callback);
        uiHelper.onCreate(savedInstanceState);
    }
@Override
    public void onResume() {
        super.onResume();
        Session session = Session.getActiveSession();
        if (session != null &&
                (session.isOpened() || session.isClosed()) ) {
            onSessionStateChange(session, session.getState(), null);
        }

        uiHelper.onResume();
    }

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(PENDING_PUBLISH_KEY, pendingPublishReauthorization);
        uiHelper.onSaveInstanceState(outState);
    }

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (state.isOpened()) {
            if (pendingPublishReauthorization &&
                    state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
                pendingPublishReauthorization = false;
       //         publishStory();
                shareOnFanPage();
            }
        } else if (state.isClosed()) {
        }
    }

    /**
     *  Method to publish on the fan page
     */
    public void shareOnFanPage(){
        Session session = Session.getActiveSession();
        if (session != null) {
            List<String> permissions = session.getPermissions();
            if (!isSubsetOf(PERMISSIONS, permissions)) {
                pendingPublishReauthorization = true;
                Session.NewPermissionsRequest newPermissionsRequest = new Session
                        .NewPermissionsRequest(this, PERMISSIONS);
                session.requestNewPublishPermissions(newPermissionsRequest); // AT THIS POINT I AM GETTING THE ERROR.
                return;
            }

            // Show a progress dialog because the batch request could take a while.
            progressDialog = ProgressDialog.show(getActivity(), "",
                    getActivity().getResources().getString(R.string.progress_dialog_text), true);

            try {
                final RequestBatch requestBatch = new RequestBatch();
                requestBatch.add(fbFan());
                requestBatch.executeAsync();
            }catch(Exception e){
                    e.printStackTrace();
            }
        }
    }

Firstly I was able to perform Posting the data on My FB wall But later according to the requirement I added the new permissions "manage_pages" & "publish_stream" but dont know why i am getting the error even when i commented the publishStory() method which post data on my FB wall.

I read many post but not able to figure out...

1

There are 1 answers

0
Ming Li On

I believe onCreateView is called before onActivityResult. In this case, if your hosting activity was killed before the permissions request finishes (or if you have the "Don't keep activities" dev option turned on), then this method will always be called before onActivityResult.

Since you also call shareOnFanPage in your onCreateView, you're essentially going to make another permissions request before the previous one finishes (since it's finished in onActivityResult when you pass it to the uiLifecycleHelper).

I would move the shareOnFanPage() call to onResume instead, or just delete it altogether since I don't think you need it since you're already going to call onSessionStateChanged in your onResume method.