android - How to get user birthday from facebook -
i used below code facebook user information when he/ login. problem couldn't user birthday or firstname of that. when wanted show user birthday show me empty string.
package com.example.test; import java.security.messagedigest; import java.util.arraylist; import java.util.arrays; import com.facebook.request; import com.facebook.request.graphusercallback; import com.facebook.response; import com.facebook.session; import com.facebook.sessionstate; import com.facebook.uilifecyclehelper; import com.facebook.model.graphuser; import com.facebook.widget.loginbutton; import android.app.activity; import android.app.progressdialog; import android.content.context; import android.content.intent; import android.content.pm.packageinfo; import android.content.pm.packagemanager; import android.content.pm.signature; import android.os.bundle; import android.util.base64; import android.util.log; import android.view.view; import android.widget.toast; public class mainactivity extends activity {public static progressdialog pdialog; private uilifecyclehelper uihelper; loginbutton btn; string id,name,sex ,birthday; void showmsg(string string) { toast.maketext(getapplicationcontext(), string, toast.length_short).show(); } private session.statuscallback callback =new session.statuscallback() { @override public void call(session session, sessionstate state, exception exception) { if (state.isopened()){ intent in = new intent("com.example.test.profile"); bundle bundle = new bundle(); bundle.putstring("fb_id",id); session.getaccesstoken() ; bundle.putstring("fb_bth", birthday); in.putextras(bundle); startactivity(in); btn.post(new runnable(){ @override public void run() { // btn.setvisibility(view.invisible); }}); } onsessionstatechange(session,state,exception); } }; void onsessionstatechange(final session session, sessionstate state, exception exception) { if (state.isopened()) { log.i("facebook", "logged in..."); request.newmerequest(session, new request.graphusercallback() { @override public void oncompleted(graphuser user, response response) { if(user!=null) { intent in = new intent("com.example.test.profile"); bundle bundle =new bundle(); bundle.putstring("fb_id", user.getid()); bundle.putstring("fb_bth", user.getbirthday()); in.putextras(bundle); startactivity(in); id=user.getid(); session.getaccesstoken(); birthday=user.getbirthday(); } else { showmsg("its null"); showmsg(response.geterror().geterrormessage()); } } }).executeasync(); } else if (state.isclosed()) { log.i("facebook", "logged out..."); } } @override protected void onresume() { super.onresume(); uihelper.onresume(); } @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); uihelper.onsaveinstancestate(outstate); } @override protected void onpause() { super.onpause(); uihelper.onpause(); } @override protected void ondestroy() { super.ondestroy(); uihelper.ondestroy(); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); uihelper.onactivityresult(requestcode, resultcode, data); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); uihelper =new uilifecyclehelper(this,callback); uihelper.oncreate(savedinstancestate); arraylist<string> permission =new arraylist<string>(); permission.add("email"); permission.add("public_profile"); permission.add("user_friends"); permission.add("user_birthday"); permission.add("friends_birthday"); btn=(loginbutton)findviewbyid(r.id.fbbtn); btn.setreadpermissions(permission); // btn.setpublishpermissions(permission); // todo auto-generated method stub try { packageinfo info = getpackagemanager().getpackageinfo( "com.example.testing", packagemanager.get_signatures); (signature signature : info.signatures) { messagedigest md = messagedigest.getinstance("sha"); md.update(signature.tobytearray()); log.d("keyhash:", base64.encodetostring(md.digest(), base64.default)); } } catch (exception e) { e.printstacktrace(); } }
}
i have found answer need add permission user graph api. need add @ last.
bundle parameters = new bundle(); parameters.putstring("fields", "email,birthday"); request.setparameters(parameters); request.executeasync(); package com.example.test; import java.security.messagedigest; import java.util.arraylist; import java.util.arrays; import com.facebook.request; import com.facebook.request.graphusercallback; import com.facebook.response; import com.facebook.session; import com.facebook.sessionstate; import com.facebook.uilifecyclehelper; import com.facebook.model.graphuser; import com.facebook.widget.loginbutton; import android.app.activity; import android.app.progressdialog; import android.content.context; import android.content.intent; import android.content.pm.packageinfo; import android.content.pm.packagemanager; import android.content.pm.signature; import android.os.bundle; import android.util.base64; import android.util.log; import android.view.view; import android.widget.toast; public class mainactivity extends activity {public static progressdialog pdialog; private uilifecyclehelper uihelper; loginbutton btn; string id,name,sex ,birthday; void showmsg(string string) { toast.maketext(getapplicationcontext(), string, toast.length_short).show(); } private session.statuscallback callback =new session.statuscallback() { @override public void call(session session, sessionstate state, exception exception) { if (state.isopened()){ intent in = new intent("com.example.test.profile"); bundle bundle = new bundle(); bundle.putstring("fb_id",id); session.getaccesstoken() ; bundle.putstring("fb_bth", birthday); in.putextras(bundle); startactivity(in); btn.post(new runnable(){ @override public void run() { // btn.setvisibility(view.invisible); }}); } onsessionstatechange(session,state,exception); } }; void onsessionstatechange(final session session, sessionstate state, exception exception) { if (state.isopened()) { log.i("facebook", "logged in..."); request.newmerequest(session, new request.graphusercallback() { @override public void oncompleted(graphuser user, response response) { if(user!=null) { intent in = new intent("com.example.test.profile"); bundle bundle =new bundle(); bundle.putstring("fb_id", user.getid()); bundle.putstring("fb_bth", user.getbirthday()); in.putextras(bundle); startactivity(in); id=user.getid(); session.getaccesstoken(); birthday=user.getbirthday(); } else { showmsg("its null"); showmsg(response.geterror().geterrormessage()); } } }) ; bundle parameters = new bundle(); parameters.putstring("fields", "email,birthday"); request.setparameters(parameters); request.executeasync(); } else if (state.isclosed()) { log.i("facebook", "logged out..."); } } @override protected void onresume() { super.onresume(); uihelper.onresume(); } @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); uihelper.onsaveinstancestate(outstate); } @override protected void onpause() { super.onpause(); uihelper.onpause(); } @override protected void ondestroy() { super.ondestroy(); uihelper.ondestroy(); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); uihelper.onactivityresult(requestcode, resultcode, data); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); uihelper =new uilifecyclehelper(this,callback); uihelper.oncreate(savedinstancestate); arraylist<string> permission =new arraylist<string>(); permission.add("email"); permission.add("public_profile"); permission.add("user_friends"); permission.add("user_birthday"); permission.add("friends_birthday"); btn=(loginbutton)findviewbyid(r.id.fbbtn); btn.setreadpermissions(permission); // btn.setpublishpermissions(permission); // todo auto-generated method stub try { packageinfo info = getpackagemanager().getpackageinfo( "com.example.testing", packagemanager.get_signatures); (signature signature : info.signatures) { messagedigest md = messagedigest.getinstance("sha"); md.update(signature.tobytearray()); log.d("keyhash:", base64.encodetostring(md.digest(), base64.default)); } } catch (exception e) { e.printstacktrace(); } } }
Comments
Post a Comment