{"id":1147,"date":"2010-07-13T10:42:39","date_gmt":"2010-07-13T05:12:39","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1147"},"modified":"2010-08-02T16:59:59","modified_gmt":"2010-08-02T11:29:59","slug":"integrating-java-application-with-twitter","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/integrating-java-application-with-twitter\/","title":{"rendered":"Integrating Java Application with Twitter"},"content":{"rendered":"<p>These are few basic steps which will help you to integrate your java application with twitter using twitter4j.<\/p>\n<p>Twitter4J is a Java library for the Twitter API.<br \/>\nWith Twitter4J, you can easily integrate your Java application with the Twitter.<\/p>\n<p>Steps :<\/p>\n<p>1) Register your application at <a href=\"http:\/\/twitter.com\/apps\">http:\/\/twitter.com\/apps<\/a><br \/>\n2) Twitter will return Consumer Key and Consumer Secret for your application.<br \/>\n3) Download the twitter4j jar and place that jar file in the classpath.<br \/>\n4) Create a login action:<\/p>\n<blockquote>\n<pre lang=\"groovy\">\r\n\/\/ Login Action\r\nTwitter twitter = new Twitter();\r\ntwitter.setOAuthConsumer(ConsumerKey,ConsumerSecret);\r\nRequestToken requestToken = twitter.getOAuthRequestToken('callbackurl');\r\nString authUrl = requestToken.getAuthorizationURL(); \r\nsession.setAttribute(\"request-token\",  requestToken);\r\nredirect(url: authUrl);\r\n <\/pre>\n<\/blockquote>\n<p>5) Create a callback action .<\/p>\n<blockquote>\n<pre lang=\"groovy\">\r\n\/\/\/\u00a0 Call back action: Where the twitter will redirect after successfull authentication.\r\nRequestToken requestToken = (RequestToken) session.getAttribute(\"request-token\");\r\nString verifier = request.getParameter(\"oauth_verifier\")\r\nAccessToken accessToken = twitter.getOAuthAccessToken(requestToken,verifier)\r\n <\/pre>\n<\/blockquote>\n<p>6) Persist the access token and token secret for future use. (accessToken.token ,accessToken.tokenSecret). The  access token never expire until the user explicitly rejects the application from his settings.<\/p>\n<p>7) Now whenever you want to create the twitter client , just follow the steps below:<\/p>\n<blockquote>\n<pre lang=\"groovy\">             \r\nTwitter twitter = new Twitter()\r\ntwitter.setOAuthConsumer(ConsumerKey, ConsumerSecret)\r\ntwitter.setOAuthAccessToken(token, secretToken)  \/\/  token, secretToken which you have persisted in step 6.\r\n\/\/ For Example: to search someone on twitter.\r\nList<User> users = twitter.searchUsers(name, 1)\r\n <\/pre>\n<\/blockquote>\n<p>Hope this helped!<\/p>\n<p>Cheers!<\/p>\n<p>Anshul Sharma<\/p>\n","protected":false},"excerpt":{"rendered":"<p>These are few basic steps which will help you to integrate your java application with twitter using twitter4j. Twitter4J is a Java library for the Twitter API. With Twitter4J, you can easily integrate your Java application with the Twitter. Steps : 1) Register your application at http:\/\/twitter.com\/apps 2) Twitter will return Consumer Key and Consumer [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1147"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1147"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1147\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}