Example usage for twitter4j.conf ConfigurationBuilder setOAuthAccessToken

List of usage examples for twitter4j.conf ConfigurationBuilder setOAuthAccessToken

Introduction

In this page you can find the example usage for twitter4j.conf ConfigurationBuilder setOAuthAccessToken.

Prototype

public ConfigurationBuilder setOAuthAccessToken(String oAuthAccessToken) 

Source Link

Usage

From source file:com.learnncode.twitter.Utilities.TwitterHelper.java

License:Apache License

public static Twitter getTwitterInstanceWithoutAuthentication(Context _Context) {
    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
    configurationBuilder.setOAuthConsumerKey(_Context.getResources().getString(R.string.twitter_consumer_key));
    configurationBuilder/*from  www .j a va 2s. co  m*/
            .setOAuthConsumerSecret(_Context.getResources().getString(R.string.twitter_consumer_secret));
    configurationBuilder.setOAuthAccessToken(_Context.getResources().getString(R.string.twitter_access_token));
    configurationBuilder
            .setOAuthAccessTokenSecret(_Context.getResources().getString(R.string.twitter_access_token_secret));
    Configuration configuration = configurationBuilder.build();
    return new TwitterFactory(configuration).getInstance();
}

From source file:com.left8.evs.utilities.dsretriever.TweetsRetriever.java

License:Open Source License

/**
 * Method to get authorization from Twitter API.
 * @return A ConfigurationBuilder object.
 *//*from   w ww  .ja  v  a2 s  .c om*/
public ConfigurationBuilder getAuthorization() {
    ConfigurationBuilder cb = new ConfigurationBuilder();

    Config conf;
    try {
        conf = new Config();
    } catch (IOException e) {
        Logger.getLogger(TweetsRetriever.class.getName()).log(Level.SEVERE, null, e);
        return null;
    }

    cb.setOAuthConsumerKey(conf.getConsumerKey());
    cb.setOAuthConsumerSecret(conf.getConsumerSecret());
    cb.setOAuthAccessToken(conf.getAccessToken());
    cb.setOAuthAccessTokenSecret(conf.getAccessTokenSecret());

    return cb;
}

From source file:com.mycompany.mech2015task.App.java

public static void main(String[] args) throws Exception {
    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setOAuthConsumerKey(CONSUMER_KEY);
    builder.setOAuthConsumerSecret(CONSUMER_SECRET);
    builder.setOAuthAccessToken(ACCESS_TOKEN);
    builder.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);

    // ??Twitter4J??API???betastream.twitter.com???????
    builder.setUserStreamBaseURL("https://userstream.twitter.com/2/");

    // Configuration?
    Configuration configuration = builder.build();

    /*        Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY)
        .setOAuthConsumerSecret(CONSUMER_SECRET)
        .setOAuthAccessToken(ACCESS_TOKEN)
        .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET)
        .build();//from  w  w  w .j av a  2 s . c om
    */
    TwitterStream twStream = new TwitterStreamFactory(configuration).getInstance();
    //TwitterStream twStream = new TwitterStreamFactory().getInstance();
    twStream.addListener(new MyStatusListener());
    twStream.user();
    //twStream.sample();
}

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   www. ja va  2 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  . ja v 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   w  w w.jav  a 2s .co 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.pulzitinc.flume.source.TwitterSource.java

License:Apache License

/**
 * The initialization method for the Source. The context contains all the
 * Flume configuration info, and can be used to retrieve any configuration
 * values necessary to set up the Source.
 *//*  w ww .  ja va  2  s  .  c o  m*/
@Override
public void configure(Context context) {

    logger.info(context.toString());

    consumerKey = context.getString(TwitterSourceConstants.CONSUMER_KEY_KEY);
    consumerSecret = context.getString(TwitterSourceConstants.CONSUMER_SECRET_KEY);
    accessToken = context.getString(TwitterSourceConstants.ACCESS_TOKEN_KEY);
    accessTokenSecret = context.getString(TwitterSourceConstants.ACCESS_TOKEN_SECRET_KEY);

    String accountIdsString = context.getString(TwitterSourceConstants.ACCOUNT_IDS_KEY, "");
    if (accountIdsString.trim().length() == 0) {
        throw new IllegalStateException("No accounts to follow provided");
    } else {
        String[] accountIds = accountIdsString.split(",");
        accountsToFollow = new long[accountIds.length];
        for (int i = 0; i < accountIds.length; i++) {
            accountsToFollow[i] = Long.valueOf(accountIds[i]);
        }
    }

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey(consumerKey);
    cb.setOAuthConsumerSecret(consumerSecret);
    cb.setOAuthAccessToken(accessToken);
    cb.setOAuthAccessTokenSecret(accessTokenSecret);
    cb.setJSONStoreEnabled(true);
    cb.setIncludeEntitiesEnabled(true);

    twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
}

From source file:com.rowland.hashtrace.utility.Utility.java

License:Apache License

public static Twitter getTwitter() {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey(Constants.TWITTER_CONSUMER_KEY);
    cb.setOAuthConsumerSecret(Constants.TWITTER_CONSUMER_SECRET);
    cb.setOAuthAccessToken(Constants.TWITTER_ACCESS_TOKEN);
    cb.setOAuthAccessTokenSecret(Constants.TWITTER_TOKEN_SECRET).setHttpConnectionTimeout(100000);
    //cb.setUseSSL(true);

    /*/*from   ww w .  ja v  a 2 s.  co  m*/
     * if() { cb.setHttpProxyHost(httpProxyHost);
     * cb.setHttpProxyPort(httpProxyPort);
     * cb.setHttpProxyUser(httpProxyUser);
     * cb.setHttpProxyPassword(httpProxyPassword);
     *
     * }
     */

    return new TwitterFactory(cb.build()).getInstance();
}

From source file:com.sinfonier.drains.TweetIt.java

License:Apache License

@Override
public void userexecute() {

    ConfigurationBuilder config = new ConfigurationBuilder();
    config.setOAuthConsumerKey(this.CONSUMER_KEY);
    config.setOAuthConsumerSecret(this.CONSUMER_SECRET);
    config.setOAuthAccessToken(this.ACCESS_TOKEN);
    config.setOAuthAccessTokenSecret(this.ACCESS_TOKEN_SECRET);
    config.setJSONStoreEnabled(true);//from  w  w w.  java 2  s.c  o  m
    config.setIncludeEntitiesEnabled(true);
    try {
        TwitterFactory tf = new TwitterFactory(config.build());
        Twitter twitter = tf.getInstance();
        Status status = twitter.updateStatus(this.customText + " " + this.getField(this.fieldToTweet));
        System.out.println("Successfully updated the status to [" + status.getText() + "].");
    } catch (TwitterException te) {
        te.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.sinfonier.spouts.Twitter.java

License:Apache License

/**
 * Create a TwitterStream. Requires Twitter API keys.
 * //from ww w  . ja  va2 s  .  c  o  m
 * @return {@link twitter4j.TwitterStream}
 */
private TwitterStream createTwitterStream() {

    ConfigurationBuilder config = new ConfigurationBuilder();
    config.setOAuthConsumerKey(xml.get(CONSUMER_KEY, true));
    config.setOAuthConsumerSecret(xml.get(CONSUMER_SECRET, true));
    config.setOAuthAccessToken(xml.get(ACCESS_TOKEN, true));
    config.setOAuthAccessTokenSecret(xml.get(ACCESS_TOKEN_SECRET, true));
    config.setJSONStoreEnabled(true);
    config.setIncludeEntitiesEnabled(true);

    return new TwitterStreamFactory(config.build()).getInstance();
}