If you implement "Login With Twitter" for your website, users don't need to remember  password for your website, so users will feel comfortable to use your website. In advance, you can access users  timeline feed. Twitter is providing Twitter4J to implement twitter api using java, download this twitter4j and add it to build path


I have provided sample project here, download it. Open Setup.java and give your twitter app credentials, database credentials. Create MySQL database schema with name "demos". Created below specified table. Import this project into Eclipse, add it to tomcat server, run index.html


  1. When user click on Login with twitter button, Provide them access link which is generated by Twitter4j with your app credentials. 
  2. User will be redirected to twitter api page
  3. Once user gives permission, access token and oauth verifier will be sent to callback URL
  4. Now verify the access token with oauth verifier. It will generate access token object which will have access token and access token secret 
  5. Now save access token and access token secret in database with user details like twitter user id and screen name.
User cant change their twitter user id, so we can use this to identify user. If user login again, we can process user's account using twitter user id. Whenever you want to update status on user's timeline, you can do with these saved access tokens.

Create App In Twitter Developer Page

Open twitter apps webpage.  Create your app and get Consumer Key and Consumer Secret Key. Observe below diagram.

Database Table

Created database table like below.

CREATE TABLE `demos`.`twitter_user` (
  `twitter_user_id` INT NULL COMMENT '',
  `screen_name` VARCHAR(45) NULL COMMENT '',
  `access_token` VARCHAR(100) NULL COMMENT '',
  `access_token_secret` VARCHAR(100) NULL COMMENT '',
  PRIMARY KEY (`user_id`)  COMMENT '',
  UNIQUE INDEX `twitter_user_id_UNIQUE` (`twitter_user_id` ASC)  COMMENT '');

Prepare Signin Link

Observe below code. Configure twitter object with Consumer Key and Secret Key, Generate request token with callback URL, save it to session. Generate authentication URL and redirect to that URL
// configure twitter api with consumer key and secret key
ConfigurationBuilder cb = new ConfigurationBuilder();
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
request.getSession().setAttribute("twitter", twitter);
try {
    // setup callback URL
    StringBuffer callbackURL = request.getRequestURL();
    int index = callbackURL.lastIndexOf("/");
    callbackURL.replace(index, callbackURL.length(), "").append("/callback");

    // get request object and save to session
    RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL.toString());
    request.getSession().setAttribute("requestToken", requestToken);
    // redirect to twitter authentication URL

} catch (TwitterException e) {
    throw new ServletException(e);

Callback Servlet

Callback servlet will receive oauth_verifier parameter. Verify access token with that verification code, then you will get access token object, Now save or update access token or access token secret to database. Here we identify user with twitter user id that is always constant. TwitterDAO.selectTwitterUser method gives user object based on twitter user id.
// Get twitter object from session
Twitter twitter = (Twitter) request.getSession().getAttribute("twitter");
//Get twitter request token object from session
RequestToken requestToken = (RequestToken) request.getSession().getAttribute("requestToken");
String verifier = request.getParameter("oauth_verifier");
try {
    // Get twitter access token object by verifying request token 
    AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
    // Get user object from database with twitter user id
    UserPojo user = TwitterDAO.selectTwitterUser(accessToken.getUserId());
    if(user == null) {
       // if user is null, create new user with given twitter details 
       user = new UserPojo();
       user = TwitterDAO.selectTwitterUser(accessToken.getUserId());
    } else {
       // if user already there in database, update access token
    request.setAttribute("user", user);
} catch (TwitterException | DBException e) {
    throw new ServletException(e);
request.getRequestDispatcher("/status.jsp").forward(request, response);


  1. how to implement login with twitter functionality in a web application using java play framework?

  2. Thanks for this tutorial, it saved my project

    1. Can you send me your code to my email ID : "aruntheboss222@gmail.com", Am getting error. I need this code for my project, pls

  3. To begin adequately first gathering the general population who really have something intriguing to state on TweetDeck buy twitter followers uk

  4. When you set up your Twitter account, your first aim would be to get the maximum number of followers. BRSM.io

  5. many people come and go but i don't think that anyone can replace him https://twitter.com/darrenwinters01 follow this man for the best ideas about business..!!

  6. Adopt Restrictions on Posts, yet comprehend the necessities of the NLRA first! social media groups

  7. I received all the services for which I paid, the price is affordable and corresponds to quality. I recommend ALL-SMM to all those who want to cheat Instagram account ... thanks to the guys for the great work! :))) Now I'm going to buy Instagram likes.

  8. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. smm panels list

  9. howdy was simply checking whether you minded a remark. i like your site and the thme you picked is super. I will be back. Buy Organic Twitter Retweets

  10. Much thanks to you for some other useful blog.
    Twitter accounts for sale

  11. This comment has been removed by the author.

  12. Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you. Buy Twitter Likes

  13. If you are a music lover I suggest you stay in Korca and listen to their beautiful serenades. Every note they sing seems like it is a part of a beautiful story from the city Albanian Riviera

  14. More often than not, when a LinkedIn profile for the organization is available, it is appeared at the exceptionally top of a hunt. visit now



Follow this blog by Email

Popular Posts