{"id":8035,"date":"2012-09-24T01:47:54","date_gmt":"2012-09-23T20:17:54","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8035"},"modified":"2015-05-19T17:43:50","modified_gmt":"2015-05-19T12:13:50","slug":"followunfollow-twitter-account-using-twitter4j","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/followunfollow-twitter-account-using-twitter4j\/","title":{"rendered":"Follow\/Unfollow twitter account using Twitter4j"},"content":{"rendered":"<p>Hi,<\/p>\n<p>In my recent grails project, i worked on integrating twitter API with the grails application. In one of the requirements, we needed to show the tweets from user wall and show the profile of the person\/account who posted that tweet. In the profile, the user should be able to <strong>Follow<\/strong> or <strong>Unfollow<\/strong> that person from his twitter account using our application interface. I used <a href=\"http:\/\/twitter4j.org\/en\/index.html\">Twitter4j<\/a> (a java wrapper for twitter API calls), to make API calls and implemented this functionality.<\/p>\n<pre><\/pre>\n<p>For making any twitter API calls we need to have twitter account <strong>access_token<\/strong> and <strong>access_secret<\/strong>, which we obtains after authorizing\/connecting twitter account with the application as mentioned in this <a href=\"http:\/\/www.tothenew.com\/blog\/integrating-java-application-with-twitter\/\">blog<\/a>.<\/p>\n<pre><\/pre>\n<p><strong>Pre-Requirements:-<\/strong><\/p>\n<p>[java]<br \/>\n       String consumerKey = CONSUMER_KEY \/\/ key obtained after registering app.<br \/>\n       String consumerSecret =CONSUMER_SECRET \/\/ secret key obtained from the registered app.<br \/>\n       String twitterToken = USER_TWITTER_TOKEN<br \/>\n       \/\/ access_token received by authentication user&#8217;s twitter account<br \/>\n       String twitterSecret= USER_TWITTER_SECRET<br \/>\n        \/\/ access_secret obtained by authentication user&#8217;s twitter account<br \/>\n       String twitterName = \/\/ twitterName of the twitter account the user want to follow\/unfollow<\/p>\n<p>[\/java]<\/p>\n<pre><\/pre>\n<p><strong>Code to Follow a Twitter Account:-<\/strong><\/p>\n<p>[java]<\/p>\n<p>         TwitterFactory factory = new TwitterFactory()<br \/>\n         Twitter twitter = factory.getInstance()<br \/>\n         twitter.setOAuthConsumer(consumerKey, consumerSecret)<br \/>\n         AccessToken accessToken = new AccessToken(twitterToken, twitterSecret)<br \/>\n         twitter.setOAuthAccessToken(accessToken)<br \/>\n         twitter4j.User  user = twitter.createFriendship(twitterName)<br \/>\n    }<br \/>\n[\/java]<\/p>\n<p>The above code will help to <strong>Follow<\/strong> the twitter account provided in the twitterName.<\/p>\n<pre><\/pre>\n<p><strong>Code to Unfollow a Twitter Account:-<\/strong><\/p>\n<p>[java]<\/p>\n<p>        TwitterFactory factory = new TwitterFactory()<br \/>\n        Twitter twitter = factory.getInstance()<br \/>\n        twitter.setOAuthConsumer(consumerKey, consumerSecret)<br \/>\n        AccessToken accessToken = new AccessToken(twitterToken, twitterSecret)<br \/>\n        twitter.setOAuthAccessToken(accessToken)<br \/>\n        twitter4j.User  user = twitter.destroyFriendship(twitterName)<br \/>\n    }<br \/>\n[\/java]<\/p>\n<p>The above code will help to <strong>Unfollow<\/strong> the twitter account provided in the twitterName.<\/p>\n<pre><\/pre>\n<p>This worked for me.<br \/>\nHope it helps.<\/p>\n<pre><\/pre>\n<p>Other Blogs:-<\/p>\n<p><a href=\"http:\/\/www.tothenew.com\/blog\/sending-direct-message-to-a-twitter-user-using-twitter4j\/\">Send Direct Message using Twitter4j<\/a><br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/mark-tweet-as-favorite-using-twitter4j\/\">Mark Tweet as Favorite using Twitter4j<\/a><br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/reply-to-a-user-tweet-using-twitter4j\/\">Reply to a tweet using Twitter4j<\/a><br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/retweet-a-tweet-using-twitter4j\/\">Retweet a tweet using Twitter4j<\/a><\/p>\n<pre><\/pre>\n<p>Cheers!!!<br \/>\nVishal Sahu<br \/>\nvishal@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, In my recent grails project, i worked on integrating twitter API with the grails application. In one of the requirements, we needed to show the tweets from user wall and show the profile of the person\/account who posted that tweet. In the profile, the user should be able to Follow or Unfollow that person [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[7],"tags":[700,882,873,1018,1020,1022,1021,1019],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8035"}],"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\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8035"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8035\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}