java - Authentication with Twitter4j Android -
i trying post tweet after being authenticated twitter4j , walking on this tutorial. problem login button doesn't while has open authentication page. couldn't find reason. help.
mainactivity:
public class mainactivity extends activity { // constants static string twitter_consumer_key = "my_api_key"; static string twitter_consumer_secret = "my_api_secret"; // preference constants static string preference_name = "twitter_oauth"; static final string pref_key_oauth_token = "oauth_token"; static final string pref_key_oauth_secret = "oauth_token_secret"; static final string pref_key_twitter_login = "istwitterlogedin"; static final string twitter_callback_url = "oauth://t4jsample"; // twitter oauth urls static final string url_twitter_auth = "auth_url"; static final string url_twitter_oauth_verifier = "oauth_verifier"; static final string url_twitter_oauth_token = "oauth_token"; // login button button btnlogintwitter; // update status button button btnupdatestatus; // logout button button btnlogouttwitter; // edittext update edittext txtupdate; // lbl update textview lblupdate; textview lblusername; // progress dialog progressdialog pdialog; // twitter private static twitter twitter; private static requesttoken requesttoken; // shared preferences private static sharedpreferences msharedpreferences; // internet connection detector private connectiondetector cd; // alert dialog manager alertdialogmanager alert = new alertdialogmanager(); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); setrequestedorientation(activityinfo.screen_orientation_portrait); cd = new connectiondetector(getapplicationcontext()); // check if internet present if (!cd.isconnectingtointernet()) { // internet connection not present alert.showalertdialog(mainactivity.this, "internet connection error", "please connect working internet connection", false); // stop executing code return return; } // check if twitter keys set if(twitter_consumer_key.trim().length() == 0 || twitter_consumer_secret.trim().length() == 0){ // internet connection not present alert.showalertdialog(mainactivity.this, "twitter oauth tokens", "please set twitter oauth tokens first!", false); // stop executing code return return; } // ui elements btnlogintwitter = (button) findviewbyid(r.id.btnlogintwitter); btnupdatestatus = (button) findviewbyid(r.id.btnupdatestatus); btnlogouttwitter = (button) findviewbyid(r.id.btnlogouttwitter); txtupdate = (edittext) findviewbyid(r.id.txtupdatestatus); lblupdate = (textview) findviewbyid(r.id.lblupdate); lblusername = (textview) findviewbyid(r.id.lblusername); // shared preferences msharedpreferences = getapplicationcontext().getsharedpreferences( "mypref", 0); /** * twitter login button click event call logintotwitter() function * */ btnlogintwitter.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { // call login twitter function logintotwitter(); } }); /** * button click event update status, call updatetwitterstatus() * function * */ btnupdatestatus.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // call update status function // status edittext string status = txtupdate.gettext().tostring(); // check blank text if (status.trim().length() > 0) { // update status new updatetwitterstatus().execute(status); } else { // edittext empty toast.maketext(getapplicationcontext(), "please enter status message", toast.length_short) .show(); } } }); /** * button click event logout twitter * */ btnlogouttwitter.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { // call logout twitter function logoutfromtwitter(); } }); /** if conditions tested once * redirected twitter page. parse uri oauth * verifier * */ if (!istwitterloggedinalready()) { uri uri = getintent().getdata(); if (uri != null && uri.tostring().startswith(twitter_callback_url)) { // oauth verifier string verifier = uri .getqueryparameter(url_twitter_oauth_verifier); try { // access token accesstoken accesstoken = twitter.getoauthaccesstoken( requesttoken, verifier); // shared preferences editor e = msharedpreferences.edit(); // after getting access token, access token secret // store them in application preferences e.putstring(pref_key_oauth_token, accesstoken.gettoken()); e.putstring(pref_key_oauth_secret, accesstoken.gettokensecret()); // store login status - true e.putboolean(pref_key_twitter_login, true); e.commit(); // save changes log.e("twitter oauth token", "> " + accesstoken.gettoken()); // hide login button btnlogintwitter.setvisibility(view.gone); // show update twitter lblupdate.setvisibility(view.visible); txtupdate.setvisibility(view.visible); btnupdatestatus.setvisibility(view.visible); btnlogouttwitter.setvisibility(view.visible); // getting user details twitter // getting name long userid = accesstoken.getuserid(); user user = twitter.showuser(userid); string username = user.getname(); // displaying in xml ui lblusername.settext(html.fromhtml("<b>welcome " + username + "</b>")); } catch (exception e) { // check log login errors log.e("twitter login error", "> " + e.getmessage()); } } } } /** * function login twitter * */ private void logintotwitter() { // check if logged in if (!istwitterloggedinalready()) { configurationbuilder builder = new configurationbuilder(); builder.setoauthconsumerkey(twitter_consumer_key); builder.setoauthconsumersecret(twitter_consumer_secret); configuration configuration = builder.build(); twitterfactory factory = new twitterfactory(configuration); twitter = factory.getinstance(); try { requesttoken = twitter .getoauthrequesttoken(twitter_callback_url); this.startactivity(new intent(intent.action_view, uri .parse(requesttoken.getauthenticationurl()))); } catch (twitterexception e) { e.printstacktrace(); } } else { // user logged twitter toast.maketext(getapplicationcontext(), "already logged twitter", toast.length_long).show(); } } /** * function update status * */ class updatetwitterstatus extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(mainactivity.this); pdialog.setmessage("updating twitter..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); } /** * getting places json * */ protected string doinbackground(string... args) { log.d("tweet text", "> " + args[0]); string status = args[0]; try { configurationbuilder builder = new configurationbuilder(); builder.setoauthconsumerkey(twitter_consumer_key); builder.setoauthconsumersecret(twitter_consumer_secret); // access token string access_token = msharedpreferences.getstring(pref_key_oauth_token, ""); // access token secret string access_token_secret = msharedpreferences.getstring(pref_key_oauth_secret, ""); accesstoken accesstoken = new accesstoken(access_token, access_token_secret); twitter twitter = new twitterfactory(builder.build()).getinstance(accesstoken); // update status twitter4j.status response = twitter.updatestatus(status); log.d("status", "> " + response.gettext()); } catch (twitterexception e) { // error in updating status log.d("twitter update error", e.getmessage()); } return null; } /** * after completing background task dismiss progress dialog , show * data in ui use runonuithread(new runnable()) update ui * background thread, otherwise error * **/ protected void onpostexecute(string file_url) { // dismiss dialog after getting products pdialog.dismiss(); // updating ui background thread runonuithread(new runnable() { @override public void run() { toast.maketext(getapplicationcontext(), "status tweeted successfully", toast.length_short) .show(); // clearing edittext field txtupdate.settext(""); } }); } } /** * function logout twitter * clear application shared preferences * */ private void logoutfromtwitter() { // clear shared preferences editor e = msharedpreferences.edit(); e.remove(pref_key_oauth_token); e.remove(pref_key_oauth_secret); e.remove(pref_key_twitter_login); e.commit(); // after take appropriate action // showing hiding/showing buttons again // might not needed code btnlogouttwitter.setvisibility(view.gone); btnupdatestatus.setvisibility(view.gone); txtupdate.setvisibility(view.gone); lblupdate.setvisibility(view.gone); lblusername.settext(""); lblusername.setvisibility(view.gone); btnlogintwitter.setvisibility(view.visible); } /** * check user logged in application using twitter login flag * fetched shared preferences * */ private boolean istwitterloggedinalready() { // return twitter login status shared preferences return msharedpreferences.getboolean(pref_key_twitter_login, false); } protected void onresume() { super.onresume(); } }
error log:
03-20 12:12:56.779: e/audiomtkhardware(8976): setparameters() still have param.size() = 1, remain param = "screen_state=off" 03-20 12:13:01.714: e/looper(494): warning: looper class instance count has on limit(100). there should leakage of looper or handlerthread. 03-20 12:13:01.715: e/looper(494): looper class instance count = 113 03-20 12:13:01.715: e/looper(494): current thread name: keyguardwidgetpager worker 03-20 12:13:05.991: e/stp_dump(147): table == null || table->table == null 03-20 12:13:05.991: e/stp_dump(147): table == null || table->table == null 03-20 12:13:05.997: e/celllocation(976): create gsmcelllocation 03-20 12:13:06.014: e/celllocation(976): create gsmcelllocation 03-20 12:13:06.468: e/wpa_supplicant(29010): sorted scan results 03-20 12:13:06.468: e/wpa_supplicant(29010): 28:10:7b:94:c5:79 freq=2437 qual=0 noise=0 level=-78 flags=0x2b 03-20 12:13:06.468: e/wpa_supplicant(29010): ies 03-20 12:13:06.468: e/wpa_supplicant(29010): 000854454e454b454349010882848b962430486c0301062a01042f010430140100000fac040100000fac040100000fac020c0032040c1218602d1a6c181bff000000000000000000000000000000000000000000003d1606000000000000000000000000000000000000000000dd7b0050f204104a00011010440001021041000100103b0001031047001076c42cdafbfc3780d3ea25f0dc470d6f1021000842726f6164636f6d1023000842726f6164636f6d1024000631323334353610420004313233341054000800060050f20400011011000a42726f6164636f6d4150100800020084103c000101dd090010180201f0040000dd180050f2020101800003a4000027a4000042435e0062322f00 03-20 12:13:06.468: e/wpa_supplicant(29010): beacon ies 03-20 12:13:06.468: e/wpa_supplicant(29010): 000854454e454b454349010882848b962430486c0301060504000100002a01002f010030140100000fac040100000fac040100000fac020c0032040c1218602d1a6c181bff000000000000000000000000000000000000000000003d1606000000000000000000000000000000000000000000dd0e0050f204104a0001101044000102dd090010180200f0040000dd180050f2020101800003a4000027a4000042435e0062322f00 03-20 12:13:06.468: e/wpa_supplicant(29010): f8:1a:67:b8:c4:be freq=2457 qual=0 noise=0 level=-87 flags=0xb 03-20 12:13:06.468: e/wpa_supplicant(29010): ies 03-20 12:13:06.468: e/wpa_supplicant(29010): 001154544e45545f54504c494e4b5f43344245010882848b960c12182403010a2a010032043048606c2d1a6e181effff0000000000000000000000000000000000000000003d160a0000000000000000000000000000000000000000004a0e14000a00b400c8001400050019007f0101dd1a0050f20101000050f20202000050f2020050f20401000050f20230180100000fac020200000fac02000fac040100000fac020000dd180050f2020101000003a4000027a4000042435e0062322f00dd1e00904c336e181effff000000000000000000000000000000000000000000dd1a00904c340a000000000000000000000000000000000000000000dd0600e04c020160dda40050f204104a0001101044000102103b0001031047001063041253101920061228f81a67b8c4be1021001b5265616c74656b2053656d69636f6e647563746f7220436f72702e1023000752544c383637311024000d45562d323030362d30372d32371042000f3132333435363738393031323334371054000800060050f20400011011000f54502d4c494e4b2050726f647563741008000200061049000600372a000120 03-20 12:13:06.468: e/wpa_supplicant(29010): beacon ies 03-20 12:13:06.468: e/wpa_supplicant(29010): 001154544e45545f54504c494e4b5f43344245010882848b960c12182403010a0504000101002a010432043048606c2d1a6e181effff0000000000000000000000000000000000000000003d160a0000000000000000000000000000000000000000004a0e14000a00b400c8001400050019007f0101dd1a0050f20101000050f20202000050f2020050f20401000050f20230180100000fac020200000fac02000fac040100000fac020000dd180050f2020101000003a4000027a4000042435e0062322f00dd1e00904c336e181effff000000000000000000000000000000000000000000dd1a00904c340a000000000000000000000000000000000000000000dd0600e04c020160dd180050f204104a00011010440001021049000600372a000120 03-20 12:13:06.468: e/wpa_supplicant(29010): 64:66:b3:ed:de:26 freq=2472 qual=0 noise=0 level=-90 flags=0xb 03-20 12:13:06.468: e/wpa_supplicant(29010): ies 03-20 12:13:06.469: e/stp_dump(147): table == null || table->table == null 03-20 12:13:06.469: e/stp_dump(147): table == null || table->table == null 03-20 12:13:06.470: e/wpa_supplicant(29010): 00074d757a65203335010882848b961224486c03010d2a010432040c1830602d1a6e1117ff000000010000000000000000000000000c00000000003d160d0700000000000000000000000000000000000000003e010030180100000fac020200000fac02000fac040100000fac020000dd180050f2020101000003a4000027a4000042435e0062322f004a0e14000a002c01c800140005001900dd07000c43040000000706545200010d10dd9d0050f204104a0001001044000102103b00010310470010bc329e001dd811b286016466b3edde261021001852616c696e6b20546563686e6f6c6f67792c20436f72702e1023001c52616c696e6b20576972656c6573732041636365737320506f696e74102400065254323836301042000831323334353637381054000800060050f20400011011000952616c696e6b415053100800020004103c000100 03-20 12:13:06.470: e/wpa_supplicant(29010): beacon ies 03-20 12:13:06.471: e/wpa_supplicant(29010): 00074d757a65203335010882848b961224486c03010d32040c1830600706545200010d143308200102030405060733082105060708090a0b050400010000dd270050f204104a000100104400010210470010bc329e001dd811b286016466b3edde26103c0001012a01042d1a6e1117ff000000010000000000000000000000000c00000000003d160d0700000000000000000000000000000000000000004a0e14000a002c01c80014000500190030180100000fac020200000fac02000fac040100000fac020000dd180050f2020101000003a4000027a4000042435e0062322f00dd07000c4304000000 03-20 12:13:06.483: e/wpa_supplicant(29010): wps: wfa subelement id=0 len=1 03-20 12:13:06.483: e/wpa_supplicant(29010): wps: wfa subelement id=0 len=1 03-20 12:13:06.483: e/wpa_supplicant(29010): wps: wfa subelement id=0 len=1 03-20 12:14:06.019: e/stp_dump(147): table == null || table->table == null 03-20 12:14:06.019: e/stp_dump(147): table == null || table->table == null 03-20 12:14:06.026: e/celllocation(976): create gsmcelllocation 03-20 12:14:06.042: e/celllocation(976): create gsmcelllocation 03-20 12:14:06.506: e/wpa_supplicant(29010): sorted scan results 03-20 12:14:06.506: e/wpa_supplicant(29010): 28:10:7b:94:c5:79 freq=2437 qual=0 noise=0 level=-84 flags=0x2b 03-20 12:14:06.506: e/wpa_supplicant(29010): ies 03-20 12:14:06.506: e/wpa_supplicant(29010): 000854454e454b454349010882848b962430486c0301062a01042f010430140100000fac040100000fac040100000fac020c0032040c1218602d1a6c181bff000000000000000000000000000000000000000000003d1606000000000000000000000000000000000000000000dd7b0050f204104a00011010440001021041000100103b0001031047001076c42cdafbfc3780d3ea25f0dc470d6f1021000842726f6164636f6d1023000842726f6164636f6d1024000631323334353610420004313233341054000800060050f20400011011000a42726f6164636f6d4150100800020084103c000101dd090010180201f0040000dd180050f2020101800003a4000027a4000042435e0062322f00 03-20 12:14:06.506: e/wpa_supplicant(29010): beacon ies 03-20 12:14:06.506: e/wpa_supplicant(29010): 000854454e454b454349010882848b962430486c0301060504000100002a01002f010030140100000fac040100000fac040100000fac020c0032040c1218602d1a6c181bff000000000000000000000000000000000000000000003d1606000000000000000000000000000000000000000000dd0e0050f204104a0001101044000102dd090010180200f0040000dd180050f2020101800003a4000027a4000042435e0062322f00 03-20 12:14:06.506: e/wpa_supplicant(29010): 64:66:b3:ed:de:26 freq=2472 qual=0 noise=0 level=-89 flags=0xb 03-20 12:14:06.506: e/wpa_supplicant(29010): ies 03-20 12:14:06.506: e/wpa_supplicant(29010): 00074d757a65203335010882848b961224486c03010d2a010432040c1830602d1a6e1117ff000000010000000000000000000000000c00000000003d160d0700000000000000000000000000000000000000003e010030180100000fac020200000fac02000fac040100000fac020000dd180050f2020101000003a4000027a4000042435e0062322f004a0e14000a002c01c800140005001900dd07000c43040000000706545200010d10dd9d0050f204104a0001001044000102103b00010310470010bc329e001dd811b286016466b3edde261021001852616c696e6b20546563686e6f6c6f67792c20436f72702e1023001c52616c696e6b20576972656c6573732041636365737320506f696e74102400065254323836301042000831323334353637381054000800060050f20400011011000952616c696e6b415053100800020004103c000100 03-20 12:14:06.506: e/wpa_supplicant(29010): beacon ies 03-20 12:14:06.506: e/wpa_supplicant(29010): 00074d757a65203335010882848b961224486c03010d32040c1830600706545200010d143308200102030405060733082105060708090a0b050400010000dd270050f204104a000100104400010210470010bc329e001dd811b286016466b3edde26103c0001012a01042d1a6e1117ff000000010000000000000000000000000c00000000003d160d0700000000000000000000000000000000000000004a0e14000a002c01c80014000500190030180100000fac020200000fac02000fac040100000fac020000dd180050f2020101000003a4000027a4000042435e0062322f00dd07000c4304000000 03-20 12:14:06.506: e/wpa_supplicant(29010): 00:27:22:82:e8:21 freq=2437 qual=0 noise=0 level=-93 flags=0xb 03-20 12:14:06.506: e/wpa_supplicant(29010): ies 03-20 12:14:06.507: e/wpa_supplicant(29010): 00066b6174322d31010882848b960c1218240301060504000100002a0100dd26000c42000000011e000000001f660902ff0f55424e540000000000000000000000000000000032043048606cdd180050f202010182000364000027a4000041435e0061322f00dd1e00904c334c101bffff0000000000000000000000000000000000000000002d1a4c101bffff000000000000000000000000000000000000000000dd1a00904c3406001b000000000000000000000000000000000000003d1606001b00000000000000000000000000000000000000dd0900037f01010000ff7fdd0a00037f04010002004000dd0e00156d000000010212e002021200 03-20 12:14:06.507: e/stp_dump(147): table == null || table->table == null 03-20 12:14:06.507: e/stp_dump(147): table == null || table->table == null 03-20 12:14:06.518: e/wpa_supplicant(29010): wps: wfa subelement id=0 len=1 03-20 12:14:06.518: e/wpa_supplicant(29010): wps: wfa subelement id=0 len=1 03-20 12:14:06.518: e/wpa_supplicant(29010): wps: wfa subelement id=0 len=1
edit:
after updated jar file last version (twitter4j 4.0.1) there no more log errors warnings , button still doesn't work. here new error log.
Comments
Post a Comment