Manage Twitter List Members - Java Social Media

Java examples for Social Media:Twitter

Description

Manage Twitter List Members

Demo Code

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

public class ManageListMembers {
    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));/*  www. j  a  v  a  2 s  .c  om*/

        try {
            if (args.length == 0) {
                System.out.println("@" + twitter.getScreenName());
                PagableResponseList<UserList> lists = twitter.getUserLists(
                        twitter.getScreenName(), -1l);
                for (UserList list : lists) {
                    System.out.println(list.getName() + " - "
                            + list.getDescription() + "(" + list.getId()
                            + ")");
                }
            } else {
                int listId = Integer.parseInt(args[0]);
                if (args.length == 3) {
                    String command = args[1];
                    long userId = Long.parseLong(args[2]);
                    if ("add".equals(command)) {
                        twitter.addUserListMember(listId, userId);
                        System.out.println("id:" + listId + " ID: " + userId);
                    } else if ("delete".equals(command)) {
                        twitter.deleteUserListMember(listId, userId);
                        System.out.println("id:" + listId + " ID: " + userId);
                    }
                }
                if (args.length == 2) {
                    twitter.createUserList(args[0], true, args[1]);
                }
                System.out.println("id:" + listId);
                PagableResponseList<User> users = twitter
                        .getUserListMembers(twitter.getId(), listId, -1l);
                for (User user : users) {
                    System.out.println("@" + user.getScreenName() + "("
                            + user.getId() + ")");
                }
            }
        } catch (TwitterException te) {
            System.out.println(te.getMessage());
        }
    }
}

Related Tutorials