post a Tweet using Twitter REST API and Twitter4J Library in Java - Java Social Media

Java examples for Social Media:Twitter

Description

post a Tweet using Twitter REST API and Twitter4J Library in Java

Demo Code



import twitter4j.*;
import twitter4j.auth.AccessToken;

import java.io.IOException;
import java.net.URL;
import java.util.Arrays;


public class TweetUsingTwitter4jExample {

    public static void main(String[] args) throws IOException,
            TwitterException {//from   www . jav  a2 s  .  c  o m

        //Your Twitter App's Consumer Key
        String consumerKey = "X";

        //Your Twitter App's Consumer Secret
        String consumerSecret = "X";

        //Your Twitter Access Token
        String accessToken = "XXXXXXXX-XXXXXXXXXXXXXX";

        //Your Twitter Access Token Secret
        String accessTokenSecret = "XXXXXXXXXXXXXXXXX";

        //Instantiate a re-usable and thread-safe factory
        TwitterFactory twitterFactory = new TwitterFactory();

        //Instantiate a new Twitter instance
        Twitter twitter = twitterFactory.getInstance();

        //setup OAuth Consumer Credentials
        twitter.setOAuthConsumer(consumerKey, consumerSecret);

        //setup OAuth Access Token
        twitter.setOAuthAccessToken(new AccessToken(accessToken,
                accessTokenSecret));

        //Instantiate and initialize a new twitter status update
        StatusUpdate statusUpdate = new StatusUpdate(
        //your tweet or status message
                "H-1B Transfer Jobs | Java Developer | Harrison, NY | 2 Years"
                        + " - http://h1b-work-visa-usa.blogspot.com/2013/07/h-1b-transfer-jobs-java-developer_19.html");
        //attach any media, if you want to
        statusUpdate
                .setMedia(
                        //title of media
                        "http://h1b-work-visa-usa.blogspot.com",
                        new URL("http://")
                                .openStream());

        //tweet or update status
        Status status = twitter.updateStatus(statusUpdate);

        //response from twitter server
        System.out.println("status.toString() = " + status.toString());
        System.out.println("status.getInReplyToScreenName() = "
                + status.getInReplyToScreenName());
        System.out.println("status.getSource() = " + status.getSource());
        System.out.println("status.getText() = " + status.getText());
        System.out.println("status.getContributors() = "
                + Arrays.toString(status.getContributors()));
        System.out.println("status.getCreatedAt() = "
                + status.getCreatedAt());
        System.out.println("status.getCurrentUserRetweetId() = "
                + status.getCurrentUserRetweetId());
        System.out.println("status.getGeoLocation() = "
                + status.getGeoLocation());
        System.out.println("status.getId() = " + status.getId());
        System.out.println("status.getInReplyToStatusId() = "
                + status.getInReplyToStatusId());
        System.out.println("status.getInReplyToUserId() = "
                + status.getInReplyToUserId());
        System.out.println("status.getPlace() = " + status.getPlace());
        System.out.println("status.getRetweetCount() = "
                + status.getRetweetCount());
        System.out.println("status.getRetweetedStatus() = "
                + status.getRetweetedStatus());
        System.out.println("status.getUser() = " + status.getUser());
        System.out.println("status.getAccessLevel() = "
                + status.getAccessLevel());
        System.out.println("status.getHashtagEntities() = "
                + Arrays.toString(status.getHashtagEntities()));
        System.out.println("status.getMediaEntities() = "
                + Arrays.toString(status.getMediaEntities()));
        if (status.getRateLimitStatus() != null) {
            System.out.println("status.getRateLimitStatus().getLimit() = "
                    + status.getRateLimitStatus().getLimit());
            System.out
                    .println("status.getRateLimitStatus().getRemaining() = "
                            + status.getRateLimitStatus().getRemaining());
            System.out
                    .println("status.getRateLimitStatus().getResetTimeInSeconds() = "
                            + status.getRateLimitStatus()
                                    .getResetTimeInSeconds());
            System.out
                    .println("status.getRateLimitStatus().getSecondsUntilReset() = "
                            + status.getRateLimitStatus()
                                    .getSecondsUntilReset());
            //System.out.println("status.getRateLimitStatus().getRemainingHits() = " + status.getRateLimitStatus().getRemainingHits());
        }
        System.out.println("status.getURLEntities() = "
                + Arrays.toString(status.getURLEntities()));
        System.out.println("status.getUserMentionEntities() = "
                + Arrays.toString(status.getUserMentionEntities()));
    }

}

Related Tutorials