views:

41

answers:

1

xI've been working for days to get Twitter to authenticate with Ruby, but I'm not having any luck.

My first attempt was something like this:

class TwitterController < ApplicationController

  def index
    @callback_url = "http://dev.twipler.com:3000/twitter/auth"
    @auth= TwitterOAuth::Client.new( :consumer_key => "xxx", :consumer_secret => "xxxxxxxxxxxxxxxxx" )
    @rtoken = @auth.request_token :oauth_callback => @callback_url
    @token = @rtoken.token
    @secret = @rtoken.secret
    @link = @rtoken.authorize_url
    session['token' ] = @token
    session['secret'] = @secret
    redirect_to @link
  end

  def auth
     @auth.authorize_from_request(session[:rtoken], session[:rsecret], params[:oauth_verifier])
  end
end

And a very similar way but with the Twitter gem, and the same with the OAuth gem directly. No matter what OAuth::Consumer::token_request dies with a 401 error.

So, out of desperation I attempted to git clone Snitter, add my Twitter creds, and try it, but it too dies with a 401.

I've tried using localhost:300/twitter/auth, http://dev.twipler.com:3000/twitter/auth, and a bit.ly for each of the former 2. Nothing works.

Any help?

EDIT: Of course I would forget to do the most logical thing to do and delete my secrets. (They've been changed ;)).

+1  A: 

You may want to edit your consumer secret out. With that, anyone can make requests on behalf of your app.

That said, make sure your system time is synced to an ntp server. If your system time has drifted fast or slow, OAuth requests will fail, since their include a timestamp and relatively short TTL. I had this exact problem a while back.

Failing that, you can crack open the oauth gem and turn on HTTP debugging, which will show you the full HTTP transaction including any error message returned.

Chris Heald
I checked my time when I found out that could be an issue, but it's fine. But I can dig through the HTTP debugging, thanks!
Tyler Smith
Look at Net::HTTP#set_debug_output. Find where in the OAuth gem the HTTP setup is done, and then on the HTTP object, set_debug_output($stderr). The full HTTP transaction will show up in your webserver error log (in my case, /var/log/http/error_log, for Apache).
Chris Heald
Awesome, thanks man!
Tyler Smith