Skip to content

Instantly share code, notes, and snippets.

@flakd
Forked from hayesdavis/oauth_dance.rb
Created July 3, 2013 00:46
Show Gist options
  • Select an option

  • Save flakd/5914587 to your computer and use it in GitHub Desktop.

Select an option

Save flakd/5914587 to your computer and use it in GitHub Desktop.
##
# Below is a template for implementing the "OAuth Dance" with Twitter using the Ruby OAuth gem in a Rails app.
# Ruby OAuth gem is required by grackle and is found here: http://github.com/oauth/oauth-ruby
##
# Step 1: User clicks "Sign in with Twitter" button
# Step 2: User is routed to your controller action that looks like the method below
def start_oauth
@consumer = OAuth::Consumer.new("YOUR_CONSUMER_KEY","YOUR_CONSUMER_SECRET",{:site=>"http://twitter.com" })
@req_token = @consumer.get_request_token(:oauth_callback=>"http://YOUR_CALLBACK_URL")
session[:request_token] = @req_token.token
session[:request_token_secret] = @req_token.secret
redirect_to @req_token.authorize_url
end
# Step 3: User is taken to Twitter to authorize your application
# Step 4: User is redirected from Twitter to your URL which invokes the action below
def finish_oauth
@consumer = OAuth::Consumer.new("YOUR_CONSUMER_KEY","YOUR_CONSUMER_SECRET",{:site=>"http://twitter.com" })
@req_token = OAuth::RequestToken.new(@consumer,session[:request_token],session[:request_token_secret])
# Request user access info from Twitter
@access_token = @req_token.get_access_token
# Store the OAuth info for the user
@user = current_user
@user.update_attributes(:token=>@access_token.token,:token_secret=>@access_token.secret)
# Send the user on their way
redirect_to user_path(@user)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment