我有一个使用Facebook登录功能的rails(4.2.0)应用程序。主要的宝石是devise(3.4.0)和omniauth-facebook(2.0.0)。我已经在Facebook上注册了该应用程序,并一直在使用其测试应用程序进行开发。Facebook登录功能可在开发环境中使用。
尝试在生产服务器上使用Facebook登录功能时,出现错误,因为“应用程序配置不允许使用URL:应用程序的设置不允许使用一个或多个给定URL。它必须与网站URL或画布URL,或者域必须是该应用程序域之一的子域。”
将在开发ENV被用于测试应用程序设置的详细信息如-
Settings:
Basic:
App Domains: 'localhost'
Website:
Site URL: 'http://localhost:3000'
Advanced:
OAuth Settings:
Embedded browser OAuth Login: Yes
Valid OAuth redirect URIs: "http://localhost:3000/users/auth/facebook/callback"
该生产ENV被用于用于注册应用程序设置的详细信息如-
Settings:
Basic:
App Domains: 'www.mysite.co'
Website:
Site URL: 'http://www.mysite.co'
Advanced:
OAuth Settings:
Embedded browser OAuth Login: Yes
Valid OAuth redirect URIs: "http://www.mysite.co/users/auth/facebook/callback"
我在secrets.yml中指定了以下内容
development:
secret_key_base: some_secret_key
facebook:
app_id: test_app_id
app_secret: test_app_secret
production:
secret_key_base: some_secret_key
facebook:
app_id: registered_app_id
app_secret: registered_app_secret
并且一直在设计初始化程序中使用secrets.yml中的凭据作为
# ==> OmniAuth
# Add a new OmniAuth provider. Check the wiki for more information on setting
# up on your models and hooks.
# config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo'
require 'omniauth-facebook'
config.omniauth :facebook, Rails.application.secrets.facebook['app_id'], Rails.application.secrets.facebook['app_secret'], scope: ['user_photos', 'email', 'public_profile']
实际域名(涂黑)在任何地方都没有错别字,并且无论在何处使用都相同。
与omniauth相关的route.rb包含为
cat config/routes.rb
Rails.application.routes.draw do
root 'home#index'
devise_for :users, controllers: { omniauth_callbacks: "users/omniauth_callbacks" }
# routes related to other controllers
end
路线如下
bundle exec rake routes | grep user
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_omniauth_authorize GET|POST /users/auth/:provider(.:format) users/omniauth_callbacks#passthru {:provider=>/facebook/}
user_omniauth_callback GET|POST /users/auth/:action/callback(.:format) users/omniauth_callbacks#:action
The only code related to omniauth in the entire app is as
$ cat app/controllers/users/omniauth_callbacks_controller.rb
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
end
Upon further digging the problem, it was observed that the error didnt occur when 'www.example.com' was specified in the url and hence, the callback worked. When 'example.com' was specified in the address bar and facebook login tried, the login crashed with the above error.
So, I fixed the above issue by making some changes to the settings in for the facebook app. I donno if this is the right approach but it worked out. Just making the change as in point 2 didnt solve the problem.
Changes are:
1)使用“ example.com”和“ www.example.com”指定“应用程序域” 2)将“客户端OAuth登录”启用为“是” 3)使用“ http:// example ”指定“有效OAuth重定向URI” .com / users / auth / facebook / callback '和' http://www.example.com/users/auth/facebook/callback '
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句