我使用https://github.com/plataformatec/devise/wiki/OmniAuth%3a-Overview整合了omniauth-facebook。但我得到的错误是:
Could not authenticate you from Facebook because "Invalid credentials".
我已经安装好了。当我点击Facebook登录链接时,它会将我重定向到facebook登录。当我输入我的详细信息时,它会回来设计符号并给出上述错误。我在https://github.com/plataformatec/devise/wiki/OmniAuth%3a-Overview上检查了“无效凭据”的解决方案,我的应用是针对App Type = Web设置的标头。不明白为什么它不起作用。
此外,我的应用程序已上线,但尚未批准从Facebook。但我不认为这与此错误有关。以下是我为omniauth-facebook所做的事情:
Gemfile包含:
gem 'omniauth'
gem 'omniauth-facebook', '1.4.0'
在用户模型中,添加:
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"
facebook sign_in的链接:
<%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>
route.rb:
devise_for:user,:controllers =&gt; {:omniauth_callbacks =&gt; “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
有人可以帮忙吗?
答案 0 :(得分:1)
尝试清除浏览器Cookie。有时omniauth不会因为您必须删除浏览器cookie而无效。
另一件事可能是版本不对。
问题似乎是依赖关系。它需要1.4.0 omniauth-oauth2 1.0.3,并且在1.4.1它需要omniauth-oauth 1.1.x