当我尝试将 gem "omniauth-facebook"
与 devise 集成时,我的 Rails 应用程序在 facebook 上收到以下错误:redirect_uri URL 必须是绝对的
。
以下是我的配置
设计.rb
config.omniauth :facebook, "ID", "SECRET",callback_url: "/auth/facebook"
我的 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"}
和 Controller OmniauthCallbacksController
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 Url 必须是绝对的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34304444/