Search code examples
rubytwitterherokusinatratwitter-gem

Twitter gem, Heroku, Internal server error


I haven't worked with the Twitter API before.

I have a Sinatra application pushed to Heroku, which makes use of the Twitter gem. The only thing I do in the application (as far as Twitter is concerned) is to pull my 5 latest tweets.

  @tweets = Twitter.user_timeline("username").first(5)

However, last night I had trouble getting it to work, Heroku threw an internal server error and the logs tell me:

2011-12-03T11:30:08+00:00 app[web.1]: Twitter::Error::BadRequest - : Rate limit exceeded. Clients may not make more than 150 requests per hour.:

and a stack trace. Restarting the app made it work again.

Today I woke up, checked my heroku app and I got the internal server error again..

What can I do to make this work? Is there a way to make it cache the tweets in case it throws a BadRequest error, or is there a way to get unlimited (or nearly unlimited) requests to Twitter?

I'm not authenticating or anything. Bit confused.


Solution

  • It seems that you've met Twitter's Rate limit for unauth users/accounts:

    The REST API does account- and IP-based rate limiting. Unauthenticated calls are permitted 150 requests per hour. Unauthenticated calls are measured against the public facing IP of the server or device making the request.

    Try to use OAuth authentication:

    OAuth calls are permitted 350 requests per hour and are measured against the oauth_token used in the request.