是不是authlogic_facebook_connect应该绕过登录/密码验证?我错过了一些配置步骤吗?
任何帮助,将不胜感激!
解决方法
Facebook连接已过时.所有酷孩子现在都在使用带有OAuth的Facebook Graph. Facebook团队甚至关闭了Facebook Connect的文档.
无论如何,这适用于Authlogic Facebook连接,它根本不再起作用.
最后,我一直使用Authlogic进行正常登录和帐户管理,但对于Facebook连接,我编写了自己的代码.
您可能使用过facebooker gem.卸载该gem,但保留config / facebooker.yml文件.
试试这个:
>通过在config / initializers / load_config.rb中添加此代码继续使用config / facebooker.yml(您需要创建此文件)
config = YAML.load_file(“#{Rails.root} /config/facebooker.yml”)|| {}
facebook_config = config [‘common’] || {}
facebook_config.update(config [Rails.env] || {})
FB_CONFIG = facebook_config.symbolize_keys
>在user_controller.rb中添加此代码:
def facebook_oauth_callback if not params[:code].nil? callback = url_for(:host => APP_CONFIG[:host],:controller => 'gallery/user',:action => 'facebook_oauth_callback') url = URI.parse("https://graph.facebook.com/oauth/access_token?client_id=#{FB_CONFIG[:application_id]}&redirect_uri=#{callback}&client_secret=#{FB_CONFIG[:secret_key]}&code=#{CGI::escape(params[:code])}") http = Net::HTTP.new(url.host,url.port) http.use_ssl = (url.scheme == 'https') tmp_url = url.path + "?" + url.query request = Net::HTTP::Get.new(tmp_url) response = http.request(request) data = response.body access_token = data.split("=")[1] if access_token.blank? flash[:notice] = 'An error occurred while connecting through Facebook,please try again later.' else url = URI.parse("https://graph.facebook.com/me?access_token=#{CGI::escape(access_token)}") http = Net::HTTP.new(url.host,url.port) http.use_ssl = (url.scheme == 'https') tmp_url = url.path + "?" + url.query request = Net::HTTP::Get.new(tmp_url) response = http.request(request) user_data = response.body user_data_obj = JSON.parse(user_data) @user = User.new_or_find_by_facebook_oauth_access_token(access_token,{:user_data => user_data_obj}) if @user.new_record? session[:user] = @user session[:external_app] = "Facebook" redirect_to(:action => 'new_details') else user_session = UserSession.create(@user) flash[:notice] = "Successfully logged in." redirect_back_or_default root_url end end end end def create_facebook redirect_to("https://graph.facebook.com/oauth/authorize?client_id=#{FB_CONFIG[:application_id]}&redirect_uri=" + url_for(:host => APP_CONFIG[:host],:action => 'facebook_oauth_callback') + "&scope=email,offline_access") end
>在用户模型(app / models / user.rb)中添加find方法:
def self.new_or_find_by_facebook_oauth_access_token(access_token,options = {}) user = User.find_by_facebook_oauth_access_token(access_token) if user.blank? #code to create new user here end user end
>在视图中添加指向动作create_facebook的链接,例如
<%= link_to image_tag('gallery / button-facebook.png',:title =>“注册Facebook”,:alt =>“注册Facebook”),{:action => ‘create_facebook’},{:target => ‘_parent’}%>
说明
>在第一步中,您创建了一个文件以从facebooker.yml获取配置数据
>在第二步中,您基本上创建了两个函数,一个用于将用户发送到Facebook站点(create_facebook),另一个用于捕获回调数据(facebook_oauth_callback),这个想法来自此页面:http://www.wisejive.com/2010/05/facebook-oauth-on-rails.html
>在第三步中,您刚刚在User模型中添加了一个方法,以根据从Facebook返回的信息查找或创建新的(如果未找到)用户.
该代码仅适用于我的项目,不要只是复制粘贴它,而是逐行学习它.随着时间的推移,我意识到最好使用你能理解的简单代码,而不是使用其他复杂的插件,当出现问题时你无法修复它们……