Get user follower count with 1.1 - 410 Gone

15,101

Solution 1

Firstly, according to official sources:

You are discouraged from performing OAuth operations via client-side Javascript. You should perform these requests server-side. If you want the user interaction to be more client-side, the AJAX call should probably be a secure one to your own backend to initiate the tweeting process.

There isn't any easy way to perform client side requests to the new 1.1 API via AJAX. You should use some server-side language to perform this transaction, like .


You're getting 410 (Gone) response code from their API. Let's see what this actually means:

Indicates that the resource requested is no longer available and will not be available again. This should be used when a resource has been intentionally removed and the resource should be purged. Upon receiving a 410 status code, the client should not request the resource again in the future. [Emphasis my own]

Now let's take a look at the latest twitter API news:

enter image description here

As of yesterday, (June 11th 2013), the previously deprecated v1.0 API was retired. This means that the resource will not be available again, and you need to progress onto the v1.1 API.

The v1.1 API requires authenticated requests, usually using OAuth or 'application-specific'.

Not sure why, but you already posted the most helpful answer as a comment, but yes that's a simple library I wrote to help you guys out transitioning to v1.1 of the twitter API.

Your response: GET https://api.twitter.com/1/[Emphasis my own] contains the API version in the URL.

In closing, any twitter url's with /1/ instead of /1.1/ will no longer take on any requests whatsoever, and you will always get a 410 (Gone) response. Time to move to 1.1!

Solution 2

i follow this step.and to get twitter follower count

use this code

$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=Android';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$follow_count=$twitter->setGetfield($getfield)
             ->buildOauth($url, $requestMethod)
             ->performRequest();
            $testCount = json_decode($follow_count, true);
echo "<pre>";
echo $testCount[0]['user']['followers_count'];
echo "</pre>";

you can change the "screen_name" and get the follower count.

cheers.

Solution 3

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)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="follower-count">

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

Share:
15,101
Ryan Grush
Author by

Ryan Grush

http://www.twitter.com/ryangrush

Updated on September 05, 2022

Comments

  • Ryan Grush
    Ryan Grush over 1 year

    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 solution. My question, what is the best, easiest solution for this (preferably JS).

    Here is the old solution

    $.getJSON("https://api.twitter.com/1/users/show.json?callback=?&screen_name=twitter",
    
    function(data) { 
        $('.here').text(data);
    });
    

    And the error I'm getting now

    GET https://api.twitter.com/1/users/show.json?callback=jQuery20205200183007400483_1371012819892&screen_name=twitter&_=1371012819893 410 (Gone) 
    
  • Ryan Grush
    Ryan Grush almost 11 years
    Thanks for the detailed answer and the php helper you wrote. I looked around a little bit and this was the simplest solution to just get it up and running.
  • Jimbo
    Jimbo almost 11 years
    @RyanGrush No worries. It's pointless making a complicated library with a load of shortcut methods for the API. Simple single file include is how it should be :)
  • Ryan Grush
    Ryan Grush almost 11 years
    yeah I couldn't agree more, I wish more people approached it that way. Thanks again!
  • Augustin Riedinger
    Augustin Riedinger over 10 years
    This is ridiculously complicated for a single follower count. At least Facebook gives public access to this. Why then wouldn't I fetch the html page to get the number of followers instead ? It works without authentication at least !
  • Jimbo
    Jimbo over 10 years
    @AugustinRiedinger Because if you're scraping a webpage, not only are you going against their ToS (and if they detect too much traffic from you, ban you), and not only is it also bad practice - if they change their markup at any time, your script will stop working. It's also terribly inefficient. Do it properly.
  • Augustin Riedinger
    Augustin Riedinger over 10 years
    Yes but shouldn't it be a best practice for a web service to make data that IS already publicly accessible in html accessible through API? I insist, Facebook seems to have understood that (graph.facebook.com/?ids=facebook).
  • Jimbo
    Jimbo over 10 years
    If you're merely stating that Facebook has a better authentication / api data retrieval system than Twitter's, that's fine. However, you still need to do it the way the service intends. This is nothing to do with the code above.