Site icon C1CTech

Android Twitter Integration Example

&NewLine;<p>This post is about how to implement Twitter Integration in our Android app and let users do Sign In with a single click&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<div class&equals;"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-a89b3969 wp-block-buttons-is-layout-flex"> &NewLine;<div class&equals;"wp-block-button"><a class&equals;"wp-block-button&lowbar;&lowbar;link has-white-color has-text-color has-background" style&equals;"background-color&colon; &num;020252&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;TwitLoginExp"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div> &NewLine;<&sol;div> &NewLine;<div> <&sol;div> &NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"FSK8zE10Uys" title&equals;"Android Twitter Integration Example"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;FSK8zE10Uys"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;FSK8zE10Uys&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Android Twitter Integration Example"><&sol;a><&sol;amp-youtube><&sol;p> &NewLine;<p>Integrating Twitter API in the Android app requires the app <span style&equals;"color&colon; &num;008000&semi;"><strong>Consumer Key &lpar;API Key&rpar;<&sol;strong> <&sol;span>and<span style&equals;"color&colon; &num;008000&semi;"> <strong>Consumer Secret &lpar;API Secret&rpar;<&sol;strong>&period;<&sol;span><&sol;p> &NewLine;<h4 class&equals;"h3"><span style&equals;"color&colon; &num;000080&semi;"><strong>Steps to generate Twitter API Key and API Secret<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>1&period; Login at <a href&equals;"https&colon;&sol;&sol;apps&period;twitter&period;com&sol;"><strong><span style&equals;"color&colon; &num;0000ff&semi;">https&colon;&sol;&sol;apps&period;twitter&period;com&sol;<&sol;span> <&sol;strong><&sol;a>with your Twitter account and click on <span style&equals;"color&colon; &num;008000&semi;"><strong>&&num;8216&semi;Create App&&num;8217&semi;<&sol;strong><&sol;span>&period;<&sol;p> &NewLine;<p>2&period; Fill in the application details and get keys and tokens related to your project&comma; then click on <span style&equals;"color&colon; &num;0000ff&semi;"><strong>&&num;8216&semi;App Settings<&sol;strong><&sol;span>&&num;8216&semi;&period;<&sol;p> &NewLine;<p><img class&equals;"alignnone size-full wp-image-3087" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;02&sol;create&lowbar;app-e1644659272732&period;png" alt&equals;"" width&equals;"585" height&equals;"377" &sol;><&sol;p> &NewLine;<p>&nbsp&semi;<&sol;p> &NewLine;<p>3&period; Under <span style&equals;"color&colon; &num;0000ff&semi;"><strong>User authentication settings<&sol;strong><&sol;span> click on <span style&equals;"color&colon; &num;008000&semi;"><strong>set up<&sol;strong><&sol;span>&period;<&sol;p> &NewLine;<p>4&period; Enable the authentication methods according to your requirement as shown below&colon;<&sol;p> &NewLine;<p><img class&equals;"alignnone wp-image-3083" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;02&sol;auth&lowbar;page&period;png" alt&equals;"" width&equals;"603" height&equals;"444" &sol;><&sol;p> &NewLine;<p>5&period; Enter App type&comma; enable <span style&equals;"color&colon; &num;008000&semi;"><strong>request email from users<&sol;strong><&sol;span> only if you want to request user&&num;8217&semi;s email in your app and also select app permission according to your requirement&period;<&sol;p> &NewLine;<p><img class&equals;"alignnone wp-image-3084" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;02&sol;auth&lowbar;next&period;png" alt&equals;"" width&equals;"582" height&equals;"529" &sol;><&sol;p> &NewLine;<p>6&period; Enter your website URL and enter <span style&equals;"color&colon; &num;008000&semi;"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>twittersdk&colon;&sol;&sol; <&sol;strong><&sol;span><span style&equals;"color&colon; &num;333333&semi;">in<&sol;span><strong> Callback URI <&sol;strong><span style&equals;"color&colon; &num;333333&semi;">field<&sol;span><&sol;span> which is mandatory in order to enable authentication through Twitter&period;<&sol;p> &NewLine;<p><img class&equals;"alignnone wp-image-3085" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;02&sol;auth&lowbar;3&period;png" alt&equals;"" width&equals;"642" height&equals;"290" &sol;><&sol;p> &NewLine;<p>7&period; Fill in the below two properties which are mandatory when you enable &&num;8220&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>Request email from users&&num;8221&semi;<&sol;strong><&sol;span> and then click on <strong><span style&equals;"color&colon; &num;008000&semi;">save<&sol;span><&sol;strong>&period;<&sol;p> &NewLine;<p><img class&equals;"alignnone wp-image-3086 size-full" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;02&sol;auth&lowbar;4-e1644659618467&period;png" alt&equals;"" width&equals;"803" height&equals;"349" &sol;><&sol;p> &NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Creating new project<&sol;span><&sol;strong><&sol;h4> &NewLine;<p>1 &period; Create a new project by going to <span style&equals;"color&colon; &num;008000&semi;"><strong>File &Implies; New Android Project<&sol;strong><&sol;span>&comma; select <span style&equals;"color&colon; &num;008000&semi;"><strong>Empty<&sol;strong><&sol;span> Activity&comma; provide <span style&equals;"color&colon; &num;008000&semi;"><strong>app<&sol;strong><&sol;span> name&comma; select language to <span style&equals;"color&colon; &num;008000&semi;"><strong>kotlin<&sol;strong><&sol;span> and then finally click on <span style&equals;"color&colon; &num;008000&semi;"><strong>finish<&sol;strong><&sol;span>&period;<&sol;p> &NewLine;<p>2 &period; Open app-level build&period;gradle file and add the below libraries under dependencies section and then click on <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Sync Now<&sol;strong><&sol;span>&period;&colon;<&sol;p> &NewLine;<pre>dependencies &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Include all the Twitter APIs<&sol;span><&sol;strong><br &sol;><strong> &sol;&sol;compile 'com&period;twitter&period;sdk&period;android&colon;twitter&colon;3&period;1&period;1'<&sol;strong><br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;include only the necessary dependencies to avoid the Android dex limit<&sol;span><&sol;strong><br &sol;> implementation 'com&period;twitter&period;sdk&period;android&colon;twitter-core&colon;3&period;1&period;1'<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;dependency to load image url over image view<&sol;span><&sol;strong><br &sol;> implementation 'com&period;squareup&period;picasso&colon;picasso&colon;2&period;5&period;2'<br &sol;>&rcub;<&sol;pre> &NewLine;<p>3 &period; Open the AndroidManifest&period;xml file and add the Internet permission above the application element&period;<&sol;p> &NewLine;<pre><strong><span style&equals;"color&colon; &num;008000&semi;"> &lt&semi;uses-permission android&colon;name&equals;"android&period;permission&period;INTERNET" &sol;&gt&semi;<&sol;span><&sol;strong><&sol;pre> &NewLine;<p>4&period;  Open your<span style&equals;"color&colon; &num;008000&semi;"> <strong>&sol;app&sol;res&sol;values&sol;strings&period;xml<&sol;strong><&sol;span> file and place the &&num;8216&semi;Consumer Key&&num;8217&semi; and &&num;8216&semi;Consumer Secret&&num;8217&semi; of our app generated by Twitter&colon;<&sol;p> &NewLine;<pre>&lt&semi;resources&gt&semi;<br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;"> &lt&semi;&excl;--<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> place here CONSUMER&lowbar;KEY&lpar;API Key&rpar; and CONSUMER&lowbar;SECRET&lpar;API Secret&rpar;<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> --&gt&semi;<&sol;span><&sol;strong><br &sol;> &lt&semi;string name&equals;"com&period;twitter&period;sdk&period;android&period;CONSUMER&lowbar;KEY"&gt&semi;ENTER YOUR CONSUMER&lowbar;KEY&lt&semi;&sol;string&gt&semi;<br &sol;> &lt&semi;string name&equals;"com&period;twitter&period;sdk&period;android&period;CONSUMER&lowbar;SECRET"&gt&semi;ENTER YOUR CONSUMER&lowbar;SECRET&lt&semi;&sol;string&gt&semi;<br &sol;><br &sol;>&lt&semi;&sol;resources&gt&semi;<&sol;pre> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating the Layout File<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>5&period; The below layout file consists of a <strong><span style&equals;"color&colon; &num;008000&semi;">TwitterLoginButton&comma;<&sol;span><&sol;strong> an ImageView &lpar;to show user profile picture&rpar;&comma; and a <strong><span style&equals;"color&colon; &num;008000&semi;">TextView<&sol;span><&sol;strong> &lpar;to show user info&rpar;&period;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">activity&lowbar;main&period;xml<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<br &sol;>&lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br &sol;> xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"<br &sol;> xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<br &sol;> tools&colon;context&equals;"&period;MainActivity"&gt&semi;<br &sol;><br &sol;> &lt&semi;com&period;twitter&period;sdk&period;android&period;core&period;identity&period;TwitterLoginButton<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;login&lowbar;button"<br &sol;> android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;alignParentBottom&equals;"true"<br &sol;> android&colon;layout&lowbar;centerHorizontal&equals;"true"<br &sol;> android&colon;layout&lowbar;centerVertical&equals;"true"<br &sol;> android&colon;layout&lowbar;marginBottom&equals;"50dp"<br &sol;> app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;ImageView<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;userImage"<br &sol;> android&colon;layout&lowbar;width&equals;"100dp"<br &sol;> android&colon;layout&lowbar;height&equals;"100dp"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"50dp"<br &sol;> android&colon;scaleType&equals;"center"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;TextView<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;userDetail"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"20dp"<br &sol;> android&colon;padding&equals;"5dp"<br &sol;> android&colon;text&equals;""<br &sol;> android&colon;textColor&equals;"&commat;color&sol;purple&lowbar;500"<br &sol;> android&colon;textSize&equals;"21sp"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;userImage" &sol;&gt&semi;<br &sol;><br &sol;>&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating the Application class<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>6&period; Inside the MyApplication class initialize the TwitterKit SDK&period;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">MyApplication&period;kt<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>class MyApplication&colon; Application&lpar;&rpar; &lbrace;<br &sol;><br &sol;> override fun onCreate&lpar;&rpar; &lbrace;<br &sol;> super&period;onCreate&lpar;&rpar;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; Entry point to initialize the TwitterKit SDK&period;<&sol;span><&sol;strong><br &sol;> Twitter&period;initialize&lpar;this&rpar;<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine;<p>7&period; Open the AndroidManifest&period;xml file and add the application name property as shown below&colon;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine; &NewLine;<pre>&lt&semi;application<br &sol;><strong><span style&equals;"color&colon; &num;0000ff&semi;">android&colon;name&equals;"&period;MyApplication"<&sol;span><&sol;strong><br &sol;>&period;&period;&period;&period;<br &sol;>&lt&semi;&sol;application&gt&semi;<&sol;pre> &NewLine;<div id&equals;"quads-ad2" class&equals;"quads-location quads-ad2"> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating the MainActivity File<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>8&period; The <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity&period;kt<&sol;strong><&sol;span> file contains the below code&colon;<&sol;p> &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;kt<&sol;strong><&sol;span><&sol;p> &NewLine;<pre>package com&period;c1ctech&period;twitloginexp<br &sol;><br &sol;>import androidx&period;appcompat&period;app&period;AppCompatActivity<br &sol;>import android&period;os&period;Bundle<br &sol;>import com&period;c1ctech&period;twitloginexp&period;databinding&period;ActivityMainBinding<br &sol;>import android&period;widget&period;Toast<br &sol;>import com&period;twitter&period;sdk&period;android&period;core&period;&ast;<br &sol;>import android&period;content&period;Intent<br &sol;>import com&period;squareup&period;picasso&period;Picasso<br &sol;>import com&period;twitter&period;sdk&period;android&period;core&period;models&period;User<br &sol;><br &sol;>class MainActivity &colon; AppCompatActivity&lpar;&rpar; &lbrace;<br &sol;><br &sol;> lateinit var activityMainBinding&colon; ActivityMainBinding<br &sol;><br &sol;> override fun onCreate&lpar;savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;<br &sol;> super&period;onCreate&lpar;savedInstanceState&rpar;<br &sol;><br &sol;> activityMainBinding &equals; ActivityMainBinding&period;inflate&lpar;layoutInflater&rpar;<br &sol;><br &sol;> setContentView&lpar;activityMainBinding&period;root&rpar;<br &sol;><br &sol;> loginWithTwitter&lpar;&rpar;<br &sol;> &rcub;<br &sol;><br &sol;> private fun loginWithTwitter&lpar;&rpar; &lbrace;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;user is not authenticated<&sol;span><&sol;strong><br &sol;> if &lpar;getTwitterSession&lpar;&rpar; &equals;&equals; null&rpar; &lbrace;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Sets the Callback to invoke when login completes&period;<&sol;span><&sol;strong><br &sol;> activityMainBinding&period;loginButton&period;setCallback&lpar;object &colon; Callback&lt&semi;TwitterSession&quest;&gt&semi;&lpar;&rpar; &lbrace;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Unsuccessful call due to network failure&comma; non-2XX status code&comma; or unexpected exception&period;<&sol;span><&sol;strong><br &sol;> override fun failure&lpar;exception&colon; TwitterException&quest;&rpar; &lbrace;<br &sol;> Toast&period;makeText&lpar;<br &sol;> applicationContext&comma;<br &sol;> "Login failed&colon; " &plus; exception&comma;<br &sol;> Toast&period;LENGTH&lowbar;LONG<br &sol;> &rpar;&period;show&lpar;&rpar;<br &sol;> &rcub;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Called when call completes successfully&period;<&sol;span><&sol;strong><br &sol;> override fun success&lpar;result&colon; Result&lt&semi;TwitterSession&quest;&gt&semi;&quest;&rpar; &lbrace;<br &sol;> Toast&period;makeText&lpar;applicationContext&comma; "Login successful"&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;<br &sol;> val twitterSession &equals; result&quest;&period;data<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;get secret and token from twitterSession<&sol;span><&sol;strong><br &sol;> <strong> &sol;&ast;val token &equals; twitterSession&quest;&period;authToken&quest;&period;token<&sol;strong><br &sol;><strong> val secret &equals; twitterSession&quest;&period;authToken&quest;&period;secret&ast;&sol;<&sol;strong><br &sol;><br &sol;> if &lpar;twitterSession &excl;&equals; null&rpar; &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;fetch user twitter info and display it on screen<&sol;span><&sol;strong><br &sol;> fetchUserTwitterInfo&lpar;&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;> &rcub;&rpar;<br &sol;> &rcub; else &lbrace;<br &sol;> Toast&period;makeText&lpar;this&comma; "User already authenticated"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;<br &sol;> fetchUserTwitterInfo&lpar;&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;return the active session if available<&sol;span><&sol;strong><br &sol;> private fun getTwitterSession&lpar;&rpar;&colon; TwitterSession&quest; &lbrace;<br &sol;> val twitterSession &equals; TwitterCore&period;getInstance&lpar;&rpar;&period;sessionManager&period;activeSession<br &sol;> return twitterSession<br &sol;> &rcub;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; this method will provide you User model which contains all user information<&sol;span><&sol;strong><br &sol;> fun fetchUserTwitterInfo&lpar;&rpar; &lbrace;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;fetch twitter image with other information if user is already authenticated<&sol;span><&sol;strong><br &sol;> if &lpar;getTwitterSession&lpar;&rpar; &excl;&equals; null&rpar; &lbrace;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;initialize twitter api client<&sol;span><&sol;strong><br &sol;> val twitterApiClient &equals; TwitterCore&period;getInstance&lpar;&rpar;&period;apiClient<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;pass includeEmail &colon; true if you want to fetch Email as well<&sol;span><&sol;strong><br &sol;> val call &equals;<br &sol;> twitterApiClient&period;accountService&period;verifyCredentials&lpar;true&comma; false&comma; true&rpar;<br &sol;> call&period;enqueue&lpar;object &colon; Callback&lt&semi;User&quest;&gt&semi;&lpar;&rpar; &lbrace;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; Called when call completes successfully&period;<&sol;span><&sol;strong><br &sol;> override fun success&lpar;result&colon; Result&lt&semi;User&quest;&gt&semi;&quest;&rpar; &lbrace;<br &sol;><br &sol;> val user &equals; result&quest;&period;data<br &sol;> activityMainBinding&period;userDetail&period;text &equals;<br &sol;> """<br &sol;> User Id &colon; &dollar;&lbrace;user&quest;&period;id&rcub;<br &sol;> User Name &colon; &dollar;&lbrace;user&quest;&period;name&rcub;<br &sol;> Email Id &colon; &dollar;&lbrace;user&quest;&period;email&rcub;<br &sol;> Screen Name &colon; &dollar;&lbrace;user&quest;&period;screenName&rcub;<br &sol;> """&period;trimIndent&lpar;&rpar;<br &sol;><br &sol;> var imageProfileUrl &equals; user&quest;&period;profileImageUrl<br &sol;><br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;NOTE &colon; User profile provided by twitter is very small in size<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Link &colon; https&colon;&sol;&sol;developer&period;twitter&period;com&sol;en&sol;docs&sol;accounts-and-users&sol;user-profile-images-and-banners<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;so if you want to get bigger size image then do the following&colon;<&sol;span><&sol;strong><br &sol;> imageProfileUrl &equals; imageProfileUrl&quest;&period;replace&lpar;"&lowbar;normal"&comma; ""&rpar;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;load image using Picasso<&sol;span><&sol;strong><br &sol;> Picasso&period;with&lpar;this&commat;MainActivity&rpar;<br &sol;> &period;load&lpar;imageProfileUrl&rpar;<br &sol;> &period;placeholder&lpar;R&period;drawable&period;placeholder&rpar;<br &sol;> &period;into&lpar;activityMainBinding&period;userImage&rpar;<br &sol;> &rcub;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; Unsuccessful call due to unexpected exception&period;<&sol;span><&sol;strong><br &sol;> override fun failure&lpar;exception&colon; TwitterException&quest;&rpar; &lbrace;<br &sol;> Toast&period;makeText&lpar;<br &sol;> this&commat;MainActivity&comma;<br &sol;> exception&quest;&period;message&comma;<br &sol;> Toast&period;LENGTH&lowbar;SHORT<br &sol;> &rpar;&period;show&lpar;&rpar;<br &sol;> &rcub;<br &sol;><br &sol;> &rcub;&rpar;<br &sol;> &rcub; else &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;if user is not authenticated first ask user to do authentication<&sol;span><&sol;strong><br &sol;> Toast&period;makeText&lpar;this&comma; "User is not authenticated&period;"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;<br &sol;> &period;show&lpar;&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;><br &sol;> override fun onActivityResult&lpar;requestCode&colon; Int&comma; resultCode&colon; Int&comma; data&colon; Intent&quest;&rpar; &lbrace;<br &sol;><br &sol;> super&period;onActivityResult&lpar;requestCode&comma; resultCode&comma; data&rpar;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; Pass the activity result to the login button&period;<&sol;span><&sol;strong><br &sol;> activityMainBinding&period;loginButton&period;onActivityResult&lpar;requestCode&comma; resultCode&comma; data&rpar;<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine;<p>In the above code&comma;<&sol;p> &NewLine;<ul> &NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><strong>getTwitterSession&lpar;&rpar;<&sol;strong><&sol;span> method will return the active session if available&period;<&sol;li> &NewLine;<li>When a user successfully logs in&comma; the loginButton&period;onActivityResult&lpar;&rpar; will invoke&comma; which will pass the response to the registered callback&period;<&sol;li> &NewLine;<li>If the login <span style&equals;"color&colon; &num;008000&semi;"><strong>succeeds<&sol;strong><&sol;span>&comma; the <strong><span style&equals;"color&colon; &num;008000&semi;">TwitterSession<&sol;span><&sol;strong> parameter has the <strong>userId&comma; userName&comma; authToken &lpar;contains token and secret&rpar;<&sol;strong>&comma; etc&period;<&sol;li> &NewLine;<li>If the TwitterSession parameter is not null&comma; then we will fetch the user&&num;8217&semi;s Twitter info and display it on the screen&period;<&sol;li> &NewLine;<li>If the login <strong><span style&equals;"color&colon; &num;008000&semi;">failed<&sol;span>&comma; <&sol;strong>the callback<strong><span style&equals;"color&colon; &num;008000&semi;"> failure&lpar;&rpar;<&sol;span><&sol;strong> method will call with an exception&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<p>When you run the app it will look like this&colon;<&sol;p> &NewLine;<p><img class&equals;"alignnone wp-image-3079" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;02&sol;Screenshot&lowbar;2022-02-10-13-59-28-932&lowbar;com&period;c1ctech&period;twitloginexp-473x1024&period;png" alt&equals;"" width&equals;"314" height&equals;"680" &sol;><&sol;p> &NewLine;<p>When a user clicks on <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Log in with Twitter<&sol;strong><&sol;span> button&comma; it will ask for entering the user&&num;8217&semi;s Twitter email&sol;username and password then click on &&num;8220&semi;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>Authorize app&&num;8221&semi;<&sol;strong><&sol;span>&period;<&sol;p> &NewLine;<p><img class&equals;"alignnone wp-image-3077" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;02&sol;Screenshot&lowbar;2022-02-10-14-01-53-172&lowbar;com&period;c1ctech&period;twitloginexp-473x1024&period;png" alt&equals;"" width&equals;"384" height&equals;"831" &sol;><&sol;p> &NewLine;<&sol;div> &NewLine;<p>When a user successfully logs in&comma; it will show the user info as shown below&colon;<&sol;p> &NewLine;<p><img class&equals;"alignnone wp-image-3078" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;02&sol;Screenshot&lowbar;2022-02-10-14-02-23-665&lowbar;com&period;c1ctech&period;twitloginexp-473x1024&period;png" alt&equals;"" width&equals;"366" height&equals;"792" &sol;>&NewLine;

Exit mobile version