springtwitterspring-integrationspring-social-twitter

Friends and Followers in Spring Social


I'm using spring-integration-twitter 4.1.6.RELEASE in my project. Using TwitterTemplate I was trying to get all the friends for the authenticated user.

So im using this method for it ,

friendsList = twitterTemplate.friendOperations().getFriends();

But in this case I'm only getting 20 friends as a default count. But I have 33 friends and I want to fetch them all. How can I do this.Also I'm a authenticated user when I call this method. In the TwitterTemplate there is no way to pass the count as a parameter. But the API says it will return 5000 users.

/** * Retrieves a list of up to 5000 users that the authenticated user follows. * Note that this method make multiple calls to Twitter's REST API (one call to get a list of the friend IDs and one call for every 100 friends). * If all you need is the friend IDs, consider calling getFriendIds() instead. * Or if you need only a subset of the user's friends, call UserOperations.getUsers() passing in the list of friend IDs you need. * @return a list of TwitterProfiles * @throws ApiException if there is an error while communicating with Twitter. * @throws MissingAuthorizationException if TwitterTemplate was not created with OAuth credentials. */ CursoredList<TwitterProfile> getFriends();

TwitterTemplate calls the twitter API to fetch data. So request redirects to Twitter API's https://api.twitter.com/1.1/friends/list.json access URL.

Twitter API description

At this time, results are ordered with the most recent following first — however, this ordering is subject to unannounced change and eventual consistency issues. Results are given in groups of 20 users and multiple “pages” of results can be navigated through using the next_cursor value in subsequent requests. See Using cursors to navigate collections for more information.

How can I achieve this???


Solution

  • Finally found the solution,

    1) First you have to get authenticated user's all friend Ids list using friendOperations.

    2) Then get the all the friends for the particular id list using userOperations.

    Here is the example snippet,

    List<org.springframework.social.twitter.api.TwitterProfile> friendsList;
    CursoredList<Long> friendIdList;
    long[] userIdArray;
    
    friendIdList =  twitterTemplate.friendOperations().getFriendIds();
    userIdArray = new long[friendIdList.size()];
    for(int i=0; i<friendIdList.size(); i++)
        userIdArray[i] = friendIdList.get(i);
    friendsList = twitterTemplate.userOperations().getUsers(userIdArray);