###前提・分からないこと
いつもお世話になっております。
teratailさまにいつも、助けられています。
独学で進めてきたRuby on Railsによるサイト制作も個人の完成として、一つの区切りを迎えることができました。まず、本当にありがとうございます。
お手すきの際に、ご教示いただけると嬉しいです。
開発環境:Ruby on Rails 5.0.0.1
利用しているgem:Devise
Deviseを使って、Facebook認証をしようとしています。
エラーメッセージはおそらく、Deviseに設定されているヘルパーだとは思いますが、どうやってもこのエラーが出てしまい、Facebook認証が成功しません。
Could not authenticate you from Facebook because "Invalid credentials".
そのほか、質問者さまの投稿や、Web投稿をみて、いろいろ試してみましたが、うまくいかず。。
下記のURLを参考にしながら進めました。
http://ticklecode.com/fb_omniauth/
###該当するソースコード
user.rb
Ruby
1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :lockable, :timeoutable 4 devise :database_authenticatable, :registerable, :confirmable, 5 :recoverable, :rememberable, :trackable, :validatable, :omniauthable 6 mount_uploader :image, ImageUploader 7 validates :name, uniqueness: { case_sensitive: :false }, length: { minimum: 4, maximum: 20 } 8 has_many :reviews 9 10 def self.find_for_oauth(auth) 11 user = User.where(uid: auth_uid, provider: auth.provider).first 12 13 unless user 14 user = User.create( 15 uid: auth.uid, 16 provider: auth.provider, 17 email: User.dummy_email(auth), 18 password: Devise.friendly_token[0, 20] 19 ) 20 end 21 22 user 23 end 24 25 def self.find_first_by_auth_conditions(warden_conditions) 26 conditions = warden_conditions.dup 27 if login = conditions.delete(:login) 28 where(conditions).where(["name = :value OR lower(email) = lower(:value)", { :value => login }]).first 29 else 30 where(conditions).first 31 end 32 end 33 34 private 35 36 def user_params 37 params.require(:user).permit(:name, :email, :password, :password_confirmation, :image, :background) 38 end 39 40 def self.dummy_email(auth) 41 "#{auth.uid}-#{auth.provider}@example.com" 42 end 43end
users/omniauth_callbacks_controller.rb
Ruby
1class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController 2 def facebook 3 callback_from :facebook 4 end 5 6 private 7 8 def callback_from(provider) 9 provider = provider.to_s 10 11 @user = User.find_for_oauth(request.env['omniauth.auth']) 12 13 if @user.persisted? 14 flash[:notice] = I18n.t('devise.omniauth_callbacks.success', kind: provider.capitalize) 15 sign_in_and_redirect @user, event: :authentication 16 else 17 session["devise.#{provider}_data"] = request.env['omniauth.auth'] 18 redirect_to new_user_registration_url 19 end 20 end 21end
routes.rb
Ruby
1Rails.application.routes.draw do 2 3 get 'users/show' 4 5 devise_for :users, controllers: { registrations: 'users/registrations', 6 omniauth_callbacks: 'users/omniauth_callbacks', 7 sessions: 'users/sessions', confirmations: 'users/confirmations', 8 passwords: 'users/passwords' 9 } 10 devise_scope :user do 11 get '/users/sign_out', to: 'users/sessions#destroy' 12 end 13end
Devise.rb
Ruby
1# Use this hook to configure devise mailer, warden hooks and so forth. 2# Many of these configuration options can be set straight in your model. 3Devise.setup do |config| 4 require 'devise/orm/active_record' 5・・・ 6 # ==> Mountable engine configurations 7 # When using Devise inside an engine, let's call it `MyEngine`, and this engine 8 # is mountable, there are some extra configurations to be taken into account. 9 # The following options are available, assuming the engine is mounted as: 10 # 11 # mount MyEngine, at: '/my_engine' 12 # 13 # The router that invoked `devise_for`, in the example above, would be: 14 # config.router_name = :my_engine 15 # 16 # When using OmniAuth, Devise cannot automatically set OmniAuth path, 17 # so you need to do it manually. For the users scope, it would be: 18 # config.omniauth_path_prefix = '/my_engine/users/auth' 19 config.secret_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' 20 require "omniauth-facebook" 21 config.omniauth :facebook, ENV['APP_ID'], ENV['APP_SECRET'] 22end
omniauth.rb
Ruby
1Rails.application.config.middleware.use OmniAuth::Builder do 2end
環境変数は本番環境に設定済みです。
$ heroku config === サーバー名 Config Vars APP_ID: XXXXXXXXXXXXXXXX APP_SECRET: XXXXXXXXXXXXXXXXXXXXX
###追記
user.rb
Ruby
1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :lockable, :timeoutable 4 devise :database_authenticatable, :registerable, :confirmable, 5 :recoverable, :rememberable, :trackable, :validatable, :omniauthable, omniauthable: [:facebook] 6 mount_uploader :image, ImageUploader 7 validates :name, uniqueness: { case_sensitive: :false }, length: { minimum: 4, maximum: 20 } 8 has_many :reviews 9 attr_accessible :provider, :uid 10 11 def self.find_for_oauth(auth) 12 user = User.where(uid: auth_uid, provider: auth.provider).first 13 14 unless user 15 user = User.create( 16 uid: auth.uid, 17 provider: auth.provider, 18 email: User.dummy_email(auth), 19 token: auth.credintials.token, 20 password: Devise.friendly_token[4, 30] 21 ) 22 end 23 24 user 25 end 26 27 def self.find_first_by_auth_conditions(warden_conditions) 28 conditions = warden_conditions.dup 29 if login = conditions.delete(:login) 30 where(conditions).where(["name = :value OR lower(email) = lower(:value)", { :value => login }]).first 31 else 32 where(conditions).first 33 end 34 end 35 36 private 37 38 def user_params 39 params.require(:user).permit(:name, :email, :password, :password_confirmation, :image, :background) 40 end 41 42 def self.dummy_email(auth) 43 "#{auth.uid}-#{auth.provider}@example.com" 44 end 45end
attr_accessible :provider, :uidや、omniauthable: [:facebook]を追記しました。
token_params: { parse: :json }を追記しました。
###追記その2
ominiauthのバージョンがよくないのかと思い、
gem 'omniauth-facebook', '1.4.0'
に落としてみました。ですが、やはり通りません。。
どうなっているんでしょうかね。。。。
###追記その3
Facebookアプリ側の記載をします。
「アイテムを審査に送信」も今回の件は必要なさそうですよね。。。
###追記その4
ボタンを押すと、下記でFacebookログインが求められますが。。
ログイン後の対応がよくないのでしょうか。。
##追記その4【更新版】
Gemfile
Ruby
1source 'https://rubygems.org' 2・・・ 3gem 'dotenv-rails' 4gem 'omniauth-facebook', '1.4.0' 5・・・ 6group :production do 7 gem 'pg', '0.18.4' 8end 9 10# Windows環境ではtzinfo-dataというgemを含める必要があります 11gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
$gem list
Ruby
1・・・ 2multipart-post (2.0.0) 3nenv (0.3.0) 4nio4r (2.0.0, 1.2.1) 5nokogiri (1.7.2, 1.7.1, 1.6.7.2) 6notiffany (0.1.1, 0.0.8) 7orm_adapter (0.5.0) 8pg (0.18.4) 9・・・
gemfile.lock
・・・ notiffany (0.1.1) nenv (~> 0.1) shellany (~> 0.0) oauth2 (0.6.1) faraday (~> 0.7) httpauth (~> 0.1) multi_json (~> 1.3) omniauth (1.6.1) hashie (>= 3.4.6, < 3.6.0) rack (>= 1.6.2, < 3) omniauth-facebook (1.4.0) omniauth-oauth2 (~> 1.0.2) omniauth-oauth2 (1.0.2) oauth2 (~> 0.6.0) omniauth (~> 1.0) DEPENDENCIES ・・・ devise dotenv-rails geocoder gmaps4rails google-analytics-rails guard (= 2.13.0) guard-minitest (= 2.4.4) jbuilder (= 2.4.1) jquery-rails (= 4.1.1) listen (= 3.0.8) mini_magick minitest-reporters (= 1.1.9) omniauth-facebook (= 1.4.0) pg (= 0.18.4) puma (= 3.4.0) rails (= 5.0.0.1) rails-controller-testing (= 0.1.1) ransack rmagick sass-rails (= 5.0.6) spring (= 1.7.2) spring-watcher-listen (= 2.0.0) sqlite3 (= 1.3.11) turbolinks (= 5.0.1) tzinfo-data uglifier (= 3.0.0) web-console (= 3.1.1) will_paginate (= 3.1.0) BUNDLED WITH 1.12.1
###追記その5
下記、参考に「devise.rb」のパスワードを直してみましたが効果なしでした。。
http://abeyuusuke1978.hatenablog.com/entry/2015/01/03/235255
devise.rb
Ruby
1Devise.setup do |config| 2 config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com' 3 require 'devise/orm/active_record' 4 config.case_insensitive_keys = [:email] 5 config.strip_whitespace_keys = [:email] 6 config.skip_session_storage = [:http_auth] 7 config.stretches = Rails.env.test? ? 1 : 11 8 config.reconfirmable = true 9 config.expire_all_remember_me_on_sign_out = true 10 config.password_length = 4..30 11 config.email_regexp = /\A[^@\s]+@[^@\s]+\z/ 12 config.reset_password_within = 6.hours 13 config.sign_out_via = [:get, :delete] 14 config.secret_key = 'XXXXXXXXXXXXX' 15 require "omniauth-facebook" 16 config.omniauth :facebook, ENV['APP_ID'], ENV['APP_SECRET'],token_params: { parse: :json } 17end
なんでなんだ・・・・涙
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/23 05:53
2017/05/23 09:18 編集
2017/05/23 10:11
2017/05/23 10:12
2017/05/23 11:46 編集
2017/05/23 12:43
2017/05/23 13:18
2017/05/23 14:03 編集
2017/05/23 14:57
2017/05/23 15:30
2017/05/23 16:30
2017/05/24 02:41
2017/05/24 02:43
2017/05/24 23:35
2017/05/26 04:43