Post new tweet problems


#1

Hi

Just started working with twitter4j library, but for some reason i cant get the simplest thing to work…
I suspect it has something to do with outdated code, using the 3.0.6 version of the library.
if so, how should i correct it?

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.OAuthAuthorization;
import twitter4j.conf.ConfigurationBuilder;

public class TwitterAPI
{
private final String ACCESS_TOKEN ="***************";
private final String ACCESS_TOKEN_SECRET = “***************”;
private final String CONSUMER_KEY = “***************”;
private final String CONSUMER_SECRET = “***************”;

public static void main(String[] args) throws TwitterException
{

	
	
	ConfigurationBuilder builder = new ConfigurationBuilder();
	builder.setOAuthAccessToken("***************");
	builder.setOAuthAccessTokenSecret("***************");
	builder.setOAuthConsumerKey("***************");
	builder.setOAuthConsumerSecret("***************");
    OAuthAuthorization auth = new OAuthAuthorization(builder.build());
    Twitter twitter = new TwitterFactory().getInstance(auth);
	try {
		twitter.updateStatus("#HELLO WORLD!");
	} catch (TwitterException e) {
		System.err.println("Error occurred while updating the status!");
		return;
	}
    System.out.println("Successfully updated the status.");


}

}

Exception in thread “main” java.lang.NoClassDefFoundError: com/squareup/okhttp/OkHttpClient
at twitter4j.internal.http.alternative.HttpClientImpl.prepareClient(HttpClientImpl.java:132)
at twitter4j.internal.http.alternative.HttpClientImpl.getConnection(HttpClientImpl.java:72)
at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:87)
at twitter4j.internal.http.alternative.HttpClientImpl.request(HttpClientImpl.java:116)
at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:61)
at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:98)
at twitter4j.TwitterImpl.post(TwitterImpl.java:2041)
at twitter4j.TwitterImpl.updateStatus(TwitterImpl.java:264)
at TwitterAPI.main(TwitterAPI.java:28)
Caused by: java.lang.ClassNotFoundException: com.squareup.okhttp.OkHttpClient
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
… 9 more


#2

Changed the code a little bit, apparently twitter is forcing the use of SSL/HTTPS nowdays, and this line of code solved the issue for the most ppl, but i still get the same errors.
builder.setUseSSL(true);

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.OAuthAuthorization;
import twitter4j.conf.ConfigurationBuilder;

public class TwitterAPI
{

public static void main(String[] args) throws TwitterException
{
	ConfigurationBuilder builder = new ConfigurationBuilder();
	builder.setUseSSL(true);
	builder.setOAuthAccessToken("***************");
	builder.setOAuthAccessTokenSecret("***************");
	builder.setOAuthConsumerKey("***************");
	builder.setOAuthConsumerSecret("***************");
    OAuthAuthorization auth = new OAuthAuthorization(builder.build());
    Twitter twitter = new TwitterFactory().getInstance(auth);
	try 
	{
		 twitter.updateStatus("HELLO WORLD!");
	} catch (TwitterException e) {
		System.err.println("Error occurred while updating the status!");
		return;
	}
    System.out.println("Successfully updated the status.");
}

}