0

I am trying to post image on Facebook using sdk 3.0, but I am facing some problems. If I close the app and reopens the app it always goes in else condition, then it goes in if condition. Do I need to save session or its already stored??

          Session mSession = Session.getActiveSession();

    if (mSession != null
            && mSession.getPermissions().contains("publish_actions")) {
        postPhoto();

    } else {

        if (mSession == null) {

            Log.d("Facebook", "Session null");
            mSession = new Session.Builder(this).setApplicationId(
                    "xxxxxxxxxxxxxxx").build();
            Session.setActiveSession(mSession);

        }
        if (!mSession.isOpened()) {

            Log.d("Facebook", "Session not opened");

            Session.OpenRequest sessionRequest = new Session.OpenRequest(
                    this);

            sessionRequest.setPermissions(PERMISSIONS);
            sessionRequest.setCallback(statuscallback);
            sessionRequest
                    .setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
            mSession.openForPublish(sessionRequest);

        }

    }

StatusCallback statuscallback = new StatusCallback() {

        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
            // TODO Auto-generated method stub

            if (session.isOpened()) {

                Log.d("Facebook", "Logged in");
                postPhoto();

            }
            if (session.isClosed()) {

                Log.d("Facebook", "Logged out");
            }
        }
    };



   @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            Session.getActiveSession().onActivityResult(this, requestCode,
                    resultCode, data);

            Log.d("Facebook", "onActivity Result");
        }

What if I need to logout the user??

I tried the following code onDestroy()

if (mSession.isOpened())
   mSession.closeAndClearTokenInformation();

whenever I open the app next time it always throws an error at this line if I use the above code in destroy method

mSession.openForPublish(sessionRequest);

Please help guys. Answers/Suggestion would be highly appreciable.

Thanks

4

1 回答 1

0

您可以使用 UiLifecycleHelper 帮助管理整个 Activity 或 Fragment 生命周期中的活动会话。通常,所有与会话相关的数据(访问令牌、权限等)都被缓存,如果您创建一个新的 Session(),它将使用缓存的数据(除非您调用 closeAndClearTokenInformation,在这种情况下它会清除缓存)。我只会在用户显式注销时调用 closeAndClearTokenInformation(),否则只需在 onDestroy() 中调用 close()。

我也会避免调用 openForPublish。始终执行 openForRead,然后调用 session.getPermissions() 以检查您是否具有必要的权限,如果需要更多权限,则调用 requestNewPublishPermissions。

于 2013-03-29T17:20:55.963 回答