繁体   English   中英

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

[英]how to setup facebook app for heroku site for rails?

我的 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 设置/基本上,我的应用程序域是 = localhost

我的站点 URL 是 = http://localhost:3000/

我的设计.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

我的应用程序/模型/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) %>

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

config.omniauth :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'], scope: 'email', info_fields: 'email, name'

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

然后在 Heroku 配置中设置这些:

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

完成此操作后,您的 Heroku 应用程序应获取正确的 Facebook 凭据。 只需确保您的 Facebook 应用程序配置为与应用程序域设置中的生产 Heroku URLS 一起使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM