![](/img/trans.png)
[英]OAuth::Unauthorized 401 Unauthorized for omniauth-twitter in rails
[英]Omniauth-twitter in rails: OAuth::Unauthorized 401
我在使用twitter進行基本的omniauth身份驗證時被封鎖了2天。 我跟着Ryan Bates關於簡單omniauth的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
// sessions_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
將應用程序回調URL設置為localhost時出現問題。
嘗試使用網址縮短設置您的Twitter applcation的回調網址。
您可以使用像http://goo.gl/這樣的網址縮短程序,並將您的回調網址替換為適合您的網址。
http://127.0.0.1:3000/auth/twitter/callback => http://goo.gl/QVYCy
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.