Example usage for twitter4j TwitterFactory TwitterFactory

List of usage examples for twitter4j TwitterFactory TwitterFactory

Introduction

In this page you can find the example usage for twitter4j TwitterFactory TwitterFactory.

Prototype

public TwitterFactory() 

Source Link

Document

Creates a TwitterFactory with the root configuration.

Usage

From source file:com.redhat.middleware.jdg.TwitterDemoClient.java

License:Open Source License

protected void authorize() throws TwitterException, IOException {
    Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(consumerKey, consumerSecret);
    RequestToken requestToken = twitter.getOAuthRequestToken();
    AccessToken accessToken = null;//w w w . j a va2  s . c  om
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    while (null == accessToken) {
        logger.info("Open the following URL and grant access to your account:");
        logger.info(requestToken.getAuthorizationURL());
        logger.info("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
        String pin = br.readLine();
        try {
            if (pin.length() > 0) {
                accessToken = twitter.getOAuthAccessToken(requestToken, pin);
            } else {
                accessToken = twitter.getOAuthAccessToken();
            }

            this.accessToken = accessToken.getToken();
            this.accessTokenSecret = accessToken.getTokenSecret();
        } catch (TwitterException te) {
            if (401 == te.getStatusCode()) {
                logger.info("Unable to get the access token.");
            } else {
                te.printStackTrace();
            }
        }
    }
}

From source file:com.revolucion.secretwit.twitter.TwitterClient.java

License:Open Source License

private void init() {
    logger.info("Starting Twitter client.");

    TwitterFactory factory = new TwitterFactory();
    twitter = factory.getInstance();/*  w  w w.ja va  2  s . c o m*/

    twitter.setOAuthConsumer(consumerKey, consumerSecret);

    logger.info("OAuth consumer initialized.");

    AccessToken accessToken = loadAccessToken();
    if (accessToken != null) {
        twitter.setOAuthAccessToken(accessToken);
        logger.info("Loaded access token.");
        // signedIn = true;
        try {
            user = twitter.verifyCredentials();
        } catch (TwitterException e) {
            logger.error(e.getMessage());
        }
    }
}

From source file:com.rhymestore.twitter.AccessTokenGenerator.java

License:Open Source License

/**
 * Interactive {@link AccessToken} generation.
 * //from  www  .j  a va2  s  .c o  m
 * @param args No args are required.
 * @throws Exception If the token cannot be generated.
 */
public static void main(final String... args) throws Exception {
    Twitter twitter = new TwitterFactory().getInstance();
    RequestToken requestToken = twitter.getOAuthRequestToken();

    // Ask for the PIN
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    AccessToken accessToken = null;

    System.out.print("Open the following URL and grant access to your account: ");
    System.out.println(requestToken.getAuthorizationURL());
    System.out.print("Enter the PIN: ");

    accessToken = twitter.getOAuthAccessToken(requestToken, br.readLine());

    System.out.println("AccessToken Key: " + accessToken.getToken());
    System.out.println("AccessToken Secret: " + accessToken.getTokenSecret());
}

From source file:com.ridhofkr.hanacaraka.connector.twitter.TwitterApp.java

License:Apache License

public TwitterApp(Activity context, String consumerKey, String secretKey) {
    this.context = context;

    mTwitter = new TwitterFactory().getInstance();
    mSession = new TwitterSession(context);
    mProgressDlg = new ProgressDialog(context);

    mProgressDlg.requestWindowFeature(Window.FEATURE_NO_TITLE);

    mConsumerKey = consumerKey;/*from  ww w  .  j  a  va  2 s.  c  o  m*/
    mSecretKey = secretKey;

    mHttpOauthConsumer = new CommonsHttpOAuthConsumer(mConsumerKey, mSecretKey);

    String request_url = TWITTER_REQUEST_URL;
    String access_token_url = TWITTER_ACCESS_TOKEN_URL;
    String authorize_url = TWITTER_AUTHORZE_URL;

    mHttpOauthprovider = new DefaultOAuthProvider(request_url, access_token_url, authorize_url);
    mAccessToken = mSession.getAccessToken();

    configureToken();
}

From source file:com.robertszkutak.androidexamples.twitterexample.AndroidTwitterExample.java

License:Open Source License

public boolean isAuthenticated() {
    if (token == null || secret == null || token == "" || secret == "") {
        debug = "";
        debugStatus.setText(debug);/*from  w  w  w  .j  ava 2  s. c  om*/
        return false;
    } else {
        AccessToken a = new AccessToken(token, secret);
        Twitter twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
        twitter.setOAuthAccessToken(a);

        try {
            twitter.getAccountSettings();
            return true;
        } catch (TwitterException e) {
            debug += "\n\n" + e.getMessage();
            debugStatus.setText(debug);
            return false;
        }
    }
}

From source file:com.robertszkutak.androidexamples.twitterexample.AndroidTwitterExample.java

License:Open Source License

public void sendTweetToTwitter(String msg) throws Exception {
    AccessToken a = new AccessToken(token, secret);
    Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
    twitter.setOAuthAccessToken(a);/*from  ww  w.ja  v a 2 s.  c o  m*/
    twitter.updateStatus(msg);
}

From source file:com.schnee.tweetgeister.visualization.TweetgeisterBalloonLayout.java

License:Open Source License

/**
 * /* w  w  w . ja va  2s.  co m*/
 */
private void createTree() {

    try {
        Twitter twitter = new TwitterFactory().getInstance();

        List<Tweet> allTweets = new ArrayList<Tweet>();
        String qString = "bp";
        Query query = new Query(qString);
        query.setRpp(100);
        query.setLang("en");

        int hits = 100;
        int page = 1;
        while (allTweets.size() < 1400 && page < 16 && hits == 100) {

            query.setPage(page);

            // System.out.println(query.toString());
            QueryResult result = twitter.search(query);
            List<Tweet> tweets = result.getTweets();
            allTweets.addAll(tweets);
            hits = tweets.size();
            System.out.println("page: " + page + " hits: " + hits + " all tweets:" + allTweets.size());
            page++;
        }
        System.out.println("hits: " + allTweets.size());

        Set<CharSequence> inputSet = new HashSet<CharSequence>();

        for (Tweet tweet : allTweets) {
            inputSet.add(tweet.getText());
        }

        Clusterer cl = new Clusterer(inputSet, TokenizedCharSequence.TOKENIZER_FACTORY);

        com.schnee.tweetgeister.data.Tree<CharSequence> mindmap = cl.buildTree();

        fillTree(mindmap);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.SocialAccess.TwitterSearch.java

public static void searchWhomUserFollow() {

    List<String[]> data = CSVutil.readCSV(Constants.CSV_FILE_NAME);
    String[] people = data.get(0);
    System.out.print(people[0]);/*w  w w .java2s.  c  om*/

    try {
        Twitter twitter = new TwitterFactory().getInstance();
        long cursor = -1;
        IDs ids;
        System.out.println("Listing following ids.");
        do {

            ids = twitter.getFriendsIDs("derekmizak", cursor);

            for (long id : ids.getIDs()) {

                System.out.println(id);
            }
        } while ((cursor = ids.getNextCursor()) != 0);
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get friends' ids: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:com.SocialAccess.TwitterSearch.java

public static void ShowFriendsListNames() {
    List<String[]> data = CSVutil.readCSV(Constants.CSV_FILE_NAME);
    String[] people = data.get(0);
    try {/*from w  ww . j  a  v a  2 s .  c o  m*/
        Twitter twitter = new TwitterFactory().getInstance();
        ResponseList<Friendship> friendships = twitter.lookupFriendships(people);
        for (Friendship friendship : friendships) {
            System.out.println("@" + friendship.getScreenName() + " following: " + friendship.isFollowing()
                    + " followed_by: " + friendship.isFollowedBy());
        }
        System.out.println("Successfully looked up friendships [" + people[0] + "].");
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to lookup friendships: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:com.SocialAccess.TwitterSearch.java

public static String[] FriendshipStatus(String[] args) {

    if (args.length < 2) {
        System.out.println("Usage: [source screen name] [target screen name]");
        System.exit(-1);/* w ww .  j  a  va  2s .  co m*/
    }

    String[] lineConnection = new String[2];
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        Relationship relationship = twitter.showFriendship(args[0], args[1]);

        lineConnection[0] = args[0] + " is followed by " + args[1] + " "
                + relationship.isSourceFollowedByTarget();
        lineConnection[1] = args[0] + " is following " + args[1] + " " + relationship.isSourceFollowingTarget();

    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to show friendship: " + te.getMessage());
        System.exit(-1);
    }
    return lineConnection;
}