2
votes

I am trying to perform a "near"search query to fetch tweets nears to certain location using

twitter4j but getting the exception.

public static void main(String[] args) {
    Twitter twitter = new TwitterFactory().getInstance();
    try {
        String searchTerm = "jobs near:\"san francisco\"";
        QueryResult result = twitter.search(new Query(searchTerm));
        List<Tweet> tweets = result.getTweets();
        for (Tweet tweet : tweets) {
            System.out.println("@" + tweet.getFromUser() + " - " + tweet.getText());
        }
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to search tweets: " + te.getMessage());
    }
}

Exception that I am getting

403:The request is understood, but it has been refused. An accompanying error message will explain why. This code is used when requests are being denied due to update limits (https://support.twitter.com/articles/15364-about-twitter-limits-update-api-dm-and-following). {"error":"Invalid parameter"} Relevant discussions can be on the Internet at: http://www.google.co.jp/search?q=d35baff5 or http://www.google.co.jp/search?q=08c3b248 TwitterException{exceptionCode=[d35baff5-08c3b248], statusCode=403, retryAfter=-1, rateLimitStatus=null, featureSpecificRateLimitStatus=null, version=2.2.5}Failed to search tweets: 403:The request is understood, but it has been refused. An accompanying error message will explain why. This code is used when requests are being denied due to update limits (https://support.twitter.com/articles/15364-about-twitter-limits-update-api-dm-and-following). {"error":"Invalid parameter"}

at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:185)
at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65)
at twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:85)
at twitter4j.TwitterImpl.get(TwitterImpl.java:1895)
at twitter4j.TwitterImpl.search(TwitterImpl.java:98)
at com.xebia.lightning.scheduler.JobTweetsArchiver.main(JobTweetsArchiver.java:115)
1

1 Answers

3
votes

See here: "the near operator can't be used for search queries. Use the geocode parameter instead".

So, pass the geocode parameter to the search:

Returns tweets by users located within a given radius of the given latitude/longitude. The location is preferentially taking from the Geotagging API, but will fall back to their Twitter profile. The parameter value is specified by "latitude,longitude,radius", where radius units must be specified as either "mi" (miles) or "km" (kilometers). Note that you cannot use the near operator via the API to geocode arbitrary locations; however you can use this geocode parameter to search near geocodes directly.