Abhishek V
Abhishek V

Reputation: 12526

Not able to post on face book wall. (#200) The user hasn't authorized the application to perform this action android facebook SDK error

I know this question has been asked many times before. But none of those answers are working for me. It seems to be some problem with the permission. It seems as though it is not storing the publish permissions and it keeps asking the permissions every time user tries to post something. Here is my code.

public class MainActivity extends Activity {

    private Session.StatusCallback statusCallback = new SessionStatusCallback();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.btn_post);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                postToFaceBook();
            }

        });

        Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
        //get the active Facebook session
        Session session = Session.getActiveSession();
        //If no active sessions found
        if (session == null) {
            if (savedInstanceState != null) {
                //Restore the saved session from bundle if any
                session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
            }
            if (session == null) {
                //If the session has not been stored in bundle, create a new session
                session = new Session(this);
            }
            //set session as current active session
            Session.setActiveSession(session);
            //If the session has not been opened yet, log the user into facebook
            //It does not involve interaction of user with facebook
            if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
                session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
            }
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        Session.getActiveSession().addCallback(statusCallback);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Session session = Session.getActiveSession();
        Session.saveSession(session, outState);
    }


    @Override
    protected void onStop() {
        super.onStop();
        Session.getActiveSession().removeCallback(statusCallback);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, intent);
    }

    private class SessionStatusCallback implements Session.StatusCallback {
        @Override
        public void call(Session session, SessionState state, Exception exception) {

            if (exception != null) {
                exception.printStackTrace();
            }
        }

    }

    private void postToFaceBook() {
        Log.i("fb", "inside share via facebook");
        Session session = Session.getActiveSession();
        if (!session.isOpened() && !session.isClosed()) {
            //Interact with the user to get user name and password for facebook
            session.openForRead(new Session.OpenRequest(MainActivity.this).setCallback(statusCallback));
        } else {
            Session.openActiveSession(MainActivity.this, true, statusCallback);
        }

        Bundle postParams = new Bundle();
        postParams.putString("message", "testing");
        postParams.putString("link", "https://www.youtube.com/watch?v=GcNNx2zdXN4");


        //get the current active facebook session
        session = Session.getActiveSession();
        //If the session is open
        if(session.isOpened()) {
            Log.i("fb", "inside isopened");
            //Get the list of permissions associated with the session
            List<String> permissions = session.getPermissions();
            //if the session does not have video_upload permission
            if(!permissions.contains("publish_actions") || !permissions.contains("publish_stream")) {
                Log.i("fb", "doesn't contain permission");
                //Get the permission from user to upload the video to facebook
                Session.NewPermissionsRequest newPermissionsRequest = new Session
                        .NewPermissionsRequest(MainActivity.this, Arrays.asList("publish_actions" , "publish_stream"));
                session.requestNewPublishPermissions(newPermissionsRequest);
            }

            Request.Callback callback= new Request.Callback() {
                public void onCompleted(Response response) {
                    Log.i("fb", "On completed");
                    FacebookRequestError error = response.getError();
                    if (error != null) {
                        //AppUtils.showToastMessage(th, error.getErrorMessage());
                        Toast.makeText(MainActivity.this, error.getErrorMessage(), Toast.LENGTH_SHORT).show();
                    } else {
                        //AppUtils.showToastMessage(mActivity, mResources.getString(R.string.posted_successfully));
                        //AppUtils.setSharedOrigami(mActivity, mPortraitKey);
                        Toast.makeText(MainActivity.this, "success", Toast.LENGTH_SHORT).show();
                    }

                    //mFacebookDialog.dismiss();
                }
            };

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

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

    }




}

Manifest :

<meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id" />

Do I need to submit the app for review before I can use publish_action permission? Any help would be appreciated. Thanks!

Upvotes: 0

Views: 167

Answers (1)

Ming Li
Ming Li

Reputation: 15662

What you have is a common problem. The issue is that you're making the request to me/feed immediately after a call to requestNewPublishPermissions.

However, requestNewPublishPermissions is an asynchronous request, so when you're doing the post to me/feed, you don't actually have the publish permissions yet. You need to save the fact that you're about to post, request for publish permissions, and then when that request returns (in onActivityResult, or in the callback), do the post to "me/feed" then.

Have a look in the Hello Facebook sample app that ships with the SDK, it shows you how to request publish permissions, and postpone the publish until you have all the permissions.

Upvotes: 1

Related Questions