Problem with Android Facebook

I used the following code to authenticate Facebook, and it works fine, but when I canceled the authentication and tried to authenticate again, the application crashed and the log was called: java.lang.UnsupportedOperationException: Session: an attempt was made to open an already opened session.
On debugging I got this

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

How can I solve this problem. 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); } } } 
+6
source share
2 answers

I had the same problem too, but I solved with these lines. As far as I know, we cannot request a session for new permissions that are already open.

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

Hope you already added the line below in onActivityResult()

 Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); 
+13
source

If the session is not open or closed, I think Session.openActiveSession () is better

This cut is copied, copied from the sample SDK project for Facebook 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 fine. 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 reopened. Instead, a new session should be created. Typical applications will require only one active session at any time. The Facebook SDK provides static active session methods that take care of opening new session instances.

+3
source

Source: https://habr.com/ru/post/945563/


All Articles