Create and delete Twitter Favorite - Java Social Media

Java examples for Social Media:Twitter

Description

Create and delete Twitter Favorite

Demo Code

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

public class ManageFavorites {
    public static void main(String[] args) {
        Twitter twitter = new TwitterFactory().getInstance();
        String consumerKey = "a";
        String consumerSecret = "a";
        String accessToken = "a";
        String accessTokenSecret = "a";

        twitter.setOAuthConsumer(consumerKey, consumerSecret);
        twitter.setOAuthAccessToken(new AccessToken(accessToken,
                accessTokenSecret));//from  w  w  w . j a v  a  2s.c o m
        try {
            if (args.length == 2) {
                String command = args[0];
                long id = Long.parseLong(args[1]);
                if ("add".equals(command)) {
                    twitter.createFavorite(id);
                    System.out.println(id
                            );
                } else if ("delete".equals(command)) {
                    twitter.destroyFavorite(id);
                    System.out.println(id);
                }
            }
            System.out.println("@" + twitter.getScreenName());
            ResponseList<Status> favorites = twitter.getFavorites();
            for (Status status : favorites) {
                System.out.println("@" + status.getUser().getScreenName()
                        + " - " + status.getText() + "(id:"
                        + status.getId() + ")");
            }
        } catch (TwitterException te) {
            System.out.println(te.getMessage());
        }
    }
}

Related Tutorials