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
Project
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
Implementation
- When user click on Login with twitter button, Provide them access link which is generated by Twitter4j with your app credentials.
- User will be redirected to twitter api page
- Once user gives permission, access token and oauth verifier will be sent to callback URL
- Now verify the access token with oauth verifier. It will generate access token object which will have access token and access token secret
- Now save access token and access token secret in database with user details like twitter user id and screen name.
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` ( `user_id` INT NOT NULL AUTO_INCREMENT COMMENT '', `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 '', `created_date` DATETIME NULL DEFAULT CURRENT_TIMESTAMP 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(); cb.setDebugEnabled(true) .setOAuthConsumerKey(Setup.CONSUMER_KEY) .setOAuthConsumerSecret(Setup.CONSUMER_SECRET); 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 response.sendRedirect(requestToken.getAuthenticationURL()); } 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); request.getSession().removeAttribute("requestToken"); // 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.setTwitter_user_id(accessToken.getUserId()); user.setTwitter_screen_name(accessToken.getScreenName()); user.setAccess_token(accessToken.getToken()); user.setAccess_token_secret(accessToken.getTokenSecret()); TwitterDAO.insertRow(user); user = TwitterDAO.selectTwitterUser(accessToken.getUserId()); } else { // if user already there in database, update access token user.setAccess_token(accessToken.getToken()); user.setAccess_token_secret(accessToken.getTokenSecret()); TwitterDAO.updateAccessToken(user); } request.setAttribute("user", user); } catch (TwitterException | DBException e) { throw new ServletException(e); } request.getRequestDispatcher("/status.jsp").forward(request, response);
how to implement login with twitter functionality in a web application using java play framework?
ReplyDeleteThanks for this tutorial, it saved my project
ReplyDeleteCan you send me your code to my email ID : "aruntheboss222@gmail.com", Am getting error. I need this code for my project, pls
DeleteTo begin adequately first gathering the general population who really have something intriguing to state on TweetDeck buy twitter followers uk
ReplyDeleteWhen you set up your Twitter account, your first aim would be to get the maximum number of followers. BRSM.io
ReplyDeletemany 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..!!
ReplyDeleteAdopt Restrictions on Posts, yet comprehend the necessities of the NLRA first! social media groups
ReplyDeleteI 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.
ReplyDeleteExcellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. smm panels list
ReplyDeletehowdy 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
ReplyDeleteMuch thanks to you for some other useful blog.
ReplyDeleteTwitter accounts for sale
This comment has been removed by the author.
ReplyDeleteHey 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
ReplyDeleteIf 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
ReplyDeleteMore often than not, when a LinkedIn profile for the organization is available, it is appeared at the exceptionally top of a hunt. visit now
ReplyDeletehttps://www.socifan.com/free-youtube-views It can be pretty harsh for beginners to see legends like PewDiePie and T-Series accumulate millions of views that easy. Don't fall into despair, though. It's time to go berserk with SociFan; we got your back with free YouTube views!
ReplyDeleteYouTube is a platform with tight competition, and it can be extremely challenging for a beginner to grind amongst the best in the game. Nevertheless, all hope is not lost yet.
This harsh competition is what makes YouTube beautiful and diverse. In this way, users have millions of unique content which is ready to consume. There is something for everyone.
So, when you decided to buy facebook likes you can benefit from many variations with our service.
ReplyDeleteGreat Article.. to promote this you should go through https://ytbuyviews.com/ and you will get complete support and guarantee
ReplyDeleteGreat Article.. to promote this you should go through https://www.ytviews.in and you will get complete support and guarantee and get fast views, subscribers and comments.
ReplyDeleteThanks for sharing this awesome guide about buy facebook followers, it is very helping for all. I recommend others to visit this website. Keep sharing.
ReplyDelete
ReplyDeleteBuy youtube views from india's no.1 website https://www.tubeviews.in/ , and submit an easy order on the website TUBEVIEWS
Creatorshala is india's largest community of content creators, if you u are a blogger,influencer and youtuber, you can create your creators account on https://www.creatorshala.com/
ReplyDeleteLiftLikes.com: Buy Facebook Likes in Cheap Price
ReplyDeletehttps://www.liftlikes.com/buy-facebook-likes/
Buy Facebook Likes with 100% money back guarantee. We provide 100% Real Facebook Likes with real promotions.
This is such a nice post, I have been impressed by the unique content it has to offer thanks for sharing such nice and informative post buy bulk Instagram accounts
ReplyDeleteI like your content, it's so nice and unique, everybody likes unique content but your content is easy to understand for your readers too buy Facebook accounts
ReplyDeleteThank you for sharing such nice and informative post, I have always lacked this talent of writing unique content, you are doing an excellent job, keep it up buy Twitter accounts
ReplyDeleteThank you for nice post, I have been a fan of those who have this unique talent of writing unique content, keep up the good work buy YouTube accounts
ReplyDeleteI came across Google and found your blog, I think your blog deserves higher rating, what's the criteria google uses to rank sites buy Gmail accounts
ReplyDeleteI like your site so much and I have bookmarked this site to get updated, your content is fresh and breath of fresh air for avid readers buy Instagram accounts
ReplyDeleteThank you for the nice post, how have you been able to produce unique content everytime, I guess this is an art and only few have it keep it up
ReplyDeleteYou can purchase all of the social media like TikTok, YouTube, Instagram likes views, comments, followers, subscribers and so many greater from India's No.-1 promoting website https:www.ytviews.in/ with a money-back guarantee, steady method, genuine service, 24/7 help, and many more offers.
ReplyDeletedish tv recharge Greetings! This is my first comment here so I just wanted to give a quick shout out and say I genuinely enjoy reading your articles. keep sharing
ReplyDeleteThis site is great, I really appreciate how you put up the information.Buy YouTube Views
ReplyDeletehttps://buytruefollowers.com/pinterest-repins/
ReplyDeletehttps://internetmarketingrocks.com/buy-pinterest-repins/
ReplyDeleteBenifit is a new virus that installs itself on your Yahoo accounts and pretends to be a legitimate antivirus program. The main feature of this scam is the ability to steal your passwords & email ids. This software is continually being sold for a very cheap price on the Internet, promising that it will protect your accounts from being hacked. After downloading this software onto your computer, it will then pretend to Buy yahoo accounts scan your system and tell you that your Yahoo accounts have been infected with some invisible virus which needs to be removed before it does any damage.
ReplyDeleteI am glad to read this article.I think this will useful and informative topic for everyone.This is such a nice post.I have been impressed by the unique content.I loved your post and got some good ideas for it. Pvainsta
ReplyDelete