Fighting to get a working twitter4j instance, but having problems with Oauth. I copied-pasted multiple times, created different apps and keys/tokens and still cant get this working. What could i do wrong here really?
my main:
public class Main {
public static void main(String[] args) {
// write your code here
Tweeter tweet = new Tweeter();
try{
tweet.searchTweets();
} catch (TwitterException e) {
e.printStackTrace();
}
}
}
my twitter class:
public class Tweeter {
public static Twitter GetTwitterInstance(){
String consumerKey = "**************";
String consumerSecret = "*************";
String accessToken = "***************"; // yourt token
String accessTokenSecret = "************"; // your token secre
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(consumerKey)
.setOAuthConsumerSecret(consumerSecret)
.setOAuthAccessToken(accessToken)
.setOAuthAccessTokenSecret(accessTokenSecret);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getSingleton();
return twitter;
}
public static void searchTweets() throws TwitterException{
Twitter twitter = GetTwitterInstance();
Query query = new Query("something");
QueryResult result = twitter.search(query);
List<Status> statuses = result.getTweets();
for(Status tweet: statuses){
System.out.println("User: "+tweet.getUser().getScreenName() + " Tweet: "+tweet.getText());
}
}
}
And the error stacktrace:
400:The request was invalid. An accompanying error message will explain why.
This is the status code will be returned during version 1.0 rate
limiting(https://dev.twitter.com/pages/rate-limiting). In API v1.1, a request
without authentication is considered invalid and you will get this response.
message - Bad Authentication data.
code - 215
Relevant discussions can be found on the Internet at:
http://www.google.co.jp/search?q=506c3b98 or
http://www.google.co.jp/search?q=11ed9ae8
TwitterException{exceptionCode=[506c3b98-11ed9ae8], statusCode=400,
message=Bad Authentication data., code=215, retryAfter=-1,
rateLimitStatus=null, version=4.0.6}
at twitter4j.HttpClientImpl.handleRequest(HttpClientImpl.java:164)
at twitter4j.HttpClientBase.request(HttpClientBase.java:57)
at twitter4j.HttpClientBase.get(HttpClientBase.java:75)
at twitter4j.TwitterImpl.get(TwitterImpl.java:1786)
at twitter4j.TwitterImpl.search(TwitterImpl.java:268)
at Tweeter.searchTweets(Tweeter.java:58)
at Main.main(Main.java:14)
Printing the twitter factory's keys/tokens gives the normal keys/tokens as expected without weird characters or spaces or whatever. Any help will be really really appreciated!
Problem was that tf.getSingleton()
does not return a twitter
instance apparently.
it should be just Twitter twitter = tf.getInstance();
and then it works.
Your code appears to be cut and pasted from known working code. Check again that there are no extra characters, such as a space at the end of a string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.