Recently in one of our social projects that we are building with Lift 2.4, we had a requirement to sign in with Twitter amongst others like Facebook and Google. This post summarizes the work done step by step.
1) Create a Twitter App (if you do not have one already)
Follow the link https://dev.twitter.com/apps/new and create an app. Remember to enter a Callback URL (we missed this one and wasted an hour trying to debug). The Callback URL could be something like http://www..com/twitter/callback.
The significance of Callback URL is that, if you do not specify it, Twitter would disable callbacks.
Take a note of Consumer key wu5cUPDyBlaBla0rwRGww and
Consumer secret g9vuX4qb70iBlaBlaBlaDB2AjcYIJUtttx52TM
We would use them in our code.
2) In your build.sbt, make the following entry
as a library dependency.
3) If you are using eclipse, do sbt eclipse to get the Twitter4j dependencies. We are using Twitter4J, the unofficial Java library to access Twitter API
4) If you are using Lift, then add the keys in your default.props.
5) Let us now create a dispatcher which would handle the incoming requests
In our case we create an object TwitterDispatcher
6) The matcher is defined as following. If we get the URL as /twitter/authenticate then we call signUpRedirect and so on for other requests.
7) signUpRedirect
8) As you would see in the code, we pass a callBackURL when we are getting the request token
This tells twitter that once you are done with the authentication, pass back the results to the callbackURL which in our case is
9) Once we get a call back from twitter we get it on /twitter/callback which is mapped to processCallBack
10) You might want to store the twitter objects in session if you want and destroy them when the user logs out
11)
Entire code can be found here https://gist.github.com/3175581
12) If you are using Lift, you need to tell lift that it should use the TwitterDispatcher. You do that by putting the following line in Boot.scala
1 thought on “Providing a “Sign-in with Twitter” functionality using Scala4 min read”
Comments are closed.