Search Twitter Users using Twitter4j

25 / Sep / 2012 by Vishal Sahu 2 comments


In the previous blog we saw how to fetch tweets based on some search criteria using the Twitter4j, a java wrapper library available for Twitter API calls.

In the same grails project, i had the requirement to search users based of their name or words. Twitter4j library provides a cool way to search users over twitter with its searchUsers() method. It helped me a lot to make quick search of users on twitter.

Twitter User search API needs to have twitter account access_token and access_secret, which we obtains after authorizing/connecting twitter account with the application as mentioned in this blog.

Code to search Users:-


String queryString=’Vishal’ // some name which you want to search on twitter
TwitterFactory factory = new TwitterFactory()
Twitter twitter = factory.getInstance()
twitter.setOAuthConsumer(consumerKey, consumerSecret)
AccessToken accessToken = new AccessToken(twitterToken, twitterSecret)

List users = twitter.searchUsers(queryString, 1)
users.each { user->
println("Name: ${user.screenName}")
println("Location: ${user.location}")

This code will fetch the latest 20 users which fits the searched criteria.

Hope this helps.


comments (2)

  1. Deepak Mittal

    Hi Burt,

    I regret our failure in maintaining the quality of our posts, which has made us earn a lot of loyal audience. We were trying to promote the habit of blogging amongst our peers through an internal initiative, but didn’t realize that it would bring down the quality to such an extent.

    It has been an eye opener for us. We are really sorry for letting you down by posting sub-standard and repetitive posts.

    Your comment has been very helpful and made us realize the gravity of the situation. Sorry once again and we thank you for the continued support of our blog.

    Hoping to see you again.


Leave a Reply

Your email address will not be published. Required fields are marked *