简体   繁体   English

Facebook Android SDK会话openForPublish没有创建新会话

[英]Facebook Android SDK Session openForPublish not creating a new session

In the Facebook Android SDK when I call 我打电话时在Facebook Android SDK中

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. 它得到一个FB会话,每件事都正常运行。 But when I replace Read with Publish . 但是当我用Publish替换Read时。 ie 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? 你能告诉我为什么会这样,最好的办法是什么?

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. 我花了一段时间对其进行排序,以便用户可以点击按钮在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();

The short answer is, don't call openForPublish. 简短的回答是,不要调用openForPublish。 Call openForRead, and then requestNewPublishPermissions later if you need publish permissions. 如果需要发布权限,请调用openForRead,然后再调用requestNewPublishPermissions。

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). 答案很长,除非您已经拥有基本或默认权限(如果您使用空权限集调用openForRead,将获得什么),您无法请求发布权限(对于之前从未通过您的应用程序与Facebook连接的用户) 。 So openForPublish actually handles a very specific niche use case that most apps probably don't have. 因此openForPublish实际上处理了大多数应用程序可能没有的特定利基用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM