如何为rails的heroku站点设置facebook应用程序?

时间:2015-11-17 11:59:56

标签: ruby-on-rails heroku omniauth-facebook

我的Facebook应用安装程序仅适用于我的本地主机,但不适用于heroku网站。

我在heroku日志中收到此错误。

    ERROR -- omniauth: (facebook) Authentication failure! no_authorization_code: OmniAuth::Strategies::Facebook::NoAuthorizationCodeError, must pass either a `code` (via URL or by an `fbsr_XXX` signed request cookie)

在facebook上设置/高级,这是我的设置: 有效的OAuth重定向URI为= http://localhost:3000

在Facebook设置/基本版上,我的应用领域是= 本地主机

我的网站网址= http://localhost:3000/

my devise.rb

    config.omniauth :facebook, 'somekey', 'somekey', scope: 'email', info_fields: 'email, name'

我的omniauth_callbacks_controller.rb

    class OmniauthCallbacksController < Devise::OmniauthCallbacksController 

    def facebook
@user = User.from_omniauth(request.env["omniauth.auth"])

if @user.persisted?
  sign_in_and_redirect @user, :event => :authentication
  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
  end

我的app / models / user.rb

    class User < ActiveRecord::Base
    devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable

    def self.from_omniauth(auth)
result = User.where(email: auth.info.email).first

if result
  return result
else
  where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
    user.fullname = auth.info.name
    user.provider = auth.provider
    user.uid = auth.uid
    user.email = auth.info.email
    user.image = auth.info.image
    user.password = Devise.friendly_token[0, 20]
  end
end
 end
 end

在我的app / views / devise / sessions / new.html.erb中,

<%= link_to "Sign In with Facebook", user_omniauth_authorize_path(:facebook) %>

1 个答案:

答案 0 :(得分:1)

在你的devise.rb中,我建议使用如下的ENV变量:

heroku config:set FACEBOOK_KEY="your_fb_app_key"
heroku config:set FACEBOOK_SECRET="your_fb_app_secret"

在开发模式下,您可以使用非常有用的Dotenv gem在本地配置它们。

然后使用以下命令在Heroku配置中设置这些:

==

完成此操作后,您的Heroku应用程序应该选择正确的Facebook凭据。只需确保您的Facebook应用程序配置为在应用程序域设置中使用您的生产Heroku URL。