Get user follower count with 1.1 - 410 Gone

前端 未结 3 1584
暖寄归人
暖寄归人 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:09

    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.

    0 讨论(0)
  • 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)
    });
    <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>.

    0 讨论(0)
  • 2021-01-14 22:26

    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 php.


    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!

    0 讨论(0)
提交回复
热议问题