2

I am using Spring Social Twitter to retrieve a friend's name of a user. Here is my code.

@Controller
@RequestMapping("/")
   public class HelloController {

    private Twitter twitter;

    private ConnectionRepository connectionRepository;

    @Inject
    public HelloController(Twitter twitter, ConnectionRepository connectionRepository) {
        this.twitter = twitter;
        this.connectionRepository = connectionRepository;
    }

    @RequestMapping(method=RequestMethod.GET)
    public String helloTwitter(Model model) {
        if (connectionRepository.findPrimaryConnection(Twitter.class) == null) {
            return "redirect:/connect/twitter";
        }

        model.addAttribute(twitter.userOperations().getUserProfile());
        CursoredList<TwitterProfile> friends = twitter.friendOperations().getFriends();
        model.addAttribute("friends", friends);
        for ( TwitterProfile frnd : friends) {
            System.out.println(frnd.getName());
        }
        return "hello";
    }

}

But it retrieves only 20 friends. How could I get all the friends? (Say if I have 1000 friends)

Krishnalal P
  • 487
  • 4
  • 20
  • I have a vague memory that Twitter APIs page this kind of data, hence you must retrieve it iteratively by blocks of `X` elements (in this case, 20 apparently). Doesn't help you much but there should be some documentation about it in the Twitter API pages. – Mena Oct 10 '14 at 12:32
  • I tried to find a documentation for this. Still couldn't find any. – Krishnalal P Oct 10 '14 at 12:36
  • According [to the docs](http://docs.spring.io/spring-social-twitter/docs/1.1.0.RELEASE/apidocs/org/springframework/social/twitter/api/FriendOperations.html#getFriends%28%29), this method should retrieve up to 5000 users, using multiple calls to the Twitter API. – Duncan Jones Oct 10 '14 at 12:39
  • I tried 'friends.size()'. It returns 20. – Krishnalal P Oct 13 '14 at 04:00
  • I also have the same question and found a solution for that case. Check my post.. [link for my post](http://stackoverflow.com/questions/31794431/friends-and-followers-in-spring-social/31949250#31949250) – Neero Aug 11 '15 at 18:26

2 Answers2

3

You have to iterate through all the cursors and collect the results as follows:

    // ...
    CursoredList<TwitterProfile> friends = twitter.friendOperations().getFriends();
    ArrayList<TwitterProfile> allFriends = friends;
    while (friends.hasNext()) {
        friends = twitter.friendOperations().getFriendsInCursor(friends.getNextCursor());
        allFriends.addAll(friends);
    }
    // process allFriends...
janoskk
  • 41
  • 3
1

There must be another error, spring documentation specifically states:

getFriends()

"Retrieves a list of up to 5000 users that the authenticated user follows."

http://docs.spring.io/spring-social-twitter/docs/1.0.5.RELEASE/api/org/springframework/social/twitter/api/FriendOperations.html#getFriendIds%28%29

Are you sure that the user you are doing the query with has more friends? Maybee you could try to user getFriendsIds or getFriends(string name).

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Celeb
  • 88
  • 4