我的Rails应用程序当我试图将gem "omniauth-facebook"
与devise集成时,我在facebook上收到以下错误:The redirect_uri URL must be absolute
。
下面是我的配置
设计.rb
config.omniauth :facebook, "ID", "SECRET",callback_url: "/auth/facebook"
my user.rb型号:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:facebook]
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image
end
end
end
我的路线:
devise_for :users, :controllers => {:omniauth_callbacks => "users/omniauth_callbacks"}
和控制器omniauthcallbackcontroller
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# 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 => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def failure
redirect_to root_path
end
end
我错过了什么?
最佳答案
添加
:client_options => {:ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}}
在初始化器中
config.omniauth
为我解决了这个问题。我还将
path
配置为绝对值,如下所示:callback_url: ENV['SERVER_ROOT']+'/users/auth/facebook/callback'
关于ruby-on-rails - 设计omniauth-facebook redirect_uri网址必须是绝对的,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34304444/