こんにちは。
現在Webサービスを作っており、Twitterログインを実装したいのですが、以下のようなエラーが出てしまいます。
エラーが出るといっても、Railsでよく見る赤い画面ではなく、
Could not authenticate you from Twitter because "Invalid credentials".
と書かれているのみです。Inavalid credentialsとは何か分からない上。
エラーで検索すると、似たような質問などが見受けられ、gemのバージョンを下げるなどがありました。
しかし、それらは数年前の投稿でバージョンが上げても対応できるものが良いと思っています。
参考にしているのは、Qiitaの以下の記事です。
https://qiita.com/cigalecigales/items/f4274088f20832252374
omniauth_callbacks_controller.rb
Ruby
1class OmniauthCallbacksController < Devise::OmniauthCallbacksController 2 3 def twitter 4 callback_from(:twitter) 5 end 6 7 private 8 def callback_from(provider) 9 provider = provider.to_s 10 @user = User.find_for_oauth(request.env['omniauth.auth']) 11 if @user.persisted? 12 flash[:notice] = I18n.t('devise.omniauth_callbacks.success', kind: provider.capitalize) 13 sign_in_and_redirect @user, event: :authentication 14 else 15 session["devise.#{provider}_data"] = request.env['omniauth.auth'] 16 redirect_to new_user_registration_url 17 end 18 end 19end
User.rb
Ruby
1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 5 devise :database_authenticatable, :registerable, 6 :recoverable, :rememberable, :validatable, 7 :timeoutable, :omniauthable, omniauth_providers: [:twitter] 8 9 def self.from_omniauth(auth) 10 find_or_create_by(provider: auth["provider"], uid: auth["uid"]) do |user| 11 user.provider = auth["provider"] 12 user.uid = auth["uid"] 13 user.username = auth["info"]["nickname"] 14 end 15 end 16 17 def self.new_with_session(params, session) 18 if session["devise.user_attributes"] 19 new(session["devise.user_attributes"]) do |user| 20 user.attributes = params 21 end 22 else 23 super 24 end 25 end 26end
/app/views/devise/shared/_links.html.erb
erb
1<%- if controller_name != 'sessions' %> 2 <%= link_to "Log in", new_session_path(resource_name) %><br /> 3<% end -%> 4 5<%- if devise_mapping.registerable? && controller_name != 'registrations' %> 6 <%= link_to "Sign up", new_registration_path(resource_name) %><br /> 7<% end -%> 8 9<%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %> 10 <%= link_to "Forgot your password?", new_password_path(resource_name) %><br /> 11<% end -%> 12 13<%- if devise_mapping.confirmable? && controller_name != 'confirmations' %> 14 <%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br /> 15<% end -%> 16 17<%- if devise_mapping.lockable? && resource_class.unlock_strategy_enabled?(:email) && controller_name != 'unlocks' %> 18 <%= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name) %><br /> 19<% end -%> 20 21<%- if devise_mapping.omniauthable? %> 22 <%- resource_class.omniauth_providers.each do |provider| %> 23 <%= link_to "Sign in with #{OmniAuth::Utils.camelize(provider)}", omniauth_authorize_path(resource_name, provider) %><br /> 24 <% end -%> 25<% end -%> 26
他に原因と思われるファイルのがありましたら、提示いたします。
よろしくお願いします
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/07 04:45