2013-06-05 27 views
6

Tôi đã bị chặn đường trong 2 ngày trên xác thực omniauth cơ bản với twitter. Tôi theo dõi cùng với Ryan Bates 'railscast trên omniauth đơn giản, nhưng đã không thể vượt qua OAuth :: Unauthorized 401 ngoại lệ được nêu ra khi tôi cố gắng đăng nhập. Xin vui lòng giúp đỡ! Mã của tôi được dán bên dưới:Omniauth-twitter trong đường ray: OAuth :: Trái phép 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 

Trả lời

4

Có vấn đề khi bạn đặt url gọi lại ứng dụng của mình thành localhost.
Hãy thử đặt url gọi lại của twitter applcation bằng cách sử dụng trình rút gọn url.

Bạn có thể sử dụng trình rút gọn url như http://goo.gl/ và thay thế url gọi lại của bạn bằng url phù hợp với bạn.

http://127.0.0.1:3000/auth/twitter/callback => http://goo.gl/QVYCy 
+0

url may twitter bác bỏ rút ngắn cho callbacks :( – user2040608

+1

@ user2040608 Tôi không nghĩ như vậy? Dù sao, http://stackoverflow.com/questions/800827/twitter-oauth-callbackurl-localhost-development – oldergod

+0

làm việc cho tôi! Cảm ơn! – CloudRide

Các vấn đề liên quan