Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Facebook session issue

I have used following code for Facebook authentication and it is working fine, but when I have cancelled the authentication and tried to authenticate again the app crashed and the log was Caused by: java.lang.UnsupportedOperationException: Session: an attempt was made to open an already opened session.
On debug I got like this

{Session state:CREATED, token:{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[]}, appId:xxxxxxxxxxxxxxx}

How can I solve this issue.Please help me.Thanks in advance

private void askFacebbokAuthentication() {

        Session session = Session.getActiveSession();
        if (session.isOpened()) {

            facebook = true;
            if (!hasPublishPermission()) {

                session.requestNewPublishPermissions(new NewPermissionsRequest(
                        MyActivity.this, PERMISSIONS));

            }
        } else {
            Session.OpenRequest openRequest = null;
            openRequest = new Session.OpenRequest(MyActivity.this);
            if (openRequest != null) {

                openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS);

                if (!hasPublishPermission()) {
                    openRequest.setPermissions(PERMISSIONS);
                }
                session.openForPublish(openRequest);
            }
        }
    }
like image 560
user1767260 Avatar asked May 22 '13 09:05

user1767260


People also ask

Why does my Facebook keep saying session?

Facebook uses sessions to validate that your account is within its service. It could be when you play some games or use the Facebook app. Sessions rely on cached information. Once the cache is cleared, the session ends.

Why does it keep saying my session has expired?

If your Internet connection is unstable, periodically disconnecting and reconnecting, it can cause a website session to expire. When the Internet connection is lost the website connection can be terminated, resulting in a session expired message if you try to access any page after the Internet reconnects.


2 Answers

I too had same problem, but i solved with these lines. To my knowledge we cannot request a session for new permissions which is already opened.

Session session = new Session(this);
    Session.setActiveSession(session);
    session.openForRead(new Session.OpenRequest(this).setCallback(callback).setPermissions(Arrays.asList("your_permissions")));

I hope you already added below line in onActivityResult()

Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
like image 76
Aswin Avatar answered Sep 20 '22 02:09

Aswin


If the Session is neither opened nor closed, I think it is better to Session.openActiveSession()

This snipped is copied-pasted from the Facebook SDK sample project SessionLoginSample, LoginUsingActivityActivity#onClickLogin()

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

Note that Session#openActiveSession() also creates a Session under the hood, which is OK. From https://developers.facebook.com/docs/technical-guides/iossdk/session/#lifecycle:

Sessions can only be opened once. When a session is closed, it cannot be re-opened. Instead, a new session should be created. Typical apps will only require one active session at any time. The Facebook SDK provides static active session methods that take care of opening new session instances.

like image 25
GaRRaPeTa Avatar answered Sep 21 '22 02:09

GaRRaPeTa