So this is my problem right now:
I went to http://twitter4j.org/en/code-examples.html as I just started to use twitter4j (version 4.0.2 as far as I can remember) and I wanted to try to code examples to get a basic understanding of T4J. However, it seems like I can't even get the first example to work. This is my code right now:
import twitter4j.Status;
import twitter4j.TwitterFactory;
public class Twitter {
public static void main(String[] args) {
String latestStatus="Test";
Twitter twitter = (Twitter) TwitterFactory.getSingleton();
Status status = twitter.updateStatus(latestStatus);
System.out.println("Successfully updated the status to [" + status.getText() + "].");
}
}
This is exactly to code I found on code examples (Plus the string which should end up being the status). However, I always get an error at Status status=twitter.updateStatus(latestStatus), which is basically this:
The method updateStatus(String) is undefined for the type Twitter
I have literally no idea what could be wrong here. I mean in the end it's the official code example. Even the official doc is using strings as parameter for that method.
Can anyone give me a helping hand?
Thanks in advance.
You need couple of things to make this code work - First you need to register your app with Twitter and generate OAuth Authentication. You will get following - consumerkey, consumersecret, accesstoken and accesstokensecret.
Using the keys and secret you would create a Twitter instance, which you can use to access your Twitter User ID.
Second you need to authorize your app to be able to write updates default permission is Read Only.
You can register and manage your app at https://apps.twitter.com/
Also about the twitter$j library version, please use a latest one.
Here is a code snippet to use the authentication keys and update the status -
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("consumerKey")
.setOAuthConsumerSecret("consumerSecret")
.setOAuthAccessToken("accessToken")
.setOAuthAccessTokenSecret("accessTokenSecret");
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
Status status = twitter.updateStatus("test");
Hope this is helpful.