views:

284

answers:

1

background: trying to use the twitter gem for ruby-on-rails.

in routes:

map.resources :twitter_sessions
map.finalize_twitter_sessions 'twitter_sessions/finalize', :controller => 'twitter_sessions', :action => 'finalize'

(twitter_sessions is the controller for the twitter sessions in my app).

The view has one file new.html.erb and is very simple:

<% form_tag(twitter_sessions_path)  do |f| %>
   <p><%= submit_tag "twitter!" %></p>
<% end %>

and the twitter_sessions_controller.rb:

def new
end

def create
  oauth.set_callback_url(finalize_twitter_sessions_url)

  session['rtoken']  = oauth.request_token.token
  session['rsecret'] = oauth.request_token.secret

  redirect_to oauth.request_token.authorize_url
end

def destroy
  reset_session
  redirect_to new_session_path
end

def finalize
  oauth.authorize_from_request(session['rtoken'], session['rsecret'], params[:oauth_verifier])

  profile = Twitter::Base.new(oauth).verify_credentials
  session['rtoken'] = session['rsecret'] = nil
  session[:atoken] = oauth.access_token.token
  session[:asecret] = oauth.access_token.secret

  sign_in(profile)
  redirect_back_or root_path
end

However, after I click the "twitter" button, I get this error:

401 Unauthorized

.../gems/oauth-0.3.6/lib/oauth/consumer.rb:200:in `token_request'
.../gems/oauth-0.3.6/lib/oauth/consumer.rb:128:in `get_request_token'
.../gems/twitter-0.9.2/lib/twitter/oauth.rb:32:in `request_token'
.../gems/twitter-0.9.2/lib/twitter/oauth.rb:25:in `set_callback_url'
app/controllers/twitter_sessions_controller.rb:7:in `create'

If I go to the finalize url, http://localhost:3000/twitter_sessions/finalize, directly, I get this error:

Unknown action

No action responded to show. Actions: create, destroy, finalize, isLoggedInToBeta, login_required, and new

Any ideas? Thanks

+4  A: 

I'd try something a little different with your routes:

map.resources :twitter_sessions, :collection { :finalize => :get } #use correct verb
Andy Gaskell
Routes are matched in the order you define them in, so by defining the action after the resources it will interpret the action as an ID for the show action
Ryan Bigg
I put that before the other twitter_session routes but not joy. What am I missing?
cbrulak
Can you post all entries in your routes that have to do with twitter sessions?
Andy Gaskell
Thanks for helping but my problem was more fundamental. My app settings on twitter did not have the 'browser' setting checked: Facepalm.http://stackoverflow.com/questions/1280295/keep-getting-oauthunauthorized-error-when-using-oauth-and-twitter-ruby-gems/1994569#1994569
cbrulak