Posting status update on twitter using Twitter4j

24 / Sep / 2012 by Vishal Sahu 0 comments

Hi,

In the previous post, we saw how to Retweet any user’s tweet from our application. In the same project, i needed to post a new tweet on behalf of the authenticating user from the application using Twitter API.

Posting a new status update is quite easy using Twitter4j library.


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 
       String statusUpdate = // message to be posted

Code to create new Twitter Post :-

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

So, this code will create a new Twitter post on behalf of the authenticating user using API calls.

Hope this helps.


Other Blogs:-

Send Direct Message using Twitter4j
Mark Tweet as Favorite using Twitter4j
Reply to a tweet using Twitter4j
Retweet a tweet using Twitter4j


FOUND THIS USEFUL? SHARE IT

Leave a comment -