https://github.com/plataformatec/devise/wiki/OmniAuth%3a-Overviewを使用して omniauth-facebook を統合しました。しかし、次のエラーが発生しています:
Could not authenticate you from Facebook because "Invalid credentials".
そしてログで、これを取得します:
Authentication failure! invalid_credentials: OAuth2::Error, : {"error":{"message":"This authorization code has been used.","type":"OAuthException","code":100}}
デバイスをインストールしました。Facebookのサインインリンクをクリックすると、devise sign "www.mealnut.com/user/sign_in# = " に戻り、上記のエラーが発生します。https://github.com/plataformatec/devise/wiki/OmniAuth%3a-Overviewで「無効な資格情報」の解決策を確認しましたが、そこに記載されているように、私のアプリは App Type = Web に設定されたヘッダーです。なぜそれが機能していないのかわかりません。
また、私のアプリはFacebookからの審査待ちです。しかし、私はそれがこのエラーに関連しているとは思わない. 以下は、私が omniauth-facebook に対して行ったことです。
Gemfile には以下が含まれます。
gem "omniauth", "~> 1.1.4"
gem 'omniauth-facebook', '1.4.1'
ユーザー モデルに、以下を追加しました。
devise :omniauthable, :omniauth_providers => [:facebook]
attr_accessible :provider, :uid
def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
user = User.where(:provider => auth.provider, :uid => auth.uid).first
unless user
user = User.create(name:auth.extra.raw_info.name,
provider:auth.provider,
uid:auth.uid,
email:auth.info.email,
password:Devise.friendly_token[0,20]
)
end
user
end
devise.rb
require "omniauth-facebook"
config.omniauth :facebook, "APP_ID", "APP_SECRET", :scope => "offline_access, email"
omniauth.rb:
OmniAuth.config.logger = Rails.logger
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'], {:provider_ignores_state => true}
end
ルート.rb:
devise_for :user, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }
Omniauth コントローラー:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
@user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
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
誰でもこれを手伝ってもらえますか?