Ruby on rails Ruby on rails omniauth twitter和Desive
我尝试实现oauth twitter gem,以允许我的用户注册/登录twitter,问题是当用户在twitter上接受我的应用程序后被重定向时,它会将用户重定向到注册页面 这是我的配置: app/controllers/users/omniauth\u callbacks\u controller.rbRuby on rails Ruby on rails omniauth twitter和Desive,ruby-on-rails,twitter,oauth,devise,Ruby On Rails,Twitter,Oauth,Devise,我尝试实现oauth twitter gem,以允许我的用户注册/登录twitter,问题是当用户在twitter上接受我的应用程序后被重定向时,它会将用户重定向到注册页面 这是我的配置: app/controllers/users/omniauth\u callbacks\u controller.rb class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController def twitt
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def twitter
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Twitter") if is_navigational_format?
else
session["devise.twitter.data"] = request.env["omniauth.auth"].except("extra")
redirect_to new_user_registration_url
end end
end
在config/initializers/designe.rb中
我补充说:
require 'omniauth-twitter'
config.omniauth :twitter, "APP_ID", "APP_KEY", :strategy_class => OmniAuth::Strategies::Twitter
但当我的用户通过链接注册时:
<%= link_to "Sign in with twitter", user_omniauth_authorize_path(:twitter) %>
然后在twitter上接受我的应用程序,它们被重定向到注册页面,并且没有填充任何内容,我知道无法通过twitter检索电子邮件地址,但是名称呢?它应该填上名字吗
感谢您指出您的用户模型的新的带会话的会话(params,session)方法中的错误,您没有从twitter返回的数据中检索数据,请在那里执行一些任务。注意,现在可以从twitter检索电子邮件。为此,您必须进入您的Twitter开发帐户(),选择您的应用程序并进入“权限”设置。在那里,您可以单击“编辑”并选中“向用户请求电子邮件地址”框 之后,您的omniauth和Twitter应该可以正常工作。也就是说,您可能希望通知用户他们遇到的错误。因此,如果身份验证失败,我会在您的
twitter
方法中添加一个警报通知:
def twitter
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Twitter") if is_navigational_format?
else
session["devise.twitter.data"] = request.env["omniauth.auth"].except("extra")
redirect_to new_user_registration_url, alert: @user.errors.full_messages.join("\n")
end
end
您的查看代码是否使用@user.name或您试图自动填写的任何值?@johnpulashenfelter我不确定您在问什么,但我确实在app/views/users/show.html.erb中找到了它,在用户配置文件页面中显示用户名谢谢您指出这一点!你能给我一些关于如何从这里获得一些数据返回的建议吗?看看twitter api,或者只是将会话[“design.twitter.data”]值输出到rails控制台,然后得到你想要的。谢谢你-我不确定如何才能做到这一点?你能把rails控制台给我吗?thanks@zacchj在控制台中键入命令“rails s”以启动rails服务器。在代码中,可以使用puts输出所有内容,puts的结果将显示在该控制台中。
<%= link_to "Sign in with twitter", user_omniauth_authorize_path(:twitter) %>
def twitter
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Twitter") if is_navigational_format?
else
session["devise.twitter.data"] = request.env["omniauth.auth"].except("extra")
redirect_to new_user_registration_url, alert: @user.errors.full_messages.join("\n")
end
end