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 https //www back4app com/product/parse server 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 https //github com/back4app/android geopoints tutorial prerequisites to complete this tutorial, we need android studio https //developer android com/studio/index html an app created on back4app note follow the new parse app tutorial https //www back4app com/docs/get started/new parse app to learn how to create a parse app on back4app an android app connected to back4app note follow the install parse sdk tutoria https //www back4app com/docs/android/parse android sdk l to create an android studio project connected to back4app a device (or virtual device https //developer android com/studio/run/managing avds html ) 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 https //apps twitter com/ , sign in with a twitter account and click on \<font color="#2166ae">create new app\</font> fill in the \<font color="#2166ae">application details\</font> when asked to specify \<font color="#2166ae">callback urls\</font> , please insert \<font color="#2166ae">twittersdk //\</font> this is mandatory in order to enable authentication through twitter 3\ click on the \<font color="#2166ae">developer agreement\</font> and then on \<font color="#2166ae">create your twitter application\</font> 4\ open your android studio project, find your \<font color="#2166ae">build gradle (module app)\</font> and in the \<font color="#2166ae">dependencies{}\</font> section add the following code to install the parse twitter utils sdk for android 1 // don't forget to change the line below with the latest version of parse twitter utils sdk for android 2 implementation 'com github parse community\ parsetwitterutils android\ latest version here' 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 https //jitpack io/ , following these steps at jitpack website paste \<font color="#2166ae">parse community/parsetwitterutils android\</font> in the \<font color="#2166ae">git repo url\</font> box after doing that, click on the \<font color="#2166ae">look up\</font> 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 \<font color="#2166ae">app\</font> that extends application that you created to initialize the parse sdk, on its \<font color="#2166ae">oncreate\</font> method, right after \<font color="#2166ae">parse initialize()\</font> 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 \<font color="#2166ae">app java\</font> file as described in this step, access the install parse sdk for android 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 \<font color="#2166ae">app\</font> > \<font color="#2166ae">res\</font> > \<font color="#2166ae">values\</font> > \<font color="#2166ae">strings xml\</font> file in the \<font color="#2166ae">strings xml\</font> file add the following code \<! change the following strings as required > \<stringname="twitter consumer key">paste your twitter consumer key\</string>\<string name="twitter consumer secret">paste your twitter consumer secret\</string> 2\ leave the \<font color="#2166ae">string xml\</font> opened and go to back4app website, log in and click on \<font color="#2166ae">my apps\</font> find your app and then click on \<font color="#2166ae">server settings\</font> find the “twitter login” block and click on \<font color="#2166ae">settings\</font> the “twitter login” block looks like this 2\ leave the back4app twitter login page you visited opened and go to twitter application management website https //apps twitter com/ 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 \<font color="#2166ae">consumer key (api key)\</font> and the \<font color="#2166ae">consumer secret (api secret)\</font> and paste it in the \<font color="#2166ae">strings xml\</font> file of your android studio project 4 log in import to your \<font color="#2166ae">loginactivity\</font> 1 import android app alertdialog ; 2 import android app progressdialog ; 3 import android content dialoginterface ; 4 import android content intent ; 5 import android support v7 app appcompatactivity ; 6 import android os bundle ; 7 import android view\ view ; 8 import android util log ; 9 import android widget button ; 10 import android widget toast ; 11 12 import com parse logincallback ; 13 import com parse parseexception ; 14 import com parse twitter parsetwitterutils ; 15 import com parse parseuser ; 16 import com parse savecallback ; 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 \<font color="#2166ae">login via twitter\</font> 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 \<font color="#2166ae">loginactivity\</font> 1 import android app alertdialog ; 2 import android app progressdialog ; 3 import android content dialoginterface ; 4 import android content intent ; 5 import android support v7 app appcompatactivity ; 6 import android os bundle ; 7 import android view\ view ; 8 import android widget button ; 9 10 import com parse parseuser ; 2\ to implement twitter logout, simply use the code below 1 parseuser logout (); 2 alertdisplayer ( "so, you're going " , "ok bye bye then" ); in the example project, this code is placed inside a \<font color="#2166ae">logout via twitter\</font> button callback the method \<font color="#2166ae">alertdisplayer\</font> is the same that you added in the \<font color="#2166ae">loginactivity\</font> , just remember to change the \<font color="#2166ae">intent\</font> 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!