Get user follower count with 1.1 - 410 Gone

前端 未结 3 1622
暖寄归人
暖寄归人 2021-01-14 21:52

I\'m trying to build a \'Follow\' button with a vertical followers count above it. I had a solution working until Twitter retired the 1.0 API today and now require an Oauth

3条回答
  •  忘掉有多难
    2021-01-14 22:17

    Since Twitter stopped providing an API that doesn't require authentication for simple read-only operations like getting the follower count, they deserve to be scrapped.

    We'll use YQL to get the Twitter page of the user, then parse out the follower count. This also works on the client alone, without same-origin restrictions:

    function getFollowerCount(username, callback) {
      $.getJSON("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D'http%3A%2F%2Ftwitter.com%2F" + username + "'%20and%20xpath%3D'%2F%2Finput%5B%40id%3D%22init-data%22%5D'&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys", function (data) {
        callback(data.query.results.input.value.match(/followers_count":(\d+)/)[1]);
      });
    }
    
    getFollowerCount('dandv', function (count) {
      $('#follower-count').text(count)
    });
    
    

    Of course, this is somewhat brittle and depends on Twitter keeping the same markup - a hidden input element after the closing .

提交回复
热议问题