List of usage examples for twitter4j.auth BasicAuthorization BasicAuthorization
public BasicAuthorization(String userId, String password)
From source file:ar.com.zauber.commons.social.twitter.impl.streaming.TweetFetcher.java
License:Apache License
/** * Creates the {@link TwitterStream}//from w w w. j av a 2 s. co m * * @param user * @param password * @return */ private TwitterStream createStream(final String user, final String password) { final TwitterStream stream = new TwitterStreamFactory().getInstance(new BasicAuthorization(user, password)); stream.addListener(new StatusListener() { @Override public void onStatus(final Status status) { try { T t = transformer.transform(status); closure.execute(t); } catch (Throwable ex) { logger.error("Exception en onStatus", ex); } } @Override public void onException(final Exception e) { logger.error("Exception on TwitterStream", e); } @Override public void onTrackLimitationNotice(final int numberOfLimitedStatuses) { logger.warn("onTrackLimitationNotice: Number of limited " + "statuses: {}", numberOfLimitedStatuses); } @Override public void onDeletionNotice(final StatusDeletionNotice statusDeletionNotice) { logger.warn("statusDeletionNotice: {}", statusDeletionNotice); } @Override public void onScrubGeo(final long userId, final long upToStatusId) { logger.warn("scrubGeo: {} {}", userId, upToStatusId); } }); stream.addConnectionLifeCycleListener(new ConnectionLifeCycleListener() { @Override public void onDisconnect() { logger.warn("Disconnected from Twitter!"); } @Override public void onConnect() { } @Override public void onCleanUp() { } }); return stream; }
From source file:com.dwdesign.tweetings.util.Utils.java
License:Open Source License
public static Twitter getTwitterInstance(final Context context, final long account_id, final boolean include_entities, final boolean include_rts, final boolean use_httpclient) { if (context == null) return null; final SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); final boolean enable_gzip_compressing = preferences != null ? preferences.getBoolean(PREFERENCE_KEY_GZIP_COMPRESSING, true) : true;//from www .j a v a2 s . co m final boolean ignore_ssl_error = preferences != null ? preferences.getBoolean(PREFERENCE_KEY_IGNORE_SSL_ERROR, false) : false; final boolean enable_proxy = preferences != null ? preferences.getBoolean(PREFERENCE_KEY_ENABLE_PROXY, false) : false; final String consumer_key = preferences != null ? preferences.getString(PREFERENCE_KEY_CONSUMER_KEY, CONSUMER_KEY) : CONSUMER_KEY; final String consumer_secret = preferences != null ? preferences.getString(PREFERENCE_KEY_CONSUMER_SECRET, CONSUMER_SECRET) : CONSUMER_SECRET; Twitter twitter = null; final StringBuilder where = new StringBuilder(); where.append(Accounts.USER_ID + "=" + account_id); final Cursor cur = context.getContentResolver().query(Accounts.CONTENT_URI, Accounts.COLUMNS, where.toString(), null, null); if (cur != null) { if (cur.getCount() == 1) { cur.moveToFirst(); final ConfigurationBuilder cb = new ConfigurationBuilder(); setUserAgent(context, cb); if (use_httpclient) { cb.setHttpClientImplementation(HttpClientImpl.class); } cb.setGZIPEnabled(enable_gzip_compressing); if (enable_proxy) { final String proxy_host = preferences.getString(PREFERENCE_KEY_PROXY_HOST, null); final int proxy_port = parseInt(preferences.getString(PREFERENCE_KEY_PROXY_PORT, "-1")); if (!isNullOrEmpty(proxy_host) && proxy_port > 0) { cb.setHttpProxyHost(proxy_host); cb.setHttpProxyPort(proxy_port); } } final String rest_base_url = cur.getString(cur.getColumnIndexOrThrow(Accounts.REST_BASE_URL)); final String signing_rest_base_url = cur .getString(cur.getColumnIndexOrThrow(Accounts.SIGNING_REST_BASE_URL)); final String search_base_url = cur.getString(cur.getColumnIndexOrThrow(Accounts.SEARCH_BASE_URL)); final String upload_base_url = cur.getString(cur.getColumnIndexOrThrow(Accounts.UPLOAD_BASE_URL)); final String oauth_base_url = cur.getString(cur.getColumnIndexOrThrow(Accounts.OAUTH_BASE_URL)); final String signing_oauth_base_url = cur .getString(cur.getColumnIndexOrThrow(Accounts.SIGNING_OAUTH_BASE_URL)); //if (!isNullOrEmpty(rest_base_url)) { cb.setRestBaseURL(DEFAULT_REST_BASE_URL); //} //if (!isNullOrEmpty(search_base_url)) { cb.setSearchBaseURL(DEFAULT_SEARCH_BASE_URL); //} cb.setIncludeEntitiesEnabled(include_entities); cb.setIncludeRTsEnabled(include_rts); switch (cur.getInt(cur.getColumnIndexOrThrow(Accounts.AUTH_TYPE))) { case Accounts.AUTH_TYPE_OAUTH: case Accounts.AUTH_TYPE_XAUTH: if (isNullOrEmpty(consumer_key) || isNullOrEmpty(consumer_secret)) { cb.setOAuthConsumerKey(CONSUMER_KEY); cb.setOAuthConsumerSecret(CONSUMER_SECRET); } else { cb.setOAuthConsumerKey(consumer_key); cb.setOAuthConsumerSecret(consumer_secret); } twitter = new TwitterFactory(cb.build()).getInstance( new AccessToken(cur.getString(cur.getColumnIndexOrThrow(Accounts.OAUTH_TOKEN)), cur.getString(cur.getColumnIndexOrThrow(Accounts.TOKEN_SECRET)))); break; case Accounts.AUTH_TYPE_BASIC: twitter = new TwitterFactory(cb.build()).getInstance( new BasicAuthorization(cur.getString(cur.getColumnIndexOrThrow(Accounts.USERNAME)), cur.getString(cur.getColumnIndexOrThrow(Accounts.BASIC_AUTH_PASSWORD)))); break; default: } } cur.close(); } return twitter; }
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 w w. jav a 2s . co m } 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 {// w w w . j a v a 2 s . c om 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//from www .j a v a 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:org.getlantern.firetweet.util.Utils.java
License:Open Source License
public static Authorization getTwitterAuthorization(final Context context, final ParcelableCredentials 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); final String prefConsumerSecret = prefs.getString(KEY_CONSUMER_SECRET, TWITTER_CONSUMER_SECRET); 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)); cb.setOAuthAuthorizationURL(getApiUrl(account.api_url_format, null, null)); 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); }/*from w w w . ja va2s .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); cb.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET); } 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:org.getlantern.firetweet.util.Utils.java
License:Open Source License
public static Authorization getTwitterAuthorization(final Context context, final long accountId) { final String where = Expression.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 {// w w w .j av a2 s .co m 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); // 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); final String prefConsumerSecret = prefs.getString(KEY_CONSUMER_SECRET, TWITTER_CONSUMER_SECRET); 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/")); cb.setOAuthAuthorizationURL(getApiUrl(apiUrlFormat, null, null)); 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); cb.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET); } 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:org.getlantern.firetweet.util.Utils.java
License:Open Source License
@Nullable public static Twitter getTwitterInstance(final Context context, final long accountId, final boolean includeEntities, final boolean includeRetweets) { if (context == null) return null; final FiretweetApplication app = FiretweetApplication.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//from www . j a v a2 s . c o m final ParcelableCredentials credentials = ParcelableCredentials.getCredentials(context, accountId); if (credentials == null) return null; final ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setHostAddressResolverFactory(new FiretweetHostResolverFactory(app)); cb.setHttpClientFactory(new OkHttpClientFactory(context)); cb.setHttpConnectionTimeout(connection_timeout); cb.setGZIPEnabled(enableGzip); cb.setIgnoreSSLError(ignoreSslError); cb.setIncludeCards(true); cb.setCardsPlatform("Android-12"); // cb.setModelVersion(7); 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); final String prefConsumerSecret = prefs.getString(KEY_CONSUMER_SECRET, TWITTER_CONSUMER_SECRET); final String apiUrlFormat = credentials.api_url_format; final String consumerKey = trim(credentials.consumer_key); final String consumerSecret = trim(credentials.consumer_secret); final boolean sameOAuthSigningUrl = credentials.same_oauth_signing_url; final boolean noVersionSuffix = credentials.no_version_suffix; 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)); cb.setOAuthAuthorizationURL(getApiUrl(apiUrlFormat, null, null)); if (!sameOAuthSigningUrl) { cb.setSigningRestBaseURL(DEFAULT_SIGNING_REST_BASE_URL); cb.setSigningOAuthBaseURL(DEFAULT_SIGNING_OAUTH_BASE_URL); cb.setSigningUploadBaseURL(DEFAULT_SIGNING_UPLOAD_BASE_URL); } } if (TwitterContentUtils.isOfficialKey(context, consumerKey, consumerSecret)) { setMockOfficialUserAgent(context, cb); } else { setUserAgent(context, cb); } cb.setIncludeEntitiesEnabled(includeEntities); cb.setIncludeRTsEnabled(includeRetweets); cb.setIncludeReplyCountEnabled(true); cb.setIncludeDescendentReplyCountEnabled(true); switch (credentials.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); cb.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET); } final String token = credentials.oauth_token; final String tokenSecret = credentials.oauth_token_secret; if (isEmpty(token) || isEmpty(tokenSecret)) return null; return new TwitterFactory(cb.build()).getInstance(new AccessToken(token, tokenSecret)); } case Accounts.AUTH_TYPE_BASIC: { final String screenName = credentials.screen_name; final String username = credentials.basic_auth_username; final String loginName = username != null ? username : screenName; final String password = credentials.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; } } }
From source file:org.mariotaku.twidere.util.Utils.java
License:Open Source License
@Nullable public static Twitter getTwitterInstance(final Context context, final long accountId, final boolean includeEntities, final boolean includeRetweets) { if (context == null) return null; final TwidereApplication app = TwidereApplication.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//from ww w. j a v a 2s . c o m final ParcelableCredentials credentials = ParcelableCredentials.getCredentials(context, accountId); if (credentials == null) return null; final ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setHostAddressResolverFactory(new TwidereHostResolverFactory(app)); cb.setHttpClientFactory(new OkHttpClientFactory(context)); cb.setHttpConnectionTimeout(connection_timeout); cb.setGZIPEnabled(enableGzip); cb.setIgnoreSSLError(ignoreSslError); cb.setIncludeCards(true); cb.setCardsPlatform("Android-12"); // cb.setModelVersion(7); 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); final String prefConsumerSecret = prefs.getString(KEY_CONSUMER_SECRET, TWITTER_CONSUMER_SECRET); final String apiUrlFormat = credentials.api_url_format; final String consumerKey = trim(credentials.consumer_key); final String consumerSecret = trim(credentials.consumer_secret); final boolean sameOAuthSigningUrl = credentials.same_oauth_signing_url; final boolean noVersionSuffix = credentials.no_version_suffix; 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)); cb.setOAuthAuthorizationURL(getApiUrl(apiUrlFormat, null, null)); if (!sameOAuthSigningUrl) { cb.setSigningRestBaseURL(DEFAULT_SIGNING_REST_BASE_URL); cb.setSigningOAuthBaseURL(DEFAULT_SIGNING_OAUTH_BASE_URL); cb.setSigningUploadBaseURL(DEFAULT_SIGNING_UPLOAD_BASE_URL); } } setClientUserAgent(context, consumerKey, consumerSecret, cb); cb.setIncludeEntitiesEnabled(includeEntities); cb.setIncludeRTsEnabled(includeRetweets); cb.setIncludeReplyCountEnabled(true); cb.setIncludeDescendentReplyCountEnabled(true); switch (credentials.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); cb.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET); } final String token = credentials.oauth_token; final String tokenSecret = credentials.oauth_token_secret; if (isEmpty(token) || isEmpty(tokenSecret)) return null; return new TwitterFactory(cb.build()).getInstance(new AccessToken(token, tokenSecret)); } case Accounts.AUTH_TYPE_BASIC: { final String screenName = credentials.screen_name; final String username = credentials.basic_auth_username; final String loginName = username != null ? username : screenName; final String password = credentials.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; } } }
From source file:trendulo.ingest.twitter.TwitterStreamingStringSequenceSource.java
License:Apache License
public TwitterStreamingStringSequenceSource(String username, String password, String archiveTwitterFilePath, StatusFilter statusFilter) {//from w w w. j a v a 2 s .co m this.statusFilter = statusFilter; statusQueue = new ArrayBlockingQueue<Status>(DEFAULT_BUFFER_SIZE); // If the user wants status messages archived, create an OutputStream if (archiveTwitterFilePath != null) { try { // Verify that the file doesn't already exist so we don't clobber the archive if (new File(archiveTwitterFilePath).exists()) { throw new RuntimeException( "Archive File Already Exists. Please move/rename before starting Ingest: " + archiveTwitterFilePath); } FileOutputStream fos = new FileOutputStream(archiveTwitterFilePath); if (archiveTwitterFilePath.endsWith("gz")) { archiveOutputStream = new GZIPOutputStream(new BufferedOutputStream(fos)); } else { archiveOutputStream = fos; } } catch (IOException e) { log.error("Error creating archive file", e); } } // Configure the Twitter Streaming API client // this system property is required to access raw json System.setProperty("twitter4j.jsonStoreEnabled", "true"); auth = new BasicAuthorization(username, password); twitterStream = new TwitterStreamFactory().getInstance(auth); twitterStream.addListener(this); // We only want status messages with a geo. This needs to be pulled // out so it is configurable // Right now just the lower 48 of the US FilterQuery filterQuery = new FilterQuery(); double[][] locations = { { -126.0, 24.0 }, { -67.0, 49.0 } }; filterQuery.locations(locations); twitterStream.filter(filterQuery); }