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);
}
}
}
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.
I hope you already added below line in
onActivityResult()
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()
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: