Generic properties
There are a number of properties available for configuring Twitter4J. You can specify properties via twitter4j.properties file, ConfigurationBuilder class or System Property as follows : -
via twitter4j.properties
via ConfigurationBuilder
via System Properties
via environment variables
Save a standard properties file named "twitter4j.properties". Place it to either the current directory, root of the classpath directory.
debug=true oauth.consumerKey=********************* oauth.consumerSecret=****************************************** oauth.accessToken=************************************************** oauth.accessTokenSecret=******************************************
You can use ConfigurationBuilder class to configure Twitter4J programatically as follows:
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("*********************")
.setOAuthConsumerSecret("******************************************")
.setOAuthAccessToken("**************************************************")
.setOAuthAccessTokenSecret("******************************************");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
You can configure Twitter4J via System properties as well. Note that you need "twitter4j." prefix.
$ java -Dtwitter4j.debug=true
-Dtwitter4j.oauth.consumerKey=*********************
-Dtwitter4j.oauth.consumerSecret=******************************************
-Dtwitter4j.oauth.accessToken=**************************************************
-Dtwitter4j.oauth.accessTokenSecret=******************************************
-cp twitter4j-core-4.0.7.jar:yourApp.jar yourpackage.Main
You can configure Twitter4J via environment variables as well. Note that you need "twitter4j." prefix. This makes it easier to test, stage and deploy apps running on Heroku.
$ export twitter4j.debug=true $ export twitter4j.oauth.consumerKey=********************* $ export twitter4j.oauth.consumerSecret=****************************************** $ export twitter4j.oauth.accessToken=************************************************** $ export twitter4j.oauth.accessTokenSecret=****************************************** $ java -cp twitter4j-core-4.0.7.jar:yourApp.jar yourpackage.MainOn Heroku:
$ heroku config:add oauth.consumerKey=********** $ heroku config:add oauth.consumerSecret=************ $ heroku config:add oauth.accessToken=************************************************** $ heroku config:add oauth.accessTokenSecret=****************************************** $ git push heroku master
Available Configuration Properties
Misc.
| Property name | Description | Default value |
|---|---|---|
| debug | Enables deubg output. Effective only with the embedded logger. | false |
| includeRTs | If set to true, retweets will be included in the timeline methods' resopnse. | true |
| includeEntities | If set to true, entities will be included in the timeline methods' resopnse. | true |
| includeMyRetweet | set to true, any statuses returned that have been retweeted by the authenticating user will include an additional field: Status#getCurrentUserRetweetId () | true |
| jsonStoreEnabled | If set to true, raw JSON forms will be stored in DataObjectFactory. | false |
| mbeanEnabled | If set to true, mbean will be registerd. | false |
| loggerFactory | Logger implimentation Supported implimentations: twitter4j.StdOutLoggerFactory twitter4j.SLF4JLoggerFactory twitter4j.CommonsLoggingLoggerFactory twitter4j.Log4JLoggerFactory twitter4j.JULLoggerFactory twitter4j.NullLoggerFactory | null |
| contributingTo | sets the user id contributing to. | -1L |
OAuth
| Property name | Description | Default value |
|---|---|---|
| oauth.consumerKey | Default OAuth consumer key | null |
| oauth.consumerSecret | Default OAuth consumer secret | null |
| oauth.accessToken | Default OAuth access token | null |
| oauth.accessTokenSecret | Default OAuth access token secret | null |
Basic / xAuth authentication
| Property name | Description | Default value |
|---|---|---|
| user | Default screen name | null |
| password | Default password | null |
Streaming
| Property name | Description | Default value |
|---|---|---|
| stream.user.repliesAll | enables replies=all parameter | false |
| stream.enableStallWarnings | set to true to receive stall warnings | true |
HTTP connection
| Property name | Description | Default value |
|---|---|---|
| http.connectionTimeout | Http connection timeout in milliseconds | 20000 |
| http.readTimeout | Http read timeout in milliseconds | 120000 |
| http.streamingReadTimeout | Streaming API's Http Read timeout in milliseconds | 300000 |
| http.retryCount | Number of HTTP retries | 0 |
| http.retryIntervalSecs | HTTP retry interval in seconds | 5 |
| http.prettyDebug | prettify JSON debug output if set to true. | false |
HTTP proxy server
| Property name | Description | Default value |
|---|---|---|
| http.proxyHost | HTTP proxy server host name | null |
| http.proxyPort | HTTP proxy server port | null |
| http.proxyUser | HTTP proxy server user name | null |
| http.proxyPassword | HTTP proxy server password | null |
Media support
| Property name | Description | Default value |
|---|---|---|
| media.provider | default media provider Supported providers: twitter, imgly, plixi, lockerz, twipple, twitgoo, twitpic, yfrog | |
| media.providerAPIKey | media provider API Key | null |
Base URLs
| Property name | Description | Default value |
|---|---|---|
| restBaseURL | REST API base URL | https://api.twitter.com/1.1/ |
| streamBaseURL | Streaming API base URL | https://stream.twitter.com/1.1/ |
| siteStreamBaseURL | Site Streams API base URL | https://sitestream.twitter.com/1.1/ |
| userStreamBaseURL | User Stream API base URL | https://userstream.twitter.com/1.1/ |
| oauth.requestTokenURL | OAuth request token URL | https://api.twitter.com/oauth/request_token |
| oauth.accessTokenURL | OAuth access token URL | https://api.twitter.com/oauth/access_token |
| oauth.authorizationURL | OAuth authorization URL | https://api.twitter.com/oauth/authorize |
| oauth.authenticationURL | OAuth authentication URL | https://api.twitter.com/oauth/authenticate |
Asynchronous
| Property name | Description | Default value |
|---|---|---|
| async.numThreads | Number of threads handling asynchronous invocations | 1 |
| async.dispatcherImpl | Asynchronous dispatcher implementation class name | twitter4j.DispatcherImpl |
| async.daemonEnabled | Specifies if async threads shouldd be daemons | true |
Logger Configuration
By default, Twitter4J prints log messages to standard output. If any of SLF4J, Commons-Logging, Log4J is in the classpath, log messages will be printed via the available logging library. You can disable logging by specifying -Dtwitter4j.loggerFactory=twitter4j.NullLoggerFactory to the system properties.