4
votes

When user hits the cancel button, it closes the facebook session.

Facebook Permission Request Dialog: http://imgur.com/2PiYGrK

I'm trying to request permissions from a session that apparently was closed when user hit "Cancel" on Facebook Permission Request Dialog. When I try to openActiveSession without requestNewReadPermissions, only basic permissions are requested, but when I do I get this error:

E/AndroidRuntime(12619): java.lang.UnsupportedOperationException:Session: an attempt was made to request new permissions for a session that has been closed.

The code below is taken from Facebook SDK v3.5.2 LoginUsingActivityActivity.java sample, I just added:

Session.NewPermissionsRequest request = new Session.NewPermissionsRequest(this, PERMISSIONS);
session.requestNewReadPermissions(request);

Here is the code:

private static final List<String> PERMISSIONS = Arrays.asList("user_birthday", "user_friends","user_hometown","user_location","email");

private void onClickLogin() {
        Session session = Session.getActiveSession();
        if (!session.isOpened() && !session.isClosed()) {
            session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback).setPermissions(PERMISSIONS));
        } else {
            Session.openActiveSession(this, true, statusCallback);
            Session.NewPermissionsRequest request = new Session.NewPermissionsRequest(this, PERMISSIONS);
            session.requestNewReadPermissions(request);
        }
    }

I was searching for a solution and apparently I have to destroy the current session and create a new one. This works, but I think it's a bad code. There's another way to reopen session and request new permissions?

private void onClickLogin() {
        Session session = Session.getActiveSession();
        if (!session.isOpened() && !session.isClosed()) {
            session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback).setPermissions(PERMISSIONS));
        } else {
            session.closeAndClearTokenInformation();
            session = null; 
            session = new Session(this);

            session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback).setPermissions(PERMISSIONS));
        }
    }
1

1 Answers

1
votes

I had to do a lot of workarounds to be able to do this, I'll not recommend you to continue with that path, the new SDK has some callbacks that are not perfect but they're going to work for what you are trying to achieve:

https://developers.facebook.com/docs/android/upgrading-4.x

Just follow those guidelines to update, and you'll see an improved SDK experience, less code, less bugs, and what it's better better control of the callbacks.