Facebookのログイン機能を使用するrails(4.2.0)アプリケーションがあります。主な宝石はdevise(3.4.0)とomniauth-facebook(2.0.0)です。Facebookにアプリケーションを登録し、そのテストアプリを開発に使用しています。Facebookのログイン機能は開発環境で機能します。
本番サーバーでFacebookログイン機能を使用しようとすると、「指定されたURLはアプリケーション構成で許可されていません:指定されたURLの1つ以上がアプリの設定で許可されていません。WebサイトのURLと一致する必要があります」というエラーが表示されます。キャンバスURL、またはドメインは、アプリのドメインの1つのサブドメインである必要があります。」
開発環境で使用されているテストアプリの設定の詳細は次のとおりです-
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"
本番環境で使用されている登録済みアプリの設定の詳細は以下のとおりです。
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からのcredsを次のように使用しています
# ==> 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に関連するroutes.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]
コメントを追加