Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

follow method triggering rate limit #534

Closed
kazpsp opened this issue Feb 20, 2014 · 2 comments
Closed

follow method triggering rate limit #534

kazpsp opened this issue Feb 20, 2014 · 2 comments

Comments

@kazpsp
Copy link

kazpsp commented Feb 20, 2014

first here is my code:

class TwitterController < ApplicationController
  before_filter :twitterConnect

  def follow
    begin
      @response = @client.follow(params[:newFollower_id].to_i)
      respond_to do |format|
        format.js {  redirect_to :back}
        format.html {  redirect_to :back, :notice => "followed" }
      end  
    rescue Twitter::Error::TooManyRequests => error
      p 'tw_follower_ids sleep ' + error.rate_limit.reset_in.to_s
      flash[:error] = "Your rate limit has exceeded try again in "+error.rate_limit.reset_in.to_s+" seconds"
    end
  end

  private
  def twitterConnect
    if current_user.twitterToken && current_user.twitterSecret
      @client ||= Twitter::REST::Client.new do |config|
        config.consumer_key        = ENV['twitterKey'] 
        config.consumer_secret     = ENV['twitterSecret'] 
        config.access_token        = current_user.twitterToken
        config.access_token_secret = current_user.twitterSecret
      end
    else
      redirect_to root_url
    end
  end

when I follow several people i get rate limited, when i see where i get rate limited i can see its on: "/friends/ids" and on "/account/verify_credentials", im not sure if this is normal or is this some sort of bug.

thanks

@sferik
Copy link
Owner

sferik commented Feb 20, 2014

You can read more about rate limiting and rate limits on Twitter’s developer site.

It’s odd that you’re being limited on the friends/ids and account/verify_credentials resources, since you don’t appear to be calling any methods that would access those resources in the code you’ve shared. I’m guessing there’s some other code you’ve left out that accesses the Twitter API…perhaps a before_filter in your ApplicationController or something in your User model. What does you implementation of User#twitterToken and User#twitterSecret look like?

By the way, this issue tracker is supposed to be used only for bug reports and feature requests. If you need additional support, please use the mailing list for that.

@sferik sferik closed this as completed Feb 20, 2014
@mikegrassotti
Copy link

This is because calling connection.follow is unbelievably expensive. It fetches a list of all existing friend_ids, paginated at 5K ids per API request. The /friends/ids API call has limit of 15 every 15-minutes so it's easy to hit the limit. See https://github.com/sferik/twitter/blob/master/lib/twitter/rest/friends_and_followers.rb#L108-L113

To get around this use connection.follow! which does what you'd expect, simply a POST to the /friendships/create API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants