PostMaster (Part 3) Integrating Twitter in mobile apps

Aaditya Doshi
Novumlogic
Published in
6 min readJan 27, 2021
Photo by Claudio Schwarz on Unsplash

Welcome to the series of blog posts about PostMaster- A library for integrating social media accounts in mobile apps.

PostMaster library is a custom composer which enables its users to post on social media platforms such as Facebook, LinkedIn, Twitter, Instagram, and Telegram without opening and using the pre-installed app for each platform.

If you missed the previous parts of the blog series, you can find them here-

Part-1: Where I talked about how & why we decided to build PostMaster app, the problems we faced while building it, and tried and tested solutions for the same.

Part-2: Here, I talked in-depth about how to integrate Facebook in an android app and how we addressed the many issues faced while integrating the same.

Twitter is one of the 5 social media platforms that we have integrated in the PostMaster library. So without any further ado let’s dive deep into how to integrate Twitter in mobile apps.

Setup steps required to be done on Twitter Developer Portal-

Twitter Developer Account Dashboard
  1. Twitter developer account (can be created by using existing twitter account or make a new one).
  2. Twitter API consumer key and API secret key (can be obtained after creating an app in the developer account).
  3. Create new project.

You can create a new app if you haven’t yet or open an existing ones by clicking on ‘Project & Apps’ from the left navigation drawer on the developer portal dashboard

Below image shows an example developer app created in Twitter developer profile.

An app must be created in developer profile in order to integrate it with your mobile app.

Your Mobile App Dashboard View

4. Add the callback URLs. This is required for login to work properly in the app. The URLs are as follows-

  • Callback URLs — twittersdk://
  • Website URL — https:/twitter.com/

Below image shows where to exactly add the above URLs.

Twitter Callback urls added in dashboard

5. Set user permissions from the developer account.

For setting the permissions go to developer profile -> Select your app -> App Permissions. Make sure you edit the permissions to read, write and direct messages as shown below-

Twitter enabled permissions

6. Setting consumer keys.

Consumer Keys are very important for integration with mobile apps. For viewing and addition the keys , go to developer profile -> Select your app -> Consumer Keys.

Twitter API consumer keys view section
Twitter API consumer key details

Click on view keys to view the API key and secret, copy them into your app in strings.xml file as shown in later steps.

Note- You cannot view keys again and again as the same keys won’t be available the next time you try to view it so avoid viewing the keys again and again as previous keys will be replaced by new ones and you’ll have to change the same in your app again and again.

Now that we have done all the setup required to enable for us to write code to integrate Twitter in android app, let’s dive into the coding part now.

Steps for Login and fetching user details & tokens

  1. Integrate the Twitter SDK for android.

2. Store the API consumer key and API secret key that we talked about earlier, in strings.xml file.

3. Initialize the Twitter SDK in Application class and add it in the <application> tag of the manifest.xml file as seen

4. Add twitter login button available in the SDK

5. Writing code for- twitter login, fetching access token, access token secret, and user details (such as username, email & profile image).

Note- Some important things that needs to be taken care of while handling API consumer key and API secret key-

When you view the keys in developer profile, make sure you save it a doc file or in the android project. Reason for the same is that the keys will get refreshed when you view it from developer profile and the previous once will get outdated once you regenerate them.

Don’t unnecessarily change the keys in the project if some minor error occurs while login. Only change them if it gives permission related exception in the app.

Managing multiple logins and sessions-

After login into twitter account your session will be created and you won’t have to reauthorize the app again i.e the hassle of adding account again and again while login will not be there. But there is a use case where if I want to add multiple twitter account then I’ll have to sign out of the Twitter to add another account. But since your session is created, you’ll have to sign out and then sign in again to add another account.

To avoid this you’ll have to log out the user automatically after all the details and token are retrieved. To do so I went through a whole lot of code and trial and error but nothing helped but at last I found a hack which will log out the user as soon as all the details are retrieved.

Place the above code after all the details are retrieved and you’re good to go.

Steps required to tweet (post) from app-

For tweeting from mobile app, I used Twitter4j, a third party client which can make OAuth calls to Twitter. This is an official third part client approved by Twitter and can be found in their developer resources. Checkout the same below-

  1. Twitter Login (Only for the first time).
  2. Store access token & access token secret obtained as a result of successful login.
  3. A post composer that will enable users to compose text or text with image for posting.
  4. Configuring the required APIs to achieve the required outcome.

Composing the tweet and posting it to Twitter is divided into 2 segments-

Tweet with only text content

Tweet with text

Tweet with text and image content

Tweet with image

Conclusion

Integrating Twitter login and tweeting from android app turned out a bit easy then expected because Twitter’s official developer docs are quite easy and comprehensive which boosts up the development process. We hereby have covered all the aspects of integration with Twitter and all the related important points. Stay tuned for the upcoming blogs on other social media platform integrations.

Also do give claps if you find this useful in any capacity.

Happy Coding !

--

--

Aaditya Doshi
Novumlogic

Mobile Developer | Graphic Designer | Tech Enthusiast | Caffeine Driven | Avid Learner | Marvel Cinematic Universe & Harry Potter Fan | Blues & Blues rock lover