2

我使用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

设计.rb

require "omniauth-facebook"
config.omniauth :facebook, "APP_ID", "APP_SECRET", :scope => "offline_access, email" 

facebook登录链接:

<%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>

路线.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

有人可以帮忙吗?

4

1 回答 1

1

尝试清除您的浏览器 cookie。有时,omniauth 无法正常工作,因为您必须删除浏览器 cookie。

另一件事可能是版本不正确。

问题似乎是依赖关系。在 1.4.0 它需要omniauth-oauth2 1.0.3,在1.4.1 它需要omniauth-oauth 1.1.x

如此处所示:https ://github.com/intridea/omniauth/issues/276

于 2013-04-23T07:19:28.387 回答