我在 twitter 上进行基本的omniauth 身份验证已被封锁了2 天。我在简单的omniauth 上跟随Ryan Bates 的railscast,但无法通过我尝试登录时引发的OAuth::Unauthorized 401 异常。请帮助!我的代码粘贴在下面:
twitter info:
website: [http://127.0.0.1:3000]
callbarck url: [http://127.0.0.1:3000/auth/twitter/callback]
//routes.rb
Sentimentalist::Application.routes.draw do
resources :dashboard, only: [:index]
resources :welcome
root :to => 'welcome#index'
match '/auth/twitter/callback', to: 'sessions#create'
match "/signout" => "sessions#destroy", :as=>:signout
match ':controller(/:action(/:id))(.:format)'
end
//application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Sentimentalist</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<% yield(:head) %>
</head>
<body>
<div id="container">
<div id="user_nav">
<% if current_user %>
Welcome <%= current_user.name %>!
<%= link_to "Sign Out", signout_path %>
<% else %>
<%= link_to "Sign in with Twitter", "/auth/twitter" %>
<% end %>
</div>
</div>
<%= yield %>
</body>
</html>
// config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, '###', '###'
end
// session_controller.rb
def create
auth = request.env["omniauth.auth"]
user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) ||User.create_with_omniauth(auth)
session[:user_id] = user.id
redirect_to root_url, :notice => "Signed in!"
end
def destroy
session[:user_id] = nil
redirect_to root_url, :notice => "Signed out!"
end