如何为 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 settings/advanced 上,这是我的设置:
有效的 OAuth 重定向 URI 是 =
http://localhost:3000
在 facebook settings/basic 上,我的 App Domains 是 =
本地主机
我的网站 URL 是 =
http://localhost:3000/
我的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) %>
在您的 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 URL。
我的 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 settings/advanced 上,这是我的设置: 有效的 OAuth 重定向 URI 是 = http://localhost:3000
在 facebook settings/basic 上,我的 App Domains 是 = 本地主机
我的网站 URL 是 = http://localhost:3000/
我的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) %>
在您的 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 URL。