Android
Users
Sign in with Twitter
17 min
how to add twitter login to your android app introduction this section explains how you can create an app with user registration using twitter login and parse server core features through back4app it will look like this at any time, you can access the complete android project built with this tutorial at our github repository prerequisites to complete this tutorial, we need android studio an app created on back4app note follow the new parse app tutorial to learn how to create a parse app on back4app an android app connected to back4app note follow the install parse sdk tutoria l to create an android studio project connected to back4app a device (or virtual device ) running android 4 1 (jelly bean) or newer 1 twitter set up to start using twitter functions, you need to go to twitter application management website , sign in with a twitter account and click on create new app create new app fill in the application details application details when asked to specify callback urls callback urls , please insert twittersdk // twittersdk // this is mandatory in order to enable authentication through twitter 3\ click on the developer agreement developer agreement and then on create your twitter application create your twitter application 4\ open your android studio project, find your build gradle (module app) build gradle (module app) and in the dependencies{} dependencies{} section add the following code to install the parse twitter utils sdk for android remember to update the version of parse facebook utils sdk for android to the latest one you can find out which is the latest version at the jitpack website , following these steps at jitpack website paste parse community/parsetwitterutils android parse community/parsetwitterutils android in the git repo url git repo url box after doing that, click on the look up look up button then you should see the available versions of parse twitter utils sdk for android, as shown in the following image 2 link your twitter app with back4app in your android studio project, in the java file called app app that extends application that you created to initialize the parse sdk, on its oncreate oncreate method, right after parse initialize() parse initialize() call, use the following code to initialize parse twitter utils sdk 1 parsetwitterutils initialize(getstring(r string twitter consumer key), getstring(r string twitter consumer secret)); if you don’t have an app java app java file as described in this step, access the https //www back4app com/docs/android/parse android sdk documentation and make sure that you have followed all the steps required to install parse sdk correctly if you do not install parse sdk properly your facebook login with parse will not work 2\ go to app app > res res > values values > strings xml strings xml file in the strings xml strings xml file add the following code 2\ leave the string xml string xml opened and go to back4app website, log in and click on my apps my apps find your app and then click on server settings server settings find the “twitter login” block and click on settings settings the “twitter login” block looks like this 2\ leave the back4app twitter login page you visited opened and go to twitter application management website find your app and click on its name 3\ click on keys and access tokens, copy the consumer key (api key) and the consumer secret (api secret) and paste it in the back4app twitter login page, filling in the respective fields to finish just click on save the consumer key (api key) and the consumer secret (api secret) looks like this 4\ also, copy the consumer key (api key) consumer key (api key) and the consumer secret (api secret) consumer secret (api secret) and paste it in the strings xml strings xml file of your android studio project 4 log in import to your loginactivity loginactivity 2\ to implement twitter login, simply use below code 1 parsetwitterutils login(loginactivity this, new logincallback() { 2 3 @override 4 public void done(final parseuser user, parseexception err) { 5 if (err != null) { 6 dlg dismiss(); 7 parseuser logout(); 8 log e("err", "err", err); 9 } 10 if (user == null) { 11 dlg dismiss(); 12 parseuser logout(); 13 toast maketext(loginactivity this, "the user cancelled the twitter login ", toast length long) show(); 14 log d("myapp", "uh oh the user cancelled the twitter login "); 15 } else if (user isnew()) { 16 dlg dismiss(); 17 toast maketext(loginactivity this, "user signed up and logged in through twitter ", toast length long) show(); 18 log d("myapp", "user signed up and logged in through twitter!"); 19 user setusername(parsetwitterutils gettwitter() getscreenname()); 20 user saveinbackground(new savecallback() { 21 @override 22 public void done(parseexception e) { 23 if (null == e) { 24 alertdisplayer("first tome login!", "welcome!"); 25 } else { 26 parseuser logout(); 27 toast maketext(loginactivity this, "it was not possible to save your username ", toast length long) show(); 28 } 29 } 30 }); 31 } else { 32 dlg dismiss(); 33 toast maketext(loginactivity this, "user logged in through twitter ", toast length long) show(); 34 log d("myapp", "user logged in through twitter!"); 35 alertdisplayer("oh, you!","welcome back!"); 36 } 37 } 38 }); in the example project, this code is placed inside a login via twitter login via twitter button callback 3\ it’s interesting to add some method to display alert dialogs and make the process look more professional the method below does this 1 private void alertdisplayer(string title,string message){ 2 alertdialog builder builder = new alertdialog builder(loginactivity this) 3 settitle(title) 4 setmessage(message) 5 setpositivebutton("ok", new dialoginterface onclicklistener() { 6 @override 7 public void onclick(dialoginterface dialog, int which) { 8 dialog cancel(); 9 // don't forget to change the line below with the names of your activities 10 intent intent = new intent(loginactivity this, logoutactivity class); 11 intent addflags(intent flag activity clear task | intent flag activity new task); 12 startactivity(intent); 13 } 14 }); 15 alertdialog ok = builder create(); 16 ok show(); 17 } 5 log out import to your loginactivity loginactivity 2\ to implement twitter logout, simply use the code below in the example project, this code is placed inside a logout via twitter logout via twitter button callback the method alertdisplayer alertdisplayer is the same that you added in the loginactivity loginactivity , just remember to change the intent intent arguments in the strings xml file of your android studio project it’s done! at this stage, you can log in, register and log out of your app with twitter using parse server core features through back4app!