Applying custom configurations to twitter4j

I first made use of the ConfigurationBuilder, which looked quite obvious to me to apply new configuration values.

ConfigurationBuilder cb = new ConfigurationBuilder();
Twitter twitter = new TwitterFactory(;

These values are used by the twitter instance as expected, but when creating the RequestToken still the default twitter URL was created, as the configuration of the ConfigurationBuilder wasn’t propagated.

RequestToken requestToken = twitter.getOAuthRequestToken(CALLBACKURL);
String authorizationUrl = requestToken.getAuthorizationURL();

After searching a while I found out that the default TwitterFactory gets all its configuration data from the system properties. Therefore I just set these values in the source code:


A listing of the twitter4j properties can be found here Available Configuration Properties

Now the authorizationUrl can be created the “normal”, default way:

twitter = new TwitterFactory().getInstance();
RequestToken requestToken = twitter.getOAuthRequestToken(CALLBACKURL);
String authorizationUrl = requestToken.getAuthorizationURL();

Hint: Don’t ever mix up the methods getAuthorizationURL and getAuthenticationURL, that cost me hours of searching! :)

For fast switching between the network configurations I added a network selection when logging in. I think this is a convenient way especially for testing purposes as the twitter network offers the best testing data and I can quickly make sure that it works on the networks as well.

Now I’m just struggling a bit with the custom network, and are working fine. As soon as I will have fixed this and pimped the UI a bit :) I eventually publish a first screen cast.

, , , , ,
  • Delicious
  • Facebook
  • Digg
  • Reddit
  • StumbleUpon
  • Twitter


  • javielinux says:


    I am several days trying to connect to from Twitter4J but not working correctly

    I have done what you say. I connect to the URL that gives me getAuthorizationURL() and sends me to the login page “” but after doing the login, the page sends me to my web without “AccessToken” and I can not get “AUTH_KEY “and” AUTH_SECRET_KEY” of user

    I can upload the code to GitHub if necessary


    Sorry, my English is very bad :-(

  • Markus says:

    Hi javielinux,

    did you register the app on You can register it as a OAuth-Client-App here:
    The only important thing is to register it as a browser-app, for the callback URL you can specify any valid URL. This is overwritten by the twitter4j config, I’m using “T4JOAuth://main” as the callbackurl.
    Additionaly you can have a look at my code on github, the ConnectionManager which you can find here:
    It is used by the HomeActivity.
    Additionally you should have a look at the AndroidManifest.xml to check the permission and intent-filter definitions of your activities .

    Hope this helped,

  • javielinux says:

    Hi Markus

    Thank you very much for your code is really helping me

    I’m making some changes to store users in a database and I want to access to twitter or with the information stored

    I have two problems:

    1. If I access to twitter after I have problems access to and If I access to after I have problems access to twitter

    2. With I can not do login with the information in the database, from Twitter yes

    I would like to upload the source code to GitHub to see if you could help

    Thank you very much for everything

  • Markus says:

    Hi javielinux,

    I’m facing the same problem. Seams like twitter4j reads the system properties only once (or setting them twice has no effect?). After a restart of the emulator/device it worked for me (when selected first).
    As at the moment I’m just using it for testing purpose, this doesn’t matter but I’m thinking of using another approach (as I already spent hours on getting twitter4j working with the custom networks): This is an easy to understand, lower level access to the network you want (without system properties).
    For sure it offers really basic functionality (getting timelines, sending tweets), but it works. It worked for me connecting to too.

  • javielinux says:

    Hi Markus

    I need to work with Twitter4J. I have an application called “TweetTopics” and I want to support I do not plan on changing library, would have to make many changes in my application :-(

    I have a simple test application and I made some changes in your code for working with database. I would like to publish the code with your permission and talk to the creators of Twitter4J if I can help with this issue

    Can I publish the code?

    I will put your name on all the headers and keep you informed if it finally works

    Thank you very much

  • Markus says:

    Hi javielinux,

    no problem, you can use and publish the code.

    I’ll be glad to see if you get it to work ;)

  • javielinux says:

    Here is my test code

    If you get it to work I’ll tell you

  • afrodite says:


    I get an error:
    Exception in thread “main” java.lang.IllegalStateException: Access token already available.
    when executing line requestToken = twitter.getOAuthRequestToken(…)…

    Any ideas why?

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>