简体   繁体   English

Twitter API - 如果超出限额

[英]Twitter API - If Rate limit exceeded

I am developing a Twitter web app for myself. 我正在为自己开发一个Twitter网络应用程序。 I am retrieving the latest trending topics. 我正在检索最新的热门话题。

Here's how I'm doing it: 我是这样做的:

    $.ajax({
                      url: 'http://api.twitter.com/1/trends/1.json',
                      dataType: 'jsonp',
                       success: function(data){


                       $.each(data[0].trends, function(i){
                          $('div#trending').hide().append("<p><a href='"+data[0].trends[i].url+"'>"+data[0].trends[i].name+"</a></p>").fadeIn(1000);
                        //Cache into LocalStorage
                    localStorage["trending"+i] = data[0].trends[i].name; //Name
                    localStorage["trendurl"+i] = data[0].trends[i].url;//URL

                                });
                            }
});

But sometimes while I am developing it, the rate limit is exceeded. 但有时我在开发它时,会超出速率限制。

How can I detect if the rate limit has been exceeded? 如何检测是否超过了速率限制?

I cannot seem to detect if this error is being shown: 我似乎无法检测是否显示此错误:

{"error":"Rate limit exceeded. Clients may not make more than 150 requests per hour.","request":"\/1\/trends\/1.json"}

I have tried it by: 我试过了:

success: function(data){
  if(data[0].error != 'undefined'){
    //load localstorage cache
  }
}

But that doesn't seem to work. 但这似乎不起作用。 Please help. 请帮忙。

Thanks :) 谢谢 :)

The Twitter API sends a HTTP 400 status code when you are rate limited, so check for that: 当您受到费率限制时,Twitter API会发送HTTP 400状态代码,因此请检查:

$.ajax({
    // ...
    statusCode: {
        400: function() {
            alert( 'rate limited.' );
        }
    }
});

Also note that your comparison is a bit wrong. 另请注意,您的比较有点不对劲。 data[0].error != 'undefined' will always yield true when the error text is not 'undefined' . 当错误文本不是 'undefined'时, data[0].error != 'undefined'将始终产生true。 So even when you are rate limited, the error text won't be 'undefined' and as such succeed. 因此,即使您在速率受限时,错误文本也不会'undefined' ,因此成功。 What you probably want to check is this: 您可能要检查的是:

if ( !data[0].error ) { // data[0].error is not null
    // ...
}

try something like $.ajax({..}).fail(function(){}); 尝试类似$ .ajax({..})。fail(function(){});

ie

$.ajax({..})
.done(function() { alert("success"); })
.fail(function() { alert("error"); })
.always(function() { alert("complete"); });

and let me know how this works now. 让我知道这是如何工作的。

cheers, /Marcin 欢呼,/ Marcin

If you're not making an OAuth call, you'll be rate limited to 150 calls per hour. 如果您没有进行OAuth通话,则每小时限制为150次通话。 But, there's a small work-around which has worked for me. 但是,有一个小的解决方案对我有用。

According to the Twitter page on Rate Limiting (http://dev.twitter.com/docs/rate-limiting), "Rate limits are applied to methods that request information with the HTTP GET command. Generally API methods that use HTTP POST to submit data to Twitter are not rate limited, however some methods are being rate limited now." 根据速率限制的Twitter页面(http://dev.twitter.com/docs/rate-limiting),“速率限制适用于使用HTTP GET命令请求信息的方法。通常使用HTTP POST的API方法向Twitter提交数据的速度并不受限制,但现在有些方法的速度有限。“

Since the default type of an AJAX call is 'GET', try explicitly changing your type to 'POST' like this: 由于AJAX调用的默认类型是'GET',请尝试将类型显式更改为'POST',如下所示:

$.ajax({
     url: 'http://api.twitter.com/1/trends/1.json',
     type: 'POST',
     dataType: 'jsonp',
     success: function(data){
              $.each(data[0].trends, function(i){
                   $('div#trending').hide().append("<p><a href='"+data[0].trends[i].url+"'>"+data[0].trends[i].name+"</a></p>").fadeIn(1000);
                    //Cache into LocalStorage
                   localStorage["trending"+i] = data[0].trends[i].name; //Name
                   localStorage["trendurl"+i] = data[0].trends[i].url;//URL

               });
      }

}); });

Hope this helps! 希望这可以帮助!

James 詹姆士

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM