7

成功(twitter) Authentication failure! invalid_credentials: OAuth::Unauthorized, 401 Unauthorized登录推特后出现错误,页面被重定向到登录页面

这是应用程序配置

路线.rb

devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

设计.rb

config.omniauth :twitter, "KEY", "SECRET"

omn​​iauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController  
  def twitter
    auth = env["omniauth.auth"]
    Rails.logger.info("auth is **************** #{auth.to_yaml}")
    @user = user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.new
    if @user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success"
      sign_in_and_redirect @user, :event => :authentication
    else
      session["devise.twitter_uid"] = auth["uid"]
      redirect_to new_user_registration_url
    end
  end
end

用户.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :provider, :uid
  # attr_accessible :title, :body
end

index.html.erb

<h3>Home</h3>

<%if user_signed_in? %>
  <div><%="Welcome #{current_user.email}"%></div>

<div><%=link_to "Logout", destroy_user_session_path, method: :delete%></div>

<%else%>
<div><%=link_to "Sign in twitter", user_omniauth_authorize_path(:twitter)%></div>
<%end%>

控制台日志

Started GET "/users/auth/twitter" for 127.0.0.1 at 2012-07-09 18:58:16 +0530
(twitter) Callback phase initiated.
(twitter) Callback phase initiated.
(twitter) Authentication failure! invalid_credentials: OAuth::Unauthorized, 401 Unauthorized

Started GET "/users/auth/twitter/callback?oauth_token=rLCEqgAWPtoIzce475sacKwoqU5baEdz0JnmldXE&oauth_verifier=xYPoz2LZGHQlmz4akoVGkarPtZTebCOmeWzPUqLcPA" for 127.0.0.1 at 2012-07-09 18:58:48 +0530
Processing by Users::OmniauthCallbacksController#failure as HTML
  Parameters: {"oauth_token"=>"rLCEqgAWPtoIzce475sacKwoqU5baEdz0JnmldXE", "oauth_verifier"=>"xYPoz2LZGHQlmz4akoVGkarPtZTebCOmeWzPUqLcPA"}
Redirected to http://localhost:3000/users/sign_in

dev.twitter.com 中的回调 URL 之前是http://127.0.0.1:3000. 从Devise、Omniauth 和 Twitter我将其更改为http://127.0.0.1:3000/auth/twitter/callback但仍然出现错误

有人可以在这里帮忙解决这个问题吗?

谢谢,阿米特帕特尔

4

2 回答 2

22

我发现了这个问题。devise.rb我在和中都配置了提供程序omniauth.rb。我只是删除了omniauth.rb,它就开始工作了。

于 2012-07-11T06:29:28.600 回答
2

尝试将您的 twitter KEY 代码放在您的 initilizers 文件夹中的 omniauth.rb 文件中。像这样:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twitter, 'KEY', 'SECRET'
end

per: https://github.com/intridea/omniauth推特的身份验证,毕竟是通过omniauth,而不是设计。

祝你好运!@thatdankent

于 2012-07-10T03:16:32.280 回答