我使用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 登录链接时,它会返回设计符号“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
设计.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
有人可以帮忙吗?