Intergrating Twitter OAuth using java/spring/Twitter4j

The comprehensive link about Twitter approach for using Oauth (its same for other cases too ) can be found
To cut it short following are the important steps:

Get the consumer key and consumer secret.Dev Twitter

Twitter API

Note: If you are going to make an webapp dont forget to put a valid url in the “CallBack Url” field. The valid url can be back to your localhost. But dont put “localhost” as twitter doesnt provide support. I had put for this application. The value of url can be changed at runtime.

Following are the steps using browser.. My homepage url is


When a user clicks on the hyperlink an oauth authorize request is sent to twitter by sharing the token key and token secret and user is redirected to twitter page.

After login into Twitter the user will be redirected to the callback url and the username will be displayed.

I am using Twitter4j for connecting with Twitter.

public class WelcomeController {
	private OAuthToken oauthToken;
        Twitter twitter = new TwitterFactory().getInstance();

	private MyAccessToken accestoken;
	@RequestMapping(method = RequestMethod.GET)
	public String printWelcome(HttpServletResponse response,HttpServletRequest request) {

		RequestToken requestToken;
		try {
			String callbackURL = "";
             requestToken = twitter.getOAuthRequestToken(callbackURL);
             String token = requestToken.getToken();
 			String tokenSecret = requestToken.getTokenSecret();
			String authUrl = requestToken.getAuthorizationURL();
			request.setAttribute("authUrl", authUrl);
		} catch (TwitterException e) {
		return "login";

The callback url can be set as shown above. The MyAcessToken is used to persist the token for the request.

My login.jsp is

 <a href='<%=request.getAttribute("authUrl") %>'>Sign in with Twitter</a>

So when the user enters , they will be redirected to login.jsp.

When the user clicks on the “Sign in” link , it will be redirected to Twitter login site and then will be redirected to the callback url set. The controller to handle callback url is

public class CallBackController extends AbstractController{</code>

	private OAuthToken oauthToken;
	private MyAccessToken accessToken; 
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
			Twitter twitter = new TwitterFactory().getInstance();
			twitter.setOAuthConsumer(oauthToken.getConsumerKey(), oauthToken.getConsumerSecret());
			String verifier = request.getParameter("oauth_verifier");
	       RequestToken requestToken = new RequestToken(accessToken.getToken(), accessToken.getTokensecret());
	       AccessToken accessToken = twitter.getOAuthAccessToken(requestToken,verifier);
	       User user = twitter.verifyCredentials();
	       ModelAndView model = new ModelAndView("hello");
	       model.addObject("message", user.getName());
	       return model;

Once the user comes back it will be redirected to “hello.jsp” with user name displayed.Note the MyAccessToken set for user before request is made is used here.

<h1>Twitter Login successful for : ${message}</h1>

The oauthtoken is stored and configured in dispatcher-servlet xml which is loaded using web.xml

<context:component-scan base-package="com.common.controller" />

<property name="prefix">
<property name="suffix">
<bean id="oauthToken" class="com.common.OAuthToken">
<property name="consumerKey" value="yourconsumerkey"/>
<property name="consumerSecret" value="yourconsumersecret"/>
<bean id="accessToken" class="com.mkyong.common.MyAccessToken"/>

The other model classes OAuthToken and MyAccessToken :

public class OAuthToken {

	public String consumerKey;
	public String consumerSecret;
	public String getConsumerKey() {
		return consumerKey;
	public void setConsumerKey(String consumerKey) {
		this.consumerKey = consumerKey;
	public String getConsumerSecret() {
		return consumerSecret;
	public void setConsumerSecret(String consumerSecret) {
		this.consumerSecret = consumerSecret;
public class MyAccessToken {

	private String token;
	private String tokensecret;
	public String getTokensecret() {
		return tokensecret;
	public void setTokensecret(String tokensecret) {
		this.tokensecret = tokensecret;
	public String getToken() {
		return token;
	public void setToken(String token) {
		this.token = token;

Download Source Code

IS Twitter the new business card?

Recently a new trend is visible during tech-meet ups. While some people meeting up there were handing out business cards (mostly Moo Cards), the majority of people were merely adding the new faces they met directly to their Twitter follow lists via their smart phones. “Is Twitter the new business card?” This is how it went down:

Person meets person

Pleasantries are exchanged

Small chit chat about common business interests

Potential plans are made

“Let me get your contact info”

“Do you have Twitter?” (Note: this is where a business card should be exchanged)

“Why, yes I do?”

Both parties whip out Smart Phones and open respective Twitter clients

 Both parties type “follow newperson” into the Twitter client and submit

 “Great, nice to meet you. I will be in touch!”

It’s a novel idea, really. I did it myself. I found it easy and I didn’t have to worry so much about sorting through a mess of business cards when I got home (no, I don’t use a scanner). But then I ran into a significant issue: “Who the heck was that guy? Why am I following this person? What was that lady’s name again?” While doing such a thing is certainly convenient, is it realistic if you want to build a solid business relationship. There’s no way to enter details about this person, unless of course you immediately @ message them and say “Nice to meet you re: xyz.” But what if it’s for a business deal or a job opportunity? You certainly don’t want to announce that to the world. I think there’s an interesting opportunity here for Twitter, in terms of monetizing. What if the company gave people an opportunity to save contact details about a person as they add others to their follow lists? Perhaps an add-on service that we can subscribe to in order to turn Twitter into a contact database of sorts. Or, some sort of LinkedIn integration would be swell, too.

Post taken from : IS Twitter the new business card?