Skip to content

Instantly share code, notes, and snippets.

@acorcutt
Created April 3, 2011 14:46
Show Gist options
  • Save acorcutt/900459 to your computer and use it in GitHub Desktop.
Save acorcutt/900459 to your computer and use it in GitHub Desktop.
Rails twitter login
#a simple controller to login with twitter
#add gem 'oauth' to gemfile
#setup a route to /twitter => #show and /twitter/callback => #callback
class TwittersController < ApplicationController
def consumer
#return a consumer for twitter, add you key and secret here or dynamically grab from db etc.
OAuth::Consumer.new(
"key",
"secret",
:site => "https://api.twitter.com",
:authorize_path => "/oauth/authenticate" #use /oauth/authorize if you want to force logins
)
end
def show
request_token = consumer.get_request_token(:oauth_callback => callback_twitter_url)
#save in a session we need it in callback
session[:request_token] = request_token.token
session[:request_token_secret] = request_token.secret
redirect_to request_token.authorize_url
end
def callback
request_token = OAuth::RequestToken.new(consumer, session[:request_token], session[:request_token_secret])
access_token = request_token.get_access_token(:oauth_verifier => params[:oauth_verifier])
#do something with access_token, save to db, use for authenticated twitter requests etc.
render :text=> [access_token.params["oauth_token"],access_token.params["oauth_token_secret"],access_token.params["user_id"],access_token.params["screen_name"]].inspect
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment