6

我在 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
4

1 回答 1

4

将应用回调 url 设置为 localhost 时出现问题。
尝试使用 url shortner 设置您的 twitter 应用程序的回调 url。

您可以使用http://goo.gl/之类的 url 缩短器,并将您的回调 url 替换为适合您的。

http://127.0.0.1:3000/auth/twitter/callback => http://goo.gl/QVYCy
于 2013-06-05T02:17:12.597 回答