【发布时间】:2011-08-06 00:12:02
【问题描述】:
我正在使用omniauth 在我的应用程序中实现 twitter 身份验证。我有 facebook 身份验证工作,我知道 twitter 身份验证代码在哪里失败,但我不知道为什么!
Twitter 不提供电子邮件地址,并且我的网站基于电子邮件/密码进行身份验证,因此我只允许用户将 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 => "validemail@gmail.com" 替换:uemail => email,但我仍然得到相同的输出。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 twitter omniauth