Search code examples
twitter4j

Twitter4j with a twitter4j.properties file


I'm trying to post to Twitter using Twitter4j and putting my OAuth credentials in a twitter4j.properties file as per the documentation on http://twitter4j.org.

However I'm stuck as to properly post to Twitter using the credentials this file, as I can only find examples that use ConfigurationBuilder and there's no clear example on which methods to use when the credentials are all stored in a twitter4j.properties file.

I've tried both:

Twitter twitter = TwitterFactory.getSingleton();
twitter.updateStatus("My status");

and

Twitter twitter = new TwitterFactory().getInstance();
twitter.updateStatus("My status");

both of which give me an "Authentication credentials are missing" error.

I've looked at the examples that come with Twitter4j and they involve getting the Access Tokens through a URL callback, which I really don't need as I already have obtained all the credentials from https://apps.twitter.com/

I realise that I could just use the new ConfigurationBuilder() method but this forces me to copy and paste the keys into each of my classes which is time consuming and looks clunky. What's the proper way to set credentials from the twitter4j.properties file?


Solution

  • After some searching, I understand now that the twitter4j.properties file had to be placed in the WEB-INF folder in my case, as I am running a Google App Engine servlet.

    The wording on http://twitter4j.org/en/configuration.html is vague and ambiguous, as it simply states that the file twitter4j.properties should be placed in "either the current directory or root of the classpath directory". In my case I was originally placing it in the same directory as my other source files, which meant that Twitter4J couldn't find it ostensibly because it only searched in the root of the classpath directory, which I am going to assume in my case it's WEB-INF.