In today’s world, many popular Android apps have twitter firebase authentication 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 to their app. But there are very few resources available online which tells us how to implement twitter firebase authentication 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 firebase authentication 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.
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.
//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 firebase authentication in android
Now that we have everything set up we are ready to write the code and implement the twitter firebase authentication 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
<uses-permission android:name="android.permission.INTERNET" />
- Adding the Twitter sign-in button to
- Adding Twitter sign-in code to
- Adding a logout button to
- Adding code to
MainActivity.javasign 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 by using twitter firebase authentication with your twitter account credentials then the details of that user 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.
To get the full project you can go to the TwitterAuth on Github and can download it from there.
- 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 our twitter firebase authentication blog then share it with others. For more blog posts related to technology, you can visit my website LionGuest Studios.