Skip to content

Instantly share code, notes, and snippets.

@hayesdavis
Created September 7, 2010 18:21
Show Gist options
  • Save hayesdavis/568777 to your computer and use it in GitHub Desktop.
Save hayesdavis/568777 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
@hayesdavis
Copy link
Author

@tibbon, Yes that should be @consumer. I made that change and incorporated @alvincrespo's change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment