认证失败:Devise + OmniAuth + Twitter [英] Authentication failure : Devise + OmniAuth + Twitter
问题描述
我正在获得(twitter)身份验证失败! invalid_credentials:OAuth ::未经授权,401未经授权
成功下载到Twitter后发生错误,页面被重定向到登录页面
I am getting (twitter) Authentication failure! invalid_credentials: OAuth::Unauthorized, 401 Unauthorized
error after successfully loin to twitter and page is redirected to sign in page
这里是应用程序配置
routes.rb
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
devise.rb
config.omniauth :twitter, "KEY", "SECRET"
omniauth_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
user.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中的回调URL .com
以前它是 http://127.0.0.1:3000
。从 Devise,Omniauth和Twitter ,将其更改为 http://127.0.0.1:3000/ auth / twitter / callback
但仍然收到错误
Callback URL in dev.twitter.com
Earlier it was http://127.0.0.1:3000
. From Devise, Omniauth and Twitter I changed it to http://127.0.0.1:3000/auth/twitter/callback
but still getting error
有人可以帮助这里纠正这个问题?
Would anyone please help here to rectify the issue?
谢谢,
Amit Patel
Thanks, Amit Patel
推荐答案
我发现了这个问题。我已经在 devise.rb
和 omniauth.rb
中配置了提供程序。我只是删除了omniauth.rb,它开始工作。
I found the issue. I have configured providers in both devise.rb
and omniauth.rb
. I simply removed omniauth.rb and it started working.
这篇关于认证失败:Devise + OmniAuth + Twitter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!