Twitter Login Authentication using Firebase in Android
In today’s world, many popular Android apps have the Twitter login system implemented. This makes it easier for their users to sign in directly to their app without creating an account manually. I’ve found that most developers who are starting to learn Android development also want to add this feature into their app. But there are very few resources available regarding this on the internet so to help many of you out, I am creating this post. In this blog post, we will be implementing the Twitter login authentication using Firebase in an Android application.
For implementing the Twitter login using Firebase and to make the Twitter login work inside your app you must have the Twitter application installed on your smartphone otherwise the login will not work.
But before diving into the code we first have to create a new project on the Firebase console as well as on the Twitter developers page.
Creating a Firebase Project
First, head over to the Firebase website and click on the “+ “ icon to create a new project.
On the next page enter the name of your app and then select “Set up Google Analytics for my project” and click on “Continue”. After that, it will ask to choose a Google Analytics account. If you have one then you can choose that in the drop-down, otherwise create a new one and then click on “Create Project”.
Now that we have created our Firebase project, we also have to create an app on the Twitter developers page. To turn on the Twitter sign-in feature inside the Firebase Authentication section, we will need an API Key and an API Secret which we can get from the Twitter developers page after creating an app. But first, let’s copy the callback URL by clicking on the Authentication section in the left inside the Firebase console, select the sign-in method tab to choose the “Twitter “ option, and copy the callback URL.
Creating an app on Twitter Developers
Go to the Twitter developers page and click on “ create an app “ to create a new project.
Then you have to fill in all of the details about your app like the name, website, description, privacy policy, etc. Make sure that you check the “Enable Sign in with Twitter” option and also enter the URL of your privacy policy page because it is really important. I will tell you why later in this blog post.
Also, it is mandatory to enter the callback URL here, which we have already copied from the Firebase console as mentioned above. If you have not copied the callback URL then please do so, then click on “Create” and accept the terms and conditions.
Now to get the API key and API Secret, select the “ Keys and Tokens “ tab at the top, copy these two keys, and paste it on to your Firebase Twitter sign-in method page back at the Firebase console, and click “save”.
Setting up the Android Studio for Firebase
Now that we are done with all the required setup work, you can now create a new Android Studio project. After that, let’s add the required dependencies and google-services.json file to your project. For that click on the Android icon as shown in the figure below and follow the first 3 steps. You can skip the 4th step.
Follow the steps mentioned on the screen until you get to the end which will set up your Android Studio project for using the Firebase services. It’s really easy and it will take around 5–10 minutes to complete.
Note: To get the Debug signing certificate SHA-1 key you have to run the following command. To make the command work you must have Java JDK and OpenSSL installed on your system.
//Debug keystore keytool -list -v -alias androiddebugkey -keystore C:\Users\<your user name>\.android\debug.keystore
Make sure that you are inside your Java JDK’s bin folder while running this command otherwise the command will not work and also replace <your user name> with your username.
Implementing the Twitter Sign in using Firebase
Now that we have everything set up we are ready to write the code and implement the Twitter sign-in feature on to our Android application. First, add the Twitter dependencies which can be found on the twitter-archive page on Github and follow the “ Add Twitter Kit Dependencies “ step only, to get twitter dependencies and then come back here.
Then add the following two string values into your strings.xml file. Here you have to add your API Key (Consumer Key) and API Secret (Consumer Secret) replacing the “XXXXXXX”.
<resources> <string name="twitter_consumer_key" translatable="false"> XXXXXXXXXXX </string>
<string name="twitter_consumer_secret" translatable="false"> XXXXXXXXXXX </string></resources>
- Adding internet permission in your
AndroidManifest.xml
file
<uses-permission android:name="android.permission.INTERNET" />
2. Adding Twitter sign-in button to activity_login.xml
3. Adding Twitter sign-in code to LoginActivity.java
4. Adding a logout button to activity_main.xml
5. Adding code to MainActivity.java
sign out the user
Solving the Twitter email field appearing empty in Firebase Authentication
Now after implementing all the above code into your Android project, if you run your app and log in to Twitter with your credentials then the details of that users will be available inside your Firebase Authentication section under “Users”. But you will notice that the email of the user is not available or is otherwise blank.
To solve this, you can go to the Twitter developers page where you have created your app and then click on “ Permissions”. Then click on the “Edit” button and check the “Request email address from users” checkbox. That will solve the problem.
This option will only be accessible to you if you have entered a privacy policy link to your app on the Twitter developers page. But after making the change, the time tie the user signs in, you will see their email address inside the Firebase console.
Final Output:
To get the full project you can go to the TwitterAuth on Github and can download it from there.
References:
- Android Programming: The Big Nerd Ranch Guide (3rd Edition)
- If you want to learn more about the Twitter authentication using Firebase in Android you can check out this documentation on GitHub, or the official Firebase documentation.
Thanks for reading and if you like the content then share it with others. For more blog posts related to technology, you can visit my website LionGuest Studios.
Originally published at https://liongueststudios.com on August 9, 2019.