Rails& Omniauth:Twitter / auth / failure?message = invalid_response

时间:2011-08-05 15:49:36

标签: ruby-on-rails ruby-on-rails-3 twitter omniauth

我正在使用omniauth在我的应用程序中实现twitter身份验证。我有facebook身份验证工作,我知道twitter身份验证的代码失败,但我无法弄清楚原因!

Twitter没有提供电子邮件地址,我的网站根据email / pw进行身份验证,所以我只允许用户将Twitter帐户添加到预先存在的帐户,通过在网站上注册或通过Facebook创建。

这是相关代码,是我services_controller.rb的一部分:

logger.info("USER SIGNED IN")
auth = Service.find_by_provider_and_uid(provider, uid)
logger.info("AUTH: #{auth}")
#If user is signed in but does not have this service linked to his account
if !auth
  logger.info("AUTH IS NULL, SERVICE NOT LINKED TO ACCOUNT")
  logger.info("PROVIDER: #{provider}, UID: #{uid}, EMAIL: #{email}")
  user.services.create!(:provider => provider, :uid => uid, :uemail => email)
  logger.info("SERVICE CREATED")
  flash[:notice] = 'Sign in via ' + provider.capitalize + ' has been added to your account.'
  redirect_to services_path

现在,我在日志中看到的最后一件事是:

AUTH IS NULL, SERVICE NOT LINKED TO ACCOUNT
PROVIDER: twitter, UID: 3195xxxxx, EMAIL: 
Completed   in 231ms
Started GET "/auth/failure?message=invalid_response" for 127.0.0.1 at 2011-08-05 20:52:30 +0530
ActionController::RoutingError (No route matches "/auth/failure"):

从上面的代码中可以看出,这意味着行user.services.create!(...)失败,这就是我在日志中看不到SERVICE CREATED的原因。我不知道为什么它失败了。我知道email变量是空的,但由于Twitter不提供电子邮件,所以这是预期的。我甚至尝试用:uemail => email替换:uemail => "validemail@gmail.com",但我仍然得到相同的输出。

1 个答案:

答案 0 :(得分:1)

可能就行了

user.services.create!(:provider => provider, :uid => uid, :uemail => email)

引发异常,因此不执行代码。你使用控制台试过那条线吗?

可能是验证失败,例如validate_uniquenessvalidates_presence_of。它可能是服务表中的重复uid

从控制台运行该行将为您提供答案。