【问题标题】:Friends and Followers in Spring Social春季社交中的朋友和追随者
【发布时间】:2015-08-03 18:37:21
【问题描述】:

我在我的项目中使用 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。

Twitter API description

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

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

【问题讨论】:

    标签: spring twitter spring-integration spring-social-twitter


    【解决方案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();
    

    【讨论】:

      【解决方案2】:

      终于找到解决办法了,

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

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

      这里是sn-p的例子,

      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);
      

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-01-07
          • 2016-05-04
          • 1970-01-01
          • 2014-09-21
          • 2012-05-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多