Facebook Android SDK Session openForPublish not cr

2019-04-08 12:08发布

问题:

In the Facebook Android SDK when I call

Session tempSession = new Builder(this).build();
Session.setActiveSession(tempSession);
tempSession.openForRead(new OpenRequest(this).setPermissions(FB_PERMISSIONS));

It gets a FB session and every thing runs as normal. But when I replace Read with Publish. i.e. follows

Session tempSession = new Builder(this).build();
Session.setActiveSession(tempSession);
tempSession.openForPublish(new OpenRequest(this).setPermissions(FB_PERMISSIONS));

It gives an error saying, that the session is empty, and cannot get publish permissions to empty session.

Can you please tell why is it like this and what would be the best way to handle this?

回答1:

The short answer is, don't call openForPublish. Call openForRead, and then requestNewPublishPermissions later if you need publish permissions.

The long answer is, you can't request publish permissions (on a user who's never connected with Facebook before via your app) unless you already have basic or default permissions already (what you would get if you call openForRead with an empty permission set). So openForPublish actually handles a very specific niche use case that most apps probably don't have.



回答2:

It took me a while to sort this out so a user could click a button to share one of my products on Facebook in a feed. I didn't want them to be prompted to sign in until they actually wanted to share, so I really just wanted publish permission. The following stacks the initial login/read permission request with the publish permission request. This will double-prompt the users, first for read, then for publish, but that is required now regardless of the solution:

Session session = Session.getActiveSession();

if (session == null) {
    session = new Session.Builder(this).setApplicationId("<APP ID HERE>").build();

    Session.setActiveSession(session);
    session.addCallback(new StatusCallback() {
        public void call(Session session, SessionState state, Exception exception) {
            if (state == SessionState.OPENED) {
                Session.OpenRequest openRequest = new Session.OpenRequest(FacebookActivity.this);
                openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
                session.requestNewPublishPermissions(
                        new Session.NewPermissionsRequest(FacebookActivity.this, PERMISSIONS));
            }
            else if (state == SessionState.OPENED_TOKEN_UPDATED) {
                publishSomething();
            }
            else if (state == SessionState.CLOSED_LOGIN_FAILED) {
                session.closeAndClearTokenInformation();
                // Possibly finish the activity
            }
            else if (state == SessionState.CLOSED) {
                session.close();
                // Possibly finish the activity
            }
        }});
}

if (!session.isOpened()) {
    Session.OpenRequest openRequest = new Session.OpenRequest(this);
    openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
    session.openForRead(openRequest);
}
else
    publishSomething();