-1

I am trying to post on a friends wall using the following code, which I have taken from one of their tutorials. I have implemented all life-cycle methods, but its not clear as to what is happening.

The scenario:

I have an activity and a fragment tied to facebook login --> I request read permissions in this setup and populate my DB with the relevant information I get from Facebook.

Now I have a tab setup, which has a different MainActivity and child fragments, two of these children contain listviews, each row of this list is associated with different Facebook Friend ID's. On clikc of the facebook_post button in this list I use the following function to post on a facebook friends wall:

if (isNetworkConnected()) {
    Session session = Session.getActiveSession();
    if (session != null) {
        if(isLoggedIn()){
            Toast.makeText(getActivity(),"Session not null and logged in", Toast.LENGTH_LONG).show();
            if (hasPublishPermission()) {
                System.out.println("Has publish");
                Toast.makeText(getActivity(),"Session not null and logged in and publish too", Toast.LENGTH_LONG).show();
                Bundle params = new Bundle();
                params.putString("name", "Digital");
                params.putString("link", "http://www.digitals.com/");
                params.putString("picture","http://www.exar.com/images/frontend/logo.png");
                params.putString("to", FACEBOOK_ID);
                params.putString("caption", "Congrulations!");
                params.putString("description", textGreeting);
                params.putString("message", "message");


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

                                    @Override
                                    public void onComplete(Bundle values,
                                            FacebookException error) {
                                        // frag3.setFbId(null);
                                        // ---------------------------- got to put
                                        // check here
                                        //  onBackPressed();

                                    }
                                }).build();
                feedDialog.show();
            } else if (session.isOpened()) {
                // We need to get new permissions, then complete the action
                if (!hasPublishPermission())
                    session.requestNewPublishPermissions(new Session.NewPermissionsRequest(getActivity(), PERMISSION));
                Bundle params = new Bundle();
                params.putString("name", "Digital");
                params.putString("link", "http://www.digitals.com/");
                params.putString("picture","http://www.exareceipts.com/images/frontend/logo.png");
                params.putString("to", FACEBOOK_ID);
                params.putString("caption", "Congrulations!");
                params.putString("description", textGreeting);
                params.putString("message", "message");

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

                                    @Override
                                    public void onComplete(Bundle values,FacebookException error) {
                                        // frag3.setFbId(null);
                                        // ---------------------------- got to put
                                        // check here
                                        //onBackPressed();

                                    }
                                }).build();
                feedDialog.show();
            }
        }else{
            Toast.makeText(getActivity(),"Please check if you are logged in", Toast.LENGTH_LONG).show();

            mainFrag.setParam(1); 

            Intent mainIntent;
            mainIntent = new Intent(getActivity(),FacebookMain.class); 
            getActivity().startActivity(mainIntent);
        }
    }
} else {
    Toast.makeText(getActivity(),"Please check your internet connection", Toast.LENGTH_LONG).show();
}

}catch (Exception e){
    System.out.println("Exception post fb: " +e);
    Toast.makeText(getActivity(), "first construct"+e, Toast.LENGTH_SHORT).show();
}

this is my isLoggedIn function:

public boolean isLoggedIn() {
        Session session = Session.getActiveSession();
        if (session != null && session.isOpened()) {
            return true;
        } else {
            return false;
        }
    }

These are my LifeCycle methods ( In the current fragment):

private final String PENDING_ACTION_BUNDLE_KEY = "com.exa.dig:PendingAction";


    private PendingAction pendingAction = PendingAction.NONE;
    private GraphUser user1;

    private enum PendingAction {
        NONE, POST_PHOTO, POST_STATUS_UPDATE
    }



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

    public static boolean isActive() {
        Session session = Session.getActiveSession();
        if (session == null) {
            return false;
        }
        return session.isOpened();
    } 



@Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        uiHelper = new UiLifecycleHelper(getActivity(), callback);
        uiHelper.onCreate(savedInstanceState);
    }




private void onSessionStateChange(Session session, SessionState state,
            Exception exception) {
        if (pendingAction != PendingAction.NONE && (exception instanceof FacebookOperationCanceledException || exception instanceof FacebookAuthorizationException)) {
            new AlertDialog.Builder(getActivity()) .setTitle("cancelled").setMessage("NotGranted").setPositiveButton("Ok", null).show();
            pendingAction = PendingAction.NONE;
        } else if (state == SessionState.OPENED_TOKEN_UPDATED) {
            handlePendingAction();
        }
        updateUI();
    }





    public boolean isLoggedIn() {
        Session session = Session.getActiveSession();
        if (session != null && session.isOpened()) {
            return true;
        } else {
            return false;
        }
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }



    private void updateUI() {
        Session session = Session.getActiveSession();
        boolean enableButtons = (session != null && session.isOpened());



        if (enableButtons && user1 != null) {
            //  profilePictureView.setProfileId(user.getId());
            //  greeting.setText(getString(R.string.app_name, user.getFirstName()));
        } else {
            //  profilePictureView.setProfileId(null);
            //  greeting.setText(null);
        }
    }

    @SuppressWarnings("incomplete-switch")
    private void handlePendingAction() {
        PendingAction previouslyPendingAction = pendingAction;
        // These actions may re-set pendingAction if they are still pending, but
        // we assume they
        // will succeed.
        pendingAction = PendingAction.NONE;

    } 


    private boolean hasPublishPermission() {
        Session session = Session.getActiveSession();
        return session != null && session.getPermissions().contains("publish_actions");
    }

in the onCreate:

uiHelper = new UiLifecycleHelper(getActivity(), callback);
    uiHelper.onCreate(savedInstanceState);


//===============================================

Session.openActiveSessionFromCache(getActivity());

//================================================

However I am getting an exception:

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

The above usually happens when I uninstall the FB app from my phone to check if the web Dialog is working. Any idea?

Skynet
  • 7,820
  • 5
  • 44
  • 80
  • did you saw this http://stackoverflow.com/a/14817630/1665507 ? – Spring Breaker May 26 '14 at 11:24
  • Yes I checked that, I however am following the UiLifeCycleHelper pattern, its not clear enough for me though! I want a skeleton of an activity which can be used to post on friends wall. – Skynet May 26 '14 at 11:26

1 Answers1

1

The error typically means that you're making a permissions request while another one is ongoing. Without seeing the full flow of your code, it's impossible to tell where it's happening.

The good news is, since you're only using the feed dialog, you don't need publish permissions.

Ming Li
  • 15,672
  • 3
  • 37
  • 35
  • Hello Ming Li, can you provide me an example of a simple activity, say a skeleton which I can take reference from, or a link? – Skynet May 28 '14 at 04:08