i trying login on fb latest fb sdk.
here code
session currentsession = session.getactivesession(); if (currentsession == null || currentsession.getstate().isclosed()) { session session = new session.builder(v.getcontext()) .setapplicationid(getresources().getstring(r.string.app_id)) .build(); session.setactivesession(session); currentsession = session; } if (!currentsession.isopened()) { session.openrequest openrequest = null; openrequest = new session.openrequest(fblogin.this); if (openrequest != null) { openrequest.setdefaultaudience(sessiondefaultaudience.everyone); openrequest.setpermissions(permissions); openrequest.setloginbehavior(sessionloginbehavior.sso_with_fallback); currentsession.addcallback(callback); currentsession.openforread(openrequest); } } @override public void onactivityresult(int requestcode, int resultcode, intent data) { if (resultcode == constants.result_ok) { finish(); } else session.getactivesession().onactivityresult(this, requestcode, resultcode, data); } private session.statuscallback callback = new session.statuscallback() { public void call(session session, sessionstate state, exception exception) { onsessionstatechange(session, state, exception); } }; private void onsessionstatechange(session session, sessionstate state, exception exception) { if (state.isopened()) { registerwithserver(session); } else if (state.isclosed()) { mdialog.dismiss(); utilities .displayalert( "oops!", "could not connect facebook. try removing application" + "from app dashboard", fblogin.this); } }
the code works fine when first try run it.but if app uninstalled or reinstalled , try login, opens fb page, asks permissions , gives facebookexception
, invalid token.
the way fix "try removing application app dashboard"
what going on? how fb tokens work? on reauthorizing should create new token instead of failing right?
try code facebook login session.openactivesession(this, true, new session.statuscallback() { // callback when session changes state @override public void call(final session session, sessionstate state, exception exception) { if (session.isopened()) { if(!session.getpermissions().contains("email")) { session.requestnewreadpermissions(new session.newpermissionsrequest(facebookpersil.this, permissions)); } // make request /me api request.executemerequestasync(session, new request.graphusercallback() { // callback after graph api response user object @override public void oncompleted(graphuser user, response response) { if (user != null) { try { log.w("",""+user.asmap()); name= user.getname(); // location=user.getlocation().getcity(); nickname=user.asmap().get("username").tostring(); email=user.asmap().get("email").tostring(); string id=user.getid(); url img_value = null; try { img_value = new url("http://graph.facebook.com/"+id+"/picture?type=small"); } catch (malformedurlexception e) { // todo auto-generated catch block e.printstacktrace(); } fbprofileimage=img_value.tostring(); session.close(); if(email.length()==0) { toast("unexpected error. please try again"); finish(); } else{ new loginauth().execute(); } } catch(exception e) { log.w("error", e.tostring()); } } } }); } } }); @override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); session.getactivesession().onactivityresult(this, requestcode, resultcode, data); }
Comments
Post a Comment