1

我在我的项目中使用 spring-integration-twitter 4.1.6.RELEASE。使用 TwitterTemplate 我试图为经过身份验证的用户获取所有朋友。

所以我用这个方法,

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

但在这种情况下,我只会得到 20 个朋友作为默认计数。但是我有 33 个朋友,我想把他们都带走。我该怎么做。当我调用此方法时,我也是经过身份验证的用户。在 TwitterTemplate 中,无法将计数作为参数传递。但 API 表示它将返回 5000 个用户。

/** * 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 调用 twitter API 来获取数据。所以请求重定向到 Twitter API 的https://api.twitter.com/1.1/friends/list.json访问 URL。

推特 API 说明

目前,结果按最近的顺序排列——但是,此顺序可能会受到未宣布的更改和最终一致性问题的影响。结果以 20 个用户为一组给出,并且可以在后续请求中使用 next_cursor 值导航多个结果“页面”。有关详细信息,请参阅使用光标导航集合。

我怎样才能做到这一点???

4

3 回答 3

1

这是一个很好的解决方案,但它不会返回超过前 100 个朋友。方法 twitterTemplate.userOperations.getUsers(userIdArray) 一次只能返回 100 个用户。https://dev.twitter.com/rest/reference/get/users/lookup

更好的解决方案是:

List<TwitterProfile> getAllFollowers() {
    CursoredList<Long> cursoredList;
    cursoredList = twitter.friendOperations().getFollowerIds();
    List<TwitterProfile> followers = getFollowers(cursoredList);
    return followers;
}

List<TwitterProfile> getFollowers(CursoredList<Long> cursoredList) {
    List<TwitterProfile> followers = new ArrayList<>();
    for(int i = 0; i< cursoredList.size(); i+=100){
        followers.addAll(getProfiles(cursoredList, i));
    }
    return followers;
}

List<TwitterProfile> getProfiles(CursoredList<Long> cursoredList, int start){
    int end = Math.min(start+100, cursoredList.size());
    long[] ids = cursoredList.subList(start, end).toArray(new long[0]);
    List<TwitterProfile> profiles = twitter.userOperations().getUsers(ids);
    return profiles;
}

然后调用 getAllFollowers()。类似的代码也可用于获取所有好友。只需更改呼叫:

twitter.friendOperations.getFollowerIds() 

twitter.friendOperations.getFriendIds();
于 2015-08-19T00:21:08.360 回答
0

终于找到了解决办法

1) 首先,您必须使用friendOperations 获取经过身份验证的用户的所有好友ID 列表。

2) 然后使用 userOperations 获取特定 id 列表的所有朋友。

这是示例片段,

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);
于 2015-08-11T18:23:03.113 回答
0

要回答如何使用光标导航的原始问题,请考虑以下内容。

您必须遍历所有游标并收集结果,如下所示:

    // ...
    CursoredList<TwitterProfile> friends = twitter.friendOperations().getFriends();
    ArrayList<TwitterProfile> allFriends = friends;
    while (friends.hasNext()) {
        friends = twitter.friendOperations().getFriendsInCursor(friends.getNextCursor());
        allFriends.addAll(friends);
    }
    // process allFriends...
于 2016-05-09T20:40:29.877 回答