Spring 社交中的好友和关注者
Friends and Followers in Spring Social
我在我的项目中使用 spring-integration-twitter 4.1.6.RELEASE。使用 TwitterTemplate 我试图为经过身份验证的用户获取所有朋友。
所以我正在使用这个方法,
friendsList = twitterTemplate.friendOperations().getFriends();
但在这种情况下,默认情况下我只能获得 20 个朋友。但是我有 33 个朋友,我想把他们都带回来。我该怎么做 this.Also 当我调用此方法时,我是经过身份验证的用户。在 TwitterTemplate 中,无法将计数作为参数传递。但是 API 说它将 return 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 调用推特 API 来获取数据。因此请求重定向到 Twitter API 的 https://api.twitter.com/1.1/friends/list.json
访问 URL.
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.
我怎样才能做到这一点???
终于找到解决办法了,
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);
这是一个很好的解决方案,但它不会 return 超过前 100 个朋友。 twitterTemplate.userOperations.getUsers(userIdArray) 方法一次只能用于 return 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();
要回答如何使用游标导航的原始问题,请考虑以下内容。
您必须遍历所有游标并按如下方式收集结果:
// ...
CursoredList<TwitterProfile> friends = twitter.friendOperations().getFriends();
ArrayList<TwitterProfile> allFriends = friends;
while (friends.hasNext()) {
friends = twitter.friendOperations().getFriendsInCursor(friends.getNextCursor());
allFriends.addAll(friends);
}
// process allFriends...
我在我的项目中使用 spring-integration-twitter 4.1.6.RELEASE。使用 TwitterTemplate 我试图为经过身份验证的用户获取所有朋友。
所以我正在使用这个方法,
friendsList = twitterTemplate.friendOperations().getFriends();
但在这种情况下,默认情况下我只能获得 20 个朋友。但是我有 33 个朋友,我想把他们都带回来。我该怎么做 this.Also 当我调用此方法时,我是经过身份验证的用户。在 TwitterTemplate 中,无法将计数作为参数传递。但是 API 说它将 return 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 调用推特 API 来获取数据。因此请求重定向到 Twitter API 的 https://api.twitter.com/1.1/friends/list.json
访问 URL.
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.
我怎样才能做到这一点???
终于找到解决办法了,
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);
这是一个很好的解决方案,但它不会 return 超过前 100 个朋友。 twitterTemplate.userOperations.getUsers(userIdArray) 方法一次只能用于 return 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();
要回答如何使用游标导航的原始问题,请考虑以下内容。
您必须遍历所有游标并按如下方式收集结果:
// ...
CursoredList<TwitterProfile> friends = twitter.friendOperations().getFriends();
ArrayList<TwitterProfile> allFriends = friends;
while (friends.hasNext()) {
friends = twitter.friendOperations().getFriendsInCursor(friends.getNextCursor());
allFriends.addAll(friends);
}
// process allFriends...