List of usage examples for twitter4j.conf ConfigurationBuilder build
public Configuration build()
From source file:com.mycompany.omnomtweets.Search.java
public static void main(String[] args) throws TwitterException { System.setProperty("twitter4j.loggerFactory", "twitter4j.internal.logging.NullLoggerFactory"); if (args.length >= 1) { candidate = Candidate.valueOf(args[0]); System.out.println("Getting tweets on " + candidate.name); } else {// ww w .j av a 2 s. c o m //System.out.println("You didn't give me a candidate, so you get TRUMP"); } //System.out.println("Starting at: " + sdf.format(new Date())); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(candidate.consumerKey) .setOAuthConsumerSecret(candidate.consumerSecret).setOAuthAccessToken(candidate.accessToken) .setOAuthAccessTokenSecret(candidate.accessTokenSecret); final ArrayList<Status> statuses = new ArrayList<>(); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); StatusListener listener = new StatusListener() { public void onStatus(Status status) { statuses.add(status); writeToStdErr(status); //Removed because writing to STD OUT instead. //System.out.println("statuses: " + statuses.size()); if (statuses.size() >= 100) { /*System.out.println("Writing " + statuses.size() + " to file " + "at: " + sdf.format(new Date()));*/ writeTweetsToFile(statuses, candidate.name + "StreamTweets.txt"); statuses.clear(); } } public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { //System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); } public void onTrackLimitationNotice(int numberOfLimitedStatuses) { //System.out.println("Got track limitation notice:" + numberOfLimitedStatuses); } public void onScrubGeo(long userId, long upToStatusId) { //System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); } public void onException(Exception ex) { //ex.printStackTrace(); } @Override public void onStallWarning(StallWarning sw) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }; FilterQuery fq = new FilterQuery(); String keywords[] = candidate.aliases; fq.track(keywords); twitterStream.addListener(listener); twitterStream.filter(fq); }
From source file:com.mycompany.twitterapp.TwitterApp.java
public void buildConfig() { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("").setOAuthConsumerSecret("").setOAuthAccessToken("") .setOAuthAccessTokenSecret(""); TwitterFactory tf = new TwitterFactory(cb.build()); twitter = tf.getInstance();//from w w w . jav a2 s. c o m }
From source file:com.mycompany.twitterproductanalysistool.TwitterAPI.java
public TwitterAPI() { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("ogluOfaFe00xjVQ5WfynhkWQY") .setOAuthConsumerSecret("l4yL7iz6J1zEBaY4ownHu9VnIu2zSLuELVyzxykgoSEF3vzSup") .setOAuthAccessToken("1101064884-uMgjzYHHo82fRZdwWbvjNWs8ZKqoZj0xsKB4jnO") .setOAuthAccessTokenSecret("WyCAT00L4FK0lOQiXhshNXpS5bHVFoVsaoYGlJYVylWYm"); TwitterFactory tf = new TwitterFactory(cb.build()); twitter = tf.getInstance();/* w w w . j a va2 s .com*/ }
From source file:com.mycompany.twittersearch.SignIn.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//from w w w. ja v a 2 s .com * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("").setOAuthConsumerSecret(""); Twitter twitter = new TwitterFactory(cb.build()).getInstance(); request.getSession().setAttribute("twitter", twitter); try { StringBuffer callbackURL = request.getRequestURL(); System.out.println("TwitterLoginServlet:callbackURL:" + callbackURL); int index = callbackURL.lastIndexOf("/"); callbackURL.replace(index, callbackURL.length(), "").append("/Callback"); RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL.toString()); request.getSession().setAttribute("requestToken", requestToken); System.out.println("requestToken.getAuthenticationURL():" + requestToken.getAuthenticationURL()); response.sendRedirect(requestToken.getAuthenticationURL()); } catch (TwitterException e) { throw new ServletException(e); } }
From source file:com.mysportsshare.mysportscast.utils.HelperMethods.java
License:Apache License
public static void postToTwitter(Context context, final Activity callingActivity, final String message, final TwitterCallback postResponse) { SharedPreferences mSharedPref = context.getSharedPreferences("Android_Twitter_Preferences", Context.MODE_PRIVATE); if (mSharedPref.getString(Constants.KEY_TWITTER_ACCESS_TOKEN, null) == null) { postResponse.onFinsihed(false);//from w w w. j a v a2 s .c om return; } ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.setOAuthConsumerKey(HomeActivity.CONSUMER_KEY); configurationBuilder.setOAuthConsumerSecret(HomeActivity.CONSUMER_SECRET); configurationBuilder.setOAuthAccessToken(HomeActivity.getAccessToken((context))); configurationBuilder.setOAuthAccessTokenSecret(HomeActivity.getAccessTokenSecret(context)); Configuration configuration = configurationBuilder.build(); final Twitter twitter = new TwitterFactory(configuration).getInstance(); new Thread(new Runnable() { private double x; @Override public void run() { boolean success = true; try { x = Math.random(); twitter.updateStatus(message); } catch (TwitterException e) { e.printStackTrace(); success = false; } final boolean finalSuccess = success; callingActivity.runOnUiThread(new Runnable() { @Override public void run() { postResponse.onFinsihed(finalSuccess); } }); } }).start(); }
From source file:com.mysportsshare.mysportscast.utils.HelperMethods.java
License:Apache License
public static void postToTwitterWithImage(Context context, final Activity callingActivity, final String imageUrl, final String message, final TwitterCallback postResponse) { SharedPreferences mSharedPref = context.getSharedPreferences("Android_Twitter_Preferences", Context.MODE_PRIVATE); if (mSharedPref.getString(Constants.KEY_TWITTER_ACCESS_TOKEN, null) == null) { postResponse.onFinsihed(false);// www. j av a 2s .c o m return; } ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.setOAuthConsumerKey(HomeActivity.CONSUMER_KEY); configurationBuilder.setOAuthConsumerSecret(HomeActivity.CONSUMER_SECRET); configurationBuilder.setOAuthAccessToken(HomeActivity.getAccessToken((context))); configurationBuilder.setOAuthAccessTokenSecret(HomeActivity.getAccessTokenSecret(context)); Configuration configuration = configurationBuilder.build(); final Twitter twitter = new TwitterFactory(configuration).getInstance(); final File file = new File(imageUrl); new Thread(new Runnable() { @Override public void run() { boolean success = true; try { if (file.exists()) { StatusUpdate status = new StatusUpdate(message); status.setMedia(file); twitter.updateStatus(status); } else { Log.d(TAG, "----- Invalid File ----------"); success = false; } } catch (Exception e) { e.printStackTrace(); success = false; } final boolean finalSuccess = success; callingActivity.runOnUiThread(new Runnable() { @Override public void run() { postResponse.onFinsihed(finalSuccess); } }); } }).start(); }
From source file:com.narvis.frontend.twitter.AccessTwitter.java
License:Open Source License
private Pair<TwitterStream, Twitter> loadAccessTwitter(String token, String tokenSecret, String consumerKey, String consumerSecret) {/*from www .j a v a 2 s . c o m*/ ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true); cb.setOAuthConsumerKey(consumerKey); cb.setOAuthConsumerSecret(consumerSecret); cb.setOAuthAccessToken(token); cb.setOAuthAccessTokenSecret(tokenSecret); Configuration authConf = cb.build(); return new Pair(new TwitterStreamFactory(authConf).getInstance(), new TwitterFactory(authConf).getInstance()); }
From source file:com.nookdevs.twook.activities.SettingsActivity.java
License:Open Source License
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // set the title at the top of the eink screen final Resources res = getResources(); NAME = res.getText(R.string.app_name).toString() + res.getText(R.string.title_separator).toString() + res.getText(R.string.settings_title).toString(); setContentView(R.layout.settings);//from w w w .j a va 2 s . c o m final InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); // get the buttons txtPin = (EditText) findViewById(R.id.pin); // set listeners txtPin.setOnKeyListener(credentialsListener); WebView engine = (WebView) findViewById(R.id.web_frame); engine.setOnKeyListener(credentialsListener); // the submit is done through jscript, so we'd better allow it... engine.getSettings().setJavaScriptEnabled(true); // set credentials from Settings Settings settings = Settings.getSettings(this); ConfigurationBuilder confBuilder = settings.getConfiguration(); // this doesn't seem to be used by the authorization object... // confBuilder.setUser(username); // confBuilder.setPassword(password); Configuration conf = confBuilder.build(); mAuth = new OAuthAuthorization(conf); TextView validation = (TextView) findViewById(R.id.validation); try { // ask for the request token first, so we can ask the user for // authorization RequestToken requestToken = mAuth.getOAuthRequestToken(); Log.d(TAG, "Request token: " + requestToken.toString()); // this includes the oauth_token parameter and everything Log.d(TAG, "Sending user to " + requestToken.getAuthorizationURL()); engine.loadUrl(requestToken.getAuthorizationURL()); /* * Intent myIntent = new Intent(Intent.ACTION_VIEW, * Uri.parse(requestToken.getAuthorizationURL())); * startActivity(myIntent); */ Log.d(TAG, "Will authorize to " + conf.getOAuthAccessTokenURL()); } catch (TwitterException excep) { // TODO: better validation here validation.setText("Unknown error: " + excep.getMessage()); } // spiner for selecting refresh times // Spinner refresh = (Spinner) findViewById(R.id.refresh_time); // ArrayAdapter adapter = ArrayAdapter.createFromResource( // this, R.array.refresh_times, android.R.layout.simple_spinner_item); // adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // refresh.setAdapter(adapter); }
From source file:com.oldterns.vilebot.handlers.user.UrlTweetAnnouncer.java
License:Open Source License
/** * Accesses the source of a HTML page and looks for a title element * /*from w w w. j a v a 2 s.co m*/ * @param url http tweet String * @return String of text which represents the tweet. Empty if error. */ private String scrapeURLHTMLTitle(String url) { String text = ""; URL page; try { page = new URL(url); } catch (MalformedURLException x) { // System.err.format("scrapeURLHTMLTitle new URL error: %s%n", x); return text; } //split the url into pieces, change the request based on what we have String parts[] = url.split("/"); int userPosition = 0; long tweetID = 0; for (int i = 0; i < parts.length; i++) { if (parts[i].toString().equals("twitter.com")) userPosition = i + 1; if (parts[i].toString().equals("status") || parts[i].toString().equals("statuses")) tweetID = Long.valueOf(parts[i + 1].toString()).longValue(); } if (userPosition == 0) return text; else { try { String consumerKey = ""; //may be known as 'API key' String consumerSecret = ""; //may be known as 'API secret' String accessToken = ""; //may be known as 'Access token' String accessTokenSecret = ""; //may be known as 'Access token secret' ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret) .setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessTokenSecret); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance(); if (tweetID != 0) //tweet of the twitter.com/USERID/status/TWEETID variety { Status status = twitter.showStatus(tweetID); return (status.getUser().getName() + ": " + status.getText()); } else //just the user is given, ie, twitter.com/USERID { User user = twitter.showUser(parts[userPosition].toString()); if (!user.getDescription().isEmpty()) //the user has a description return ("Name: " + user.getName() + " | " + user.getDescription() + "\'\nLast Tweet: \'" + user.getStatus().getText()); else //the user doesn't have a description, don't print it return ("Name: " + user.getName() + "\'\nLast Tweet: \'" + user.getStatus().getText()); } } catch (TwitterException x) { return text; } } }
From source file:com.ontotext.s4.TwitterVisualization.downloadTweets.SearchTweets.java
License:Open Source License
/** * Creates and configures Twitter client instance with provided credentials. * //from w w w. j a v a2s. co m * @return Twitter instance */ private Twitter ConfigurateTwitterAccount() { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret) .setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessTokenSecret) .setJSONStoreEnabled(true); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance(); return twitter; }