Search code examples
androidfacebook-loginfacebook-android-sdkandroid-facebookandroid-sharing

Using Facebook SDK in Android, requestNewPublishPermissions always deny my publish_actions permission


I am trying to share a photo from Android through Facebook SDK, but when I try to request the publish_actions permissions of an opened session, Facebook returns back with denied permission, without showing the screen asking you about post your behalf. Why is that? and how can I solve it? By the way, I do not receive any error or something wrong in the Log.

Here is my code:

private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
public void onShareFacebook() {
    progressDialog.show();
    Session session = Session.getActiveSession();
    if (session == null || !session.isOpened()){
        LoginButton loginFacebookButton = new LoginButton(this);
        loginFacebookButton.setPublishPermissions(PERMISSIONS);
        loginFacebookButton.performClick();
        pendingShare = true;
    }
    else if (session != null && session.isOpened()){
        facebookSharePhoto();
    }
}

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

private void facebookSharePhoto(){
    pendingShare = false;

    Bitmap screenshotImage = takeScreenShot();
    String message = invokeEvent.name;

    postPhoto(screenshotImage, message, callbackShareFacebook);
}

private void postPhoto(Bitmap image, String message, Request.Callback callback) {
    Session session = Session.getActiveSession();

    if (session != null){

        // Check for publish permissions    
        List<String> permissions = session.getPermissions();
        List<String> permissionsd = session.getDeclinedPermissions();
        if (isSubsetOf(permissionsd, PERMISSIONS)){
            Toast.makeText(this, "Permisos denegados", Toast.LENGTH_SHORT).show();
            onBackPressed();
            return;
        }
        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", message);


        Log.d(TAG, "inicio getImageAsData");
        postParams.putByteArray("picture", getImageAsData(image));
        Log.d(TAG, "fin getImageAsData");

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

    }

}

At the begining I call to function:

            session.requestNewPublishPermissions(newPermissionsRequest);

But when the callback comes back the session has "publish_actions" permission denied in here:

        List<String> permissionsd = session.getDeclinedPermissions();
        if (isSubsetOf(permissionsd, PERMISSIONS)){
            Toast.makeText(this, "Permisos denegados", Toast.LENGTH_SHORT).show();
            onBackPressed();
            return;
        }

Solution

  • I have noticed that this does not happen when I am logged in into Facebook with the account which has created the application on Facebook. That means that, if I am not developer/tester of the Facebook application, the pop up asking for post permissions will not appear.

    It doesn't matter if you active the toggle button "available to general public" or not. The app will not be able to the general public until you submit your Facebook application for review ("Start a Submission" button).

    A screenshot of the two buttons I mentioned: enter image description here

    Solution: Start a Submission for your app or add the Facebook account to tester role for your app.