Mark tweet as ‘Favorite’ using Twitter4j

24 / Sep / 2012 by Vishal Sahu 1 comments

Hi,

In one of my grails project, i worked on integrating twitter API with the application. The requirement is such that we need to display all of the tweets to any user and provide the basic functionality such as reply, retweet, favorite etc using the API calls. I used Twitter4j (a java wrapper for twitter API calls), to make API calls. You can refer this blog to see how we can integrate the grails application with Twitter.


For making any twitter API calls we need to have twitter account access_token and access_secret, which we obtains after authorizing/connecting twitter account with the application as mentioned in this blog.


Pre-Requirements:-

       String consumerKey = CONSUMER_KEY // key obtained after registering app.
       String consumerSecret =CONSUMER_SECRET // secret key obtained from the registered app.
       String twitterToken = USER_TWITTER_TOKEN
       // access_token received by authentication user's twitter account
       String twitterSecret= USER_TWITTER_SECRET
        // access_secret obtained by authentication user's twitter account
       Long tweetId = // id of the tweet


Code to mark tweet as Favorite:-


       TwitterFactory factory = new TwitterFactory()
        Twitter twitter = factory.getInstance()
        twitter.setOAuthConsumer(consumerKey, consumerSecret)
        AccessToken accessToken = new AccessToken(twitterToken, twitterSecret)
        twitter.setOAuthAccessToken(accessToken)
        Status status = twitter.createFavorite(tweetId)

The above code will mark the tweet as ‘Favorite’ for that user.
This worked for me.
Hope it helps.


Other Blogs:-

Send Direct Message using Twitter4j
Reply to a tweet using Twitter4j
Retweet a tweet using Twitter4j
Post status update using Twitter4j


Cheers!!!
Vishal Sahu
vishal@intelligrape.com

FOUND THIS USEFUL? SHARE IT

comments (1 “Mark tweet as ‘Favorite’ using Twitter4j”)

Leave a comment -