How to verify Twitter details from Ruby/Rails

Further to my post on How to update Twitter from Ruby/Rails, here is a method to verify account details with an AJAX link and results feedback. I got the idea from twitterfeed.com and pretty much reverse-engineered their code - mine behaves in exactly the same way.

Firstly, in a view you need two fields - one for the username, one for the password:

Username: <input type="text" id="twitter_username" /><br />
Password: <input type="password" id="twitter_password" />

(Of course, you’d probably link these to your database model.)

Next you need a link to verify the authentication details. It uses the incredible Prototype framework which comes bundled with Rails to make an AJAX call to a method in your controller. I have called the method twittertest. Because i don’t want to make my users save their details before making the call, i am also passing the two fields as parameters:

<%= link_to_remote("Test Twitter authentication",
  :url => { :action => 'twittertest' },
  :update => 'twitter_results',
  :before => %(Element.hide('twitter_results'); Element.show('checking')),
  :success => %(Element.hide('checking'); Element.show('twitter_results')),
  :with => %('twitter_username=' + document.forms[0].twitter_username.value +
    ‘&twitter_password=’ + document.forms[0].twitter_password.value)
) %>

You’ll also need some placeholder spans (’checking’ and ‘twitter_results’) that will show and hide as appropriate. The ‘twitter_results’ will be populated with whatever the controller returns, be that success or failure in terms of authentication with Twitter.

<span style="display: none;" id="checking">Testing ...</span>
<span style="display: none;" id="twitter_results"></span>

Finally, a method in the controller:

def twittertest

  require 'net/http'
  require 'uri'

  twitter_username = params[:twitter_username]
  twitter_password = params[:twitter_password]
  result = 0

  begin

    url = URI.parse(’http://twitter.com/account/verify_credentials.xml’)
    req = Net::HTTP::Post.new(url.path)
    req.basic_auth twitter_username, twitter_password

    begin
      res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }

      case res
      when Net::HTTPSuccess, Net::HTTPRedirection
        # OK
        if res.body.empty?
          # Twitter had an error
          result = -1
        else
          # Sucess!
          result = 1
        end

      else
        # Twitter had an error
        res.error!
        result = -1
      end

    rescue
      # Wrong password
      result = 0
    end

  rescue SocketError
    # Twitter is currently unavailable
    result = -1
  end

  if result == 1
    render :text => “Twitter authentication succeeded”

  elsif result == 0
    render :text => “Twitter authentication failed”

  else
    render :text => “Something went wrong with Twitter”
  end

end

Please note, i am not saying this is the only way to do it - nor even the best way to do it. It’s just a way that i’ve found seems to work. Again, i thank twitterfeed.com for giving me the idea.

2 Responses to “How to verify Twitter details from Ruby/Rails”

  1. Gravatar (globally recognised avatar) associates an avatar with your email addressAnonymous Says:

    have you come across rememberthemilk which seems to be gaining some traction in the states it lookes like atotal rip of you I found it whilst looking at this http://futureofwebapps.com/2008/miami/, i made a bigger post that you might be interested in at http://x-mass.livejournal.com/244850.html

    hope your well

    hugs
    kate

  2. Gravatar (globally recognised avatar) associates an avatar with your email addressAimee Says:

    Hi Kate,

    I have looked at rememberthemilk - i signed up and had a play. You’re right, it is similar to mychores but i don’t think they copied me, and i certainly didn’t copy them. I think we both just thought of the same thing at around about the same time.

    They seem to have a lot more money to throw at it which is why they’re able to do things like text messages and IM integration and get a lot more publicity. I don’t mind - they’re not competition, simply an alternative. There’s another good one that’s like a game where you play against each other to score points when you do chores. Quite fun and again, simply an alternative.

    I am well, thank you, hope you are too! :D

Leave a Reply