0

After reading the FB publish tutorial

I'm trying to implement a simple publish, but in my application the user doesn't have to be logged in via FB. I want to open the loggin if needed and if so share.

And I got the next exception:

04-28 12:15:57.350: E/AndroidRuntime(32241): FATAL EXCEPTION: main
04-28 12:15:57.350: E/AndroidRuntime(32241): java.lang.UnsupportedOperationException: Session: an attempt was made to request new permissions for a session that has a pending request.
04-28 12:15:57.350: E/AndroidRuntime(32241):    at com.facebook.Session.requestNewPermissions(Session.java:968)
04-28 12:15:57.350: E/AndroidRuntime(32241):    at com.facebook.Session.requestNewPublishPermissions(Session.java:501)

My code:

    @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            boolean isFacebookResponse =
                    Session.getActiveSession().onActivityResult(getActivity(), requestCode, resultCode, data);
            if (isFacebookResponse) {
                shareViaFacebook();
            }
        }

private void CheckFacebookLogin(){
        if(Session.getActiveSession() == null){
            logInToFacebook();
        }else{
            shareViaFacebook();
        }
    }

    private void shareViaFacebook() {
        Session session = Session.getActiveSession();

        if (session != null){

            // Check for publish permissions    
            List<String> permissions = session.getPermissions();
            if (!isSubsetOf(PERMISSIONS, permissions)) {
                pendingPublishReauthorization = true;
                Session.NewPermissionsRequest newPermissionsRequest = new Session
                        .NewPermissionsRequest(this, PERMISSIONS);
            session.requestNewPublishPermissions(newPermissionsRequest);
                return;
            }

            Bundle postParams = new Bundle();
            postParams.putString("name", "Facebook SDK for Android");
            postParams.putString("caption", "Build great social apps and get more installs.");
            postParams.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
            postParams.putString("link", "https://developers.facebook.com/android");
            postParams.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");

            Request.Callback callback= new Request.Callback() {
                public void onCompleted(Response response) {
                    JSONObject graphResponse = response
                                               .getGraphObject()
                                               .getInnerJSONObject();
                    String postId = null;
                    try {
                        postId = graphResponse.getString("id");
                    } catch (JSONException e) {
                        Log.i(GA_CATEGORY,
                            "JSON error "+ e.getMessage());
                    }
                    FacebookRequestError error = response.getError();
                    if (error != null) {
                        Toast.makeText(getActivity()
                             .getApplicationContext(),
                             error.getErrorMessage(),
                             Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getActivity()
                                 .getApplicationContext(), 
                                 postId,
                                 Toast.LENGTH_LONG).show();
                    }
                }
            };

            Request request = new Request(session, "me/feed", postParams, 
                                  HttpMethod.POST, callback);

            RequestAsyncTask task = new RequestAsyncTask(request);
            task.execute();
        }
    }

    private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
        for (String string : subset) {
            if (!superset.contains(string)) {
                return false;
            }
        }
        return true;
    }

    private void logInToFacebook() {
        String app_id = getString(R.string.app_id);

        Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

        Session session = new Session.Builder(getActivity())
                .setApplicationId(app_id)
                .build();
        session.addCallback(this);
        Session.setActiveSession(session);

        // Login
        if (!session.isOpened() && !session.isClosed()) {
            session.openForRead(new Session.OpenRequest(this)
                    .setCallback(this));
        } else {
            Session.openActiveSession(getActivity(), true, this);
        }
    }

The login code works. What I'm doing wrong?

NickF
  • 5,637
  • 12
  • 44
  • 75

1 Answers1

1

I think you are trying to do login and share operations at the same time. You should wait until login operations finishing with required permissions. Then you can share your content via your application. Hope this helps.

ACengiz
  • 1,285
  • 17
  • 23