0

背景:尝试将 twitter gem 用于 ruby​​-on-rails。

在路线:

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

(twitter_sessions 是我的应用程序中 twitter 会话的控制器)。

视图只有一个文件new.html.erb,非常简单:

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

和 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

然而,在我点击“twitter”按钮后,我得到了这个错误:

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'

如果我直接转到 finalize url,http://localhost:3000/twitter_sessions/finalize我会收到此错误:

Unknown action

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

有任何想法吗?谢谢

4

1 回答 1

4

我会尝试与您的路线有所不同

map.resources :twitter_sessions, :collection { :finalize => :get } #use correct verb
于 2010-03-27T07:19:06.843 回答