Android: Sign-in with Twitter


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: Sign-inWithTwitterT4J

The code should be self explanatory, still if you need any help, revert.
my contact details can be found on:

Happy coding!

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.
I hope this was explanatory.
Again, In case of questions, revert.


Farhan Ahmad said...

hi, i just download the source and give it a run... its giving the following exception at twitter.getOAuthRequestToken(CALLBACKURL) and the exception is:

401:Authentication credentials were missing or incorrect.

error: Desktop applications only support the oauth_callback value 'oob'.....

what i am doing wrong here??? plz help.... i m giving correct key's for consumer and secret.... is there any thing else??????

_-_-AnGeL-_-_ said...

I got the same error.Any idea???

Aman Alam said...

You guys probably have set your application at twitter's end as "Client" apps, rather than marking them as "Web" apps.
Correct that (in the app that you registered with

Farhan Ahmad said...

ok thanx for replying... by changing the settings to web application, its asking the callback url... what should we put in that?

jyoti said...

hey Aman thanx 4 giving simple example but Aman as twitter is opening in browser, i want that it should open on our application area onli not on browser

Aman Alam said...

Jyoti, that's completely normal.
If you have noticed, on the web too, if any application wants to access your twitter account, it opens up a new browser window, opens twitter in it, and uses it to authenticate.

The idea is, The user provides his/her credentials only to the service you have created a client of. This increases the level of trust users are going to put on your application, since they know that you aren't storing their username/passwords, but are asking twitter for it.

jyoti said...

hiii Aman thax for explaining very clearly.

Actually i gone through one example of android twitter in which sugree has created apk for twitter like as we have in facebook i.e It is very simple and also opening on our application area onli, but in his api file there is no method define for posting tweet.

I had used it, you also have a look once.

Farhan Ahmad said...

AA aman,
Its pretty much clear now after your great help and time. just was wondering if we can have a button for Login/logout, so that i can login/logout any time and can login with diff id... have no issue in login but i searched the twitter4j and stackoverflow but didn't find anything regarding logout. As you were saying to delete the accesstoken on logout, could you explain this more and through code example.

Many thanx,

Ankit said...

Hey Aman what would be the CALLBACK_URL as when i try to make it Browser instead of Client, it is asking URL and when i provided, the one given in ur sample code, twitter is not accepting saying u mail us the call back url and we would get back 2 u, they mailed me saying we are working on it but no updates then after.

Farhan Ahmad said...

hi ankit, as aman told in above comments, just put any webaddress in callbackurl on twitter website. the real callback that would matter would be the one which you provide in code...

Ankit said...

Hey Aman thanks for your tutorial, it helped me a lot, and still problem persist with inbuilt browser of my device with 2.1update1 but somehow i managed to get through this with opening twitter authUrl in WebView and got callback from Twitter.

and Thanks Farhan for responding.

Aman Alam said...

@Ankit This was going to be my next step, to include a custom webview which handles the redirect. Kudos to you for that, although I have done that only in my real world implementations, but couldn't post it here.

@Farhan Buddy, perhaps I have told you before too, that twitter4J is stateless. you can have a button which does login/logout.
Follow all the OAuth stuff when the login button is clicked, and when logout is clicked, delete the AccessToken that you saved.

@Jyoti I tried to dig into sugree's example, but it didn't work for me. Do you have any screenshot?

Usama said...


nice tutorial. It is working fine on emulator but when i try to run my application on real device, web browser doesn't open. I get this exception."Authentication credentials were missing or incorrect. Failed to validate oauth signature and token." Same code is working fine on emulator. Can anyone help me out please?

Alan Pappachan said...

The simplest working example that I could find after an extensive search. Very good post. I especially like how you reuse the activity. "setOAuthAccessToken" shows deprecated but works nevertheless.


cb said...
This comment has been removed by the author.
cb said...
This comment has been removed by the author.
cb said...

I have figure out the timeline also! Was blind!!!
How about to launch the twitter application to the account and not a timeline?

Volksgrenadier said...

can't download example. site is down.

Lizbee said...

To bad its not ready for version 11 and up of the sdk

Gianluca Vaccaro said...

Hi, thanks for the guide, but i've an error when call getOAuthRequestToken, he says "The request is understood, but it has been refused. SSL is required", can you please help me?! I'm desperate with twitter's api