Many people use Twitter4J for coding Twitter clients for Android, but I couldn't find a single example of how to implement "Sign-in with twitter" in my Android applications, as Facebook Single Sign-on works.
So I coded my own.
I took references from sign-post methodology and Twitter4J itself, merged goods of both and finally had what I wanted.
Find the project here: https://github.com/Sheikh-Aman/Android_Samples/tree/master/1. Sign-inWithTwitterT4J
The code should be self explanatory, still if you need any help, revert.
my contact details can be found on: www.sheikhaman.com
Update: I have been seeing many people not getting the idea of all the stuff being done in the code. So here's the simple step by step explanation of what's happening:
- Make sure your twitter app is set as a Web application rather than a Desktop Client. For this you'll have to specify a callback URL at twitter's end. Don't worry, it won't bother us much.
- You have the consumerKey and consumerSecret from twitter, you use twitter4j and create an auth URL with helpt of its methods. you specify a callback URL at this level.
- Then you invoke an intent that opens this URL in device's browser and takes the user to twitter for logging in.
- Once the user logs in, Twitter asks him/her to allow/deny your app, the access to your twitter profile.
- Once the user allows/denies permissions to your app, the browser redirects to the callback URL, which you had provided in the second step, and if you're going by my code, that callback URL calls your activity back.
- When you're activity gains the focus again, you separate the token and secret from it.
- You then create an AccessToken with those token and secret, and use this AccessToken for interaction with twitter.
- You can (you should) also save the token and secret and use it to create AccessToken for future interactions with twitter.
- If the user wants to logout, simply delete the token and secret from your app. Since twitter4j works in complete stateless manner, you don't have to do anything more.
- Once logged in, you'll have access to user's twitter account untill one of the two things happen: 1) The user logs out, or 2) The user revokes access to your app from twitter.
Again, In case of questions, revert.