我正在努力让 Facebook 与 devise 和omniauth 合作。几天来我一直试图解决这个问题,但没有成功。当我按下“Facebook 连接”按钮时,会出现登录页面。输入 FB 电子邮件和密码后,我将被重定向到带有 url 后缀“{root domain}/users/sign_in# - ”的页面,同时出现错误提示“translation missing: en.devise.authentications.user 。失败。” 未将用户添加到数据库中。我已经有一段时间了,到目前为止它非常令人沮丧。我的路线如下:
路线.rb
new_user_session GET /users/sign_in(.:format) sessions#new
user_session POST /users/sign_in(.:format) sessions#create
destroy_user_session DELETE /users/sign_out(.:format) sessions#destroy
user_omniauth_authorize /users/auth/:provider(.:format) authentications#passthru {:provider=>/facebook/}
user_omniauth_callback /users/auth/:action/callback(.:format) authentications#(?-mix:facebook)
在 devise.rb 我有:
config.omniauth :facebook, Facebook::APP_ID.to_s, Facebook::SECRET.to_s
require 'devise/orm/active_record'
config.authentication_keys = [ :email ]
config.http_authenticatable = false
config.http_authenticatable_on_xhr = false
config.navigational_formats = [:js, :html, :json]
config.token_authentication_key = :auth_token
config.sign_out_via = :delete
配置初始化程序omniauth.rb
if Rails.env == 'development' || Rails.env == 'test'
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, 'XXXXXX', 'XXXXXXXXXXXXX'
end
else
# Production
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, 'PRODUCTION_APP_ID', 'PRODUCTION_APP_SECRET'
end
end
在 users.rb 我有:
def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = access_token.extra.raw_info
if user = User.where(:email => data.email).first
if user.facebook_id.nil?
user.facebook_id = data.id
user.save
end
user
else # Create a user with a stub password.
token = Devise.friendly_token[0,20]
User.create!(:email => data.email, :name=>data.name, :facebook_id=>data.id, :password => token, :password_confirmation=> token)
end
end
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
user.name = data["name"]
user.email = data["email"]
user.facebook_id = session["devise.facebook_data"]["extra"]["raw_info"]['id']
end
end
end
在身份验证控制器中(从omniauth 回调控制器派生)
class AuthenticationsController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model
@user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
if @user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
sign_in_and_redirect @user, :event => :authentication
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
flash[:notice] = "Please register an account first!"
redirect_to "/"
end
end
end
在开发日志中,有:
2013 年 1 月 22 日 20:09:28 -0500 开始为 127.0.0.1 获取“/users/auth/facebook”
Started GET "/users/auth/facebook/callback? state=blah&code=blah" for 127.0.0.1 at
2013-01-22 20:09:29 -0500
Processing by AuthenticationsController#failure as HTML
Parameters: {"state"=>"blah", "code"=>"blah"}
Redirected to http://myapp.com/users/sign_in
Completed 302 Found in 2ms (ActiveRecord: 0.0ms)
在 Facebook 应用程序设置页面上,我有:
应用程序域:(空)
网站网址:http://myapp.com/users/auth/facebook/callback?
并启用沙盒模式。在这方面我将不胜感激。我已经尝试了所有可能的方法并检查了许多站点,但我遇到了障碍。