hayesdavis / grackle

Lightweight Ruby library for the Twitter API that goes with the flow.
228 stars 36 forks source link

Does this gem work with Apigee? #14

Closed rmanalan closed 13 years ago

rmanalan commented 13 years ago

I've been using this gem for a while now on Heroku. Love it. My app has recently started hitting the rate limit so I thought I'd try Apigee a try since it's free for 50k/hr... more than enough for my app. Anyway, after diving into it a bit, Apigee is basically just a reverse proxy for api.twitter.com and so, when doing oauth, the oauth signature base string needs to be based off of api.twitter.com. Since the OAuth is being handled by ruby-oauth, I thought it would be ok if I just ran the oauth against api.twitter.com, get the tokens I need, add the :apigee key in Grackle::Client::TWITTER_API_HOSTS, then set the :api key when creating a new Grackle::Client, but every time I try, it gives me an invalid signature error which seems like the oauth sig that's being sent out by Grackle is wrong.

@consumer ||= OAuth::Consumer.new(consumer_key, consumer_secret, :site => "http://api.twitter.com")

if !session[:oauth][:request_token].nil? && !session[:oauth][:request_token_secret].nil?
  @request_token = OAuth::RequestToken.new(@consumer, session[:oauth][:request_token], session[:oauth][:request_token_secret])
end

if !session[:oauth][:access_token].nil? && !session[:oauth][:access_token_secret].nil?
  @access_token = OAuth::AccessToken.new(@consumer, session[:oauth][:access_token], session[:oauth][:access_token_secret])
end

if @access_token
  @client = Grackle::Client.new(:auth => {
      :type => :oauth,
      :consumer_key => consumer_key,
      :consumer_secret => consumer_secret,
      :token => @access_token.token, 
      :token_secret => @access_token.secret
    },
    :api => :apigee,
    :headers => {'User-Agent' => "onlythelinks.com/1.0"})

  cached_user = cache.get(@access_token.token)
  if cached_user
    @current_user = cached_user[:current_user]
    @all_lists = cached_user[:lists]
  else
    ...
  end
  @logged_in = true
else
  @logged_in = false
end

Do you know if this gem works with Apigee? I saw that Apigee has forked this project and added some "proxy" code. I tried using that gem, but it has a few problems of its own and doesn't seem to have tests for the additions they added.

Thoughts? TIA!

hayesdavis commented 13 years ago

Actually, yes it will. You just need to use Grackle's proxy support. Once you've got a client instance you can do this: client.transport.proxy = Net::HTTP.Proxy(apigee_url,80)

I tried it out myself and it seems to work just fine. Sorry for the delay getting back to you on this.

rmanalan commented 13 years ago

Thanks. That works perfectly.

tolosa commented 13 years ago

I'm having this exact same issue using this Gem with Apigee, but the proposed solution doesn't work for me, I set the proxy but I'm still receiving the same authentication problems. I use Omniauth to get the token.

What I could be missing? Any help would be very appreciated.

Thank you for this great Gem!

hayesdavis commented 13 years ago

@tolosa I can't think of any reason why this wouldn't work. OAuth has a fair number of moving parts so sometimes the errors can be deceiving. Please make sure that everything is configured as described here: https://github.com/hayesdavis/grackle/wiki/Grackle-and-OAuth.

tolosa commented 13 years ago

I still can't make any authenticated calls to the Twitter API through Apigee. The OAuth authentication itself seems to be working fine, if I call directly to the twitter API it works, but when I switch to the Apigee endoint fails. Unauthenticated calls through Apigee works fine. To have more control over the authentication process, I migrated from OmniAuth to oauth-ruby, but using almost the same code that @manalang posted here doesn't work, and changing the proxy as you suggested doesn't solves the issue for me.

Any ideas about what else can I try?

Thank you!