List of usage examples for twitter4j.conf ConfigurationBuilder build
public Configuration build()
From source file:de.jetsli.twitter.TwitterSearch.java
License:Apache License
/** * Connect with twitter to get a new personalized twitter4j instance. * * @throws RuntimeException if verification or connecting failed *///w w w .j a v a 2s . com public TwitterSearch initTwitter4JInstance(String token, String tokenSecret, boolean verify) { if (consumerKey == null) throw new NullPointerException("Please use init consumer settings!"); setupProperties(); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret) .setOAuthAccessToken(token).setOAuthAccessTokenSecret(tokenSecret); TwitterFactory tf = new TwitterFactory(cb.build()); twitter = tf.getInstance(); try { // RequestToken requestToken = t.getOAuthRequestToken(); // System.out.println("TW-URL:" + requestToken.getAuthorizationURL()); if (verify) twitter.verifyCredentials(); String str = twitter.getScreenName(); logger.info("create new TwitterSearch for " + str + " with verifification:" + verify); } catch (TwitterException ex) { // if (checkAndWaitIfRateLimited("initTwitter", ex)) // return this; throw new RuntimeException(ex); } return this; }
From source file:de.vanita5.twittnuker.activity.support.SignInActivity.java
License:Open Source License
private Configuration getConfiguration() { final ConfigurationBuilder cb = new ConfigurationBuilder(); final boolean enable_gzip_compressing = mPreferences.getBoolean(KEY_GZIP_COMPRESSING, false); final boolean ignore_ssl_error = mPreferences.getBoolean(KEY_IGNORE_SSL_ERROR, false); final boolean enable_proxy = mPreferences.getBoolean(KEY_ENABLE_PROXY, false); cb.setHostAddressResolverFactory(new TwidereHostResolverFactory(mApplication)); cb.setHttpClientFactory(new TwidereHttpClientFactory(mApplication)); if (Utils.isOfficialConsumerKeySecret(this, mConsumerKey, mConsumerSecret)) { Utils.setMockOfficialUserAgent(this, cb); } else {/* w w w .j av a2 s . c o m*/ Utils.setUserAgent(this, cb); } if (!isEmpty(mAPIUrlFormat)) { final String versionSuffix = mNoVersionSuffix ? null : "/1.1/"; cb.setRestBaseURL(Utils.getApiUrl(mAPIUrlFormat, "api", versionSuffix)); cb.setOAuthBaseURL(Utils.getApiUrl(mAPIUrlFormat, "api", "/oauth/")); cb.setUploadBaseURL(Utils.getApiUrl(mAPIUrlFormat, "upload", versionSuffix)); if (!mSameOAuthSigningUrl) { cb.setSigningRestBaseURL(DEFAULT_SIGNING_REST_BASE_URL); cb.setSigningOAuthBaseURL(DEFAULT_SIGNING_OAUTH_BASE_URL); cb.setSigningUploadBaseURL(DEFAULT_SIGNING_UPLOAD_BASE_URL); } } if (isEmpty(mConsumerKey) || isEmpty(mConsumerSecret)) { cb.setOAuthConsumerKey(TWITTER_CONSUMER_KEY_2); cb.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET_2); } else { cb.setOAuthConsumerKey(mConsumerKey); cb.setOAuthConsumerSecret(mConsumerSecret); } cb.setGZIPEnabled(enable_gzip_compressing); cb.setIgnoreSSLError(ignore_ssl_error); if (enable_proxy) { final String proxy_host = mPreferences.getString(KEY_PROXY_HOST, null); final int proxy_port = ParseUtils.parseInt(mPreferences.getString(KEY_PROXY_PORT, "-1")); if (!isEmpty(proxy_host) && proxy_port > 0) { cb.setHttpProxyHost(proxy_host); cb.setHttpProxyPort(proxy_port); } } return cb.build(); }
From source file:de.vanita5.twittnuker.util.Utils.java
License:Open Source License
public static HttpClientWrapper getHttpClient(final Context context, final int timeoutMillis, final boolean ignoreSslError, final Proxy proxy, final HostAddressResolverFactory resolverFactory, final String userAgent, final boolean twitterClientHeader) { final ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setHttpConnectionTimeout(timeoutMillis); cb.setIgnoreSSLError(ignoreSslError); cb.setIncludeTwitterClientHeader(twitterClientHeader); if (proxy != null && !Proxy.NO_PROXY.equals(proxy)) { final SocketAddress address = proxy.address(); if (address instanceof InetSocketAddress) { cb.setHttpProxyHost(((InetSocketAddress) address).getHostName()); cb.setHttpProxyPort(((InetSocketAddress) address).getPort()); }/*from ww w .j a v a 2 s. co m*/ } cb.setHostAddressResolverFactory(resolverFactory); if (userAgent != null) { cb.setHttpUserAgent(userAgent); } cb.setHttpClientFactory(new TwidereHttpClientFactory(context)); return new HttpClientWrapper(cb.build()); }
From source file:de.vanita5.twittnuker.util.Utils.java
License:Open Source License
public static Authorization getTwitterAuthorization(final Context context, final AccountWithCredentials account) { if (context == null || account == null) return null; switch (account.auth_type) { case Accounts.AUTH_TYPE_OAUTH: case Accounts.AUTH_TYPE_XAUTH: { final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); // Here I use old consumer key/secret because it's default // key for older // versions final String prefConsumerKey = prefs.getString(KEY_CONSUMER_KEY, TWITTER_CONSUMER_KEY_2); final String prefConsumerSecret = prefs.getString(KEY_CONSUMER_SECRET, TWITTER_CONSUMER_SECRET_2); final ConfigurationBuilder cb = new ConfigurationBuilder(); if (!isEmpty(account.api_url_format)) { final String versionSuffix = account.no_version_suffix ? null : "/1.1/"; cb.setRestBaseURL(getApiUrl(account.api_url_format, "api", versionSuffix)); cb.setOAuthBaseURL(getApiUrl(account.api_url_format, "api", "/oauth/")); cb.setUploadBaseURL(getApiUrl(account.api_url_format, "upload", versionSuffix)); if (!account.same_oauth_signing_url) { cb.setSigningRestBaseURL(DEFAULT_SIGNING_REST_BASE_URL); cb.setSigningOAuthBaseURL(DEFAULT_SIGNING_OAUTH_BASE_URL); cb.setSigningUploadBaseURL(DEFAULT_SIGNING_UPLOAD_BASE_URL); }/* w ww . j a v a2 s . c om*/ } if (!isEmpty(account.consumer_key) && !isEmpty(account.consumer_secret)) { cb.setOAuthConsumerKey(account.consumer_key); cb.setOAuthConsumerSecret(account.consumer_secret); } else if (!isEmpty(prefConsumerKey) && !isEmpty(prefConsumerSecret)) { cb.setOAuthConsumerKey(prefConsumerKey); cb.setOAuthConsumerSecret(prefConsumerSecret); } else { cb.setOAuthConsumerKey(TWITTER_CONSUMER_KEY_2); cb.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET_2); } final OAuthAuthorization auth = new OAuthAuthorization(cb.build()); auth.setOAuthAccessToken(new AccessToken(account.oauth_token, account.oauth_token_secret)); return auth; } case Accounts.AUTH_TYPE_BASIC: { final String screenName = account.screen_name; final String username = account.basic_auth_username; final String loginName = username != null ? username : screenName; final String password = account.basic_auth_password; if (isEmpty(loginName) || isEmpty(password)) return null; return new BasicAuthorization(loginName, password); } default: { return null; } } }
From source file:de.vanita5.twittnuker.util.Utils.java
License:Open Source License
public static Authorization getTwitterAuthorization(final Context context, final long accountId) { final String where = Where.equals(new Column(Accounts.ACCOUNT_ID), accountId).getSQL(); final Cursor c = ContentResolverUtils.query(context.getContentResolver(), Accounts.CONTENT_URI, Accounts.COLUMNS, where, null, null); if (c == null) return null; try {/*from ww w .j a va 2 s .com*/ if (!c.moveToFirst()) return null; switch (c.getInt(c.getColumnIndexOrThrow(Accounts.AUTH_TYPE))) { case Accounts.AUTH_TYPE_OAUTH: case Accounts.AUTH_TYPE_XAUTH: { final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); final String prefConsumerKey = prefs.getString(KEY_CONSUMER_KEY, TWITTER_CONSUMER_KEY_2); final String prefConsumerSecret = prefs.getString(KEY_CONSUMER_SECRET, TWITTER_CONSUMER_SECRET_2); final ConfigurationBuilder cb = new ConfigurationBuilder(); final String apiUrlFormat = c.getString(c.getColumnIndex(Accounts.API_URL_FORMAT)); final String consumerKey = trim(c.getString(c.getColumnIndex(Accounts.CONSUMER_KEY))); final String consumerSecret = trim(c.getString(c.getColumnIndex(Accounts.CONSUMER_SECRET))); final boolean sameOAuthSigningUrl = c .getInt(c.getColumnIndex(Accounts.SAME_OAUTH_SIGNING_URL)) == 1; if (!isEmpty(apiUrlFormat)) { cb.setRestBaseURL(getApiUrl(apiUrlFormat, "api", "/1.1/")); cb.setOAuthBaseURL(getApiUrl(apiUrlFormat, "api", "/oauth/")); cb.setUploadBaseURL(getApiUrl(apiUrlFormat, "upload", "/1.1/")); if (!sameOAuthSigningUrl) { cb.setSigningRestBaseURL(DEFAULT_SIGNING_REST_BASE_URL); cb.setSigningOAuthBaseURL(DEFAULT_SIGNING_OAUTH_BASE_URL); cb.setSigningUploadBaseURL(DEFAULT_SIGNING_UPLOAD_BASE_URL); } } if (!isEmpty(consumerKey) && !isEmpty(consumerSecret)) { cb.setOAuthConsumerKey(consumerKey); cb.setOAuthConsumerSecret(consumerSecret); } else if (!isEmpty(prefConsumerKey) && !isEmpty(prefConsumerSecret)) { cb.setOAuthConsumerKey(prefConsumerKey); cb.setOAuthConsumerSecret(prefConsumerSecret); } else { cb.setOAuthConsumerKey(TWITTER_CONSUMER_KEY_2); cb.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET_2); } final OAuthAuthorization auth = new OAuthAuthorization(cb.build()); final String token = c.getString(c.getColumnIndexOrThrow(Accounts.OAUTH_TOKEN)); final String tokenSecret = c.getString(c.getColumnIndexOrThrow(Accounts.OAUTH_TOKEN_SECRET)); auth.setOAuthAccessToken(new AccessToken(token, tokenSecret)); return auth; } case Accounts.AUTH_TYPE_BASIC: { final String screenName = c.getString(c.getColumnIndexOrThrow(Accounts.SCREEN_NAME)); final String username = c.getString(c.getColumnIndexOrThrow(Accounts.BASIC_AUTH_USERNAME)); final String loginName = username != null ? username : screenName; final String password = c.getString(c.getColumnIndexOrThrow(Accounts.BASIC_AUTH_PASSWORD)); if (isEmpty(loginName) || isEmpty(password)) return null; return new BasicAuthorization(loginName, password); } default: { return null; } } } finally { c.close(); } }
From source file:de.vanita5.twittnuker.util.Utils.java
License:Open Source License
public static Twitter getTwitterInstance(final Context context, final long accountId, final boolean includeEntities, final boolean includeRetweets, final boolean apacheHttp, final String mediaProvider, final String mediaProviderAPIKey) { if (context == null) return null; final TwittnukerApplication app = TwittnukerApplication.getInstance(context); final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); final int connection_timeout = prefs.getInt(KEY_CONNECTION_TIMEOUT, 10) * 1000; final boolean enableGzip = prefs.getBoolean(KEY_GZIP_COMPRESSING, true); final boolean ignoreSSLError = prefs.getBoolean(KEY_IGNORE_SSL_ERROR, false); final boolean enableProxy = prefs.getBoolean(KEY_ENABLE_PROXY, false); // Here I use old consumer key/secret because it's default key for older // versions// w ww. ja va 2s .c o m final String where = Where.equals(new Column(Accounts.ACCOUNT_ID), accountId).getSQL(); final Cursor c = ContentResolverUtils.query(context.getContentResolver(), Accounts.CONTENT_URI, Accounts.COLUMNS, where, null, null); if (c == null) return null; try { if (!c.moveToFirst()) return null; final ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setHostAddressResolverFactory(new TwidereHostResolverFactory(app)); if (apacheHttp) { cb.setHttpClientFactory(new TwidereHttpClientFactory(app)); } cb.setHttpConnectionTimeout(connection_timeout); cb.setGZIPEnabled(enableGzip); cb.setIgnoreSSLError(ignoreSSLError); if (enableProxy) { final String proxy_host = prefs.getString(KEY_PROXY_HOST, null); final int proxy_port = ParseUtils.parseInt(prefs.getString(KEY_PROXY_PORT, "-1")); if (!isEmpty(proxy_host) && proxy_port > 0) { cb.setHttpProxyHost(proxy_host); cb.setHttpProxyPort(proxy_port); } } final String prefConsumerKey = prefs.getString(KEY_CONSUMER_KEY, TWITTER_CONSUMER_KEY_2); final String prefConsumerSecret = prefs.getString(KEY_CONSUMER_SECRET, TWITTER_CONSUMER_SECRET_2); final String apiUrlFormat = c.getString(c.getColumnIndex(Accounts.API_URL_FORMAT)); final String consumerKey = trim(c.getString(c.getColumnIndex(Accounts.CONSUMER_KEY))); final String consumerSecret = trim(c.getString(c.getColumnIndex(Accounts.CONSUMER_SECRET))); final boolean sameOAuthSigningUrl = c.getInt(c.getColumnIndex(Accounts.SAME_OAUTH_SIGNING_URL)) == 1; final boolean noVersionSuffix = c.getInt(c.getColumnIndex(Accounts.NO_VERSION_SUFFIX)) == 1; if (!isEmpty(apiUrlFormat)) { final String versionSuffix = noVersionSuffix ? null : "/1.1/"; cb.setRestBaseURL(getApiUrl(apiUrlFormat, "api", versionSuffix)); cb.setOAuthBaseURL(getApiUrl(apiUrlFormat, "api", "/oauth/")); cb.setUploadBaseURL(getApiUrl(apiUrlFormat, "upload", versionSuffix)); if (!sameOAuthSigningUrl) { cb.setSigningRestBaseURL(DEFAULT_SIGNING_REST_BASE_URL); cb.setSigningOAuthBaseURL(DEFAULT_SIGNING_OAUTH_BASE_URL); cb.setSigningUploadBaseURL(DEFAULT_SIGNING_UPLOAD_BASE_URL); } } if (isOfficialConsumerKeySecret(context, consumerKey, consumerSecret)) { setMockOfficialUserAgent(context, cb); } else { setUserAgent(context, cb); } if (!isEmpty(mediaProvider)) { cb.setMediaProvider(mediaProvider); } if (!isEmpty(mediaProviderAPIKey)) { cb.setMediaProviderAPIKey(mediaProviderAPIKey); } cb.setIncludeEntitiesEnabled(includeEntities); cb.setIncludeRTsEnabled(includeRetweets); cb.setIncludeReplyCountEnabled(true); cb.setIncludeDescendentReplyCountEnabled(true); switch (c.getInt(c.getColumnIndexOrThrow(Accounts.AUTH_TYPE))) { case Accounts.AUTH_TYPE_OAUTH: case Accounts.AUTH_TYPE_XAUTH: { if (!isEmpty(consumerKey) && !isEmpty(consumerSecret)) { cb.setOAuthConsumerKey(consumerKey); cb.setOAuthConsumerSecret(consumerSecret); } else if (!isEmpty(prefConsumerKey) && !isEmpty(prefConsumerSecret)) { cb.setOAuthConsumerKey(prefConsumerKey); cb.setOAuthConsumerSecret(prefConsumerSecret); } else { cb.setOAuthConsumerKey(TWITTER_CONSUMER_KEY_2); cb.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET_2); } final String token = c.getString(c.getColumnIndexOrThrow(Accounts.OAUTH_TOKEN)); final String tokenSecret = c.getString(c.getColumnIndexOrThrow(Accounts.OAUTH_TOKEN_SECRET)); if (isEmpty(token) || isEmpty(tokenSecret)) return null; cb.setOAuthAccessToken(token); cb.setOAuthAccessTokenSecret(tokenSecret); return new TwitterFactory(cb.build()).getInstance(new AccessToken(token, tokenSecret)); } case Accounts.AUTH_TYPE_BASIC: { final String screenName = c.getString(c.getColumnIndexOrThrow(Accounts.SCREEN_NAME)); final String username = c.getString(c.getColumnIndexOrThrow(Accounts.BASIC_AUTH_USERNAME)); final String loginName = username != null ? username : screenName; final String password = c.getString(c.getColumnIndexOrThrow(Accounts.BASIC_AUTH_PASSWORD)); if (isEmpty(loginName) || isEmpty(password)) return null; return new TwitterFactory(cb.build()).getInstance(new BasicAuthorization(loginName, password)); } case Accounts.AUTH_TYPE_TWIP_O_MODE: { return new TwitterFactory(cb.build()).getInstance(new TwipOModeAuthorization()); } default: { return null; } } } finally { c.close(); } }
From source file:dhbw.clippinggorilla.external.twitter.TwitterUtils.java
/** * Configures Twitter API Keys/*from w w w. ja va 2 s .c o m*/ */ public static void config() { if (tf == null) { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true) .setOAuthConsumerKey(Props.getOrDefault("OAuthConsumerKey", "8MEY2XDvYPblVVQgFdSzSrydQ")) .setOAuthConsumerSecret(Props.getOrDefault("OAuthConsumerSecret", "IpKkXw2gvfJCEwjGv2Yz5LwRv0Zn1B9nMLkQVleldyhdVPgPVj")) .setOAuthAccessToken(Props.getOrDefault("OAuthAccessToken", "2646251996-E0Glh4yG12AyJ6DM7PCy9sTKhs0w8gP4yCFFvU2")) .setOAuthAccessTokenSecret(Props.getOrDefault("OAuthAccessTokenSecret", "8D4sZcF8trluR1dFhqxhVoaf0xsrwTvwX93qkIbKFFxKP")); tf = new TwitterFactory(cb.build()); } }
From source file:displaygrid.apps.team1100feed.Team1100TweetsServerApp.java
@Override public void init() { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true);//w w w . java 2 s . c o m cb.setOAuthConsumerKey(CONSUMER_KEY); cb.setOAuthConsumerSecret(CONSUMER_SECRET); cb.setOAuthAccessToken(ACCESS_TOKEN); cb.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET); streamTeam = new TwitterStreamFactory(cb.build()).getInstance(); streamTeam.addListener(this); streamTeam.user(); }
From source file:edu.allegheny.gatortweet.GetHomeTimeline.java
License:Apache License
public static void main(String[] args) { try {/*from w w w . j a v a2 s . co m*/ ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("rPtRCCRqdDyoxHS3E2UARA") .setOAuthConsumerSecret("hhDnR4NETStvN4F84km2xuBy3eXJ8l2FnjdL23YPs"); // gets Twitter instance with default credentials TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance(); /* Twitter twitter = new TwitterFactory().getInstance(); */ User user = twitter.verifyCredentials(); List<Status> statuses = twitter.getHomeTimeline(); //System.out.println("Showing @" + user.getScreenName() + "'s home timeline."); for (Status status : statuses) { System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText()); } } catch (TwitterException te) { te.printStackTrace(); System.out.println("Failed to get timeline: " + te.getMessage()); System.exit(-1); } }
From source file:edu.american.student.util.TwitterSpider.java
License:Open Source License
/** * Grabs the last 100 tweet objects (Status) from a handle * @param handle/* ww w . j av a 2 s . c o m*/ * @return * @throws SpiderException */ public static List<Status> spider(String handle) throws SpiderException { //connect to Twitter ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(Constants.getTwitterOAuthConsumerKey()) .setOAuthConsumerSecret(Constants.getTwitterOAuthConsumerSecret()) .setOAuthAccessToken(Constants.getTwitterOAuthAccessToken()) .setOAuthAccessTokenSecret(Constants.getTwitterOAuthAccessTokenSecret()); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance(); //get tweets Query query = new Query("from:" + handle.replace("@", "")); query.setCount(100); QueryResult result; try { result = twitter.search(query); List<Status> toReturn = result.getTweets(); return toReturn; } catch (TwitterException e) { String gripe = "Twitter spider failed to...err spider!"; throw new SpiderException(gripe, e); } }