Created
September 7, 2010 18:21
-
-
Save hayesdavis/568777 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# 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
alvincrespo's thing fixed it, but I hit a new problem.
I think I'm getting a 'nil' with the @oauth_consumer around line 24. I think. NoMethodError - undefined method
http_method' for nil:NilClass: /usr/lib/ruby/gems/1.8/gems/oauth-0.4.5/lib/oauth/tokens/request_token.rb:18:in
get_access_token'Since @oauth_consumer isn't defined, I'm guessing tha'ts it. Is that supposed to be @consumer?