0

Estoy haciendo una conexion a facebook con ruby on rails y omniauth-facebook, pero no me está validando las credenciales que le paso. Estaría agradecido si pudieran ayudar me con este error que estoy teniendo, este es el código:

creo un modelo llamado User:

rails g model User provider uid name oauth_token oauth_expires_at:datetime

luego creo un método que interactúe con el omniauth:

  def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.name = auth.info.name
      user.oauth_token = auth.credentials.token
      user.oauth_expires_at = Time.at(auth.credentials.expires_at)
      user.save!
    end
  end

luego en el ApplicationController le doy acceso al usuario:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  private
  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end
  helper_method :current_user
end

luego creo una controlador de sesion:

class SessionsController < ApplicationController
  def create
    user = User.from_omniauth(env["omniauth.auth"])
    session[:user_id] = user.id
    redirect_to root_url
  end

  def destroy
    session[:user_id] = nil
    redirect_to root_url
  end
end

y le agrego las rutas :

match 'auth/:provider/callback', to: 'sessions#create', via: [:get, :post]
match 'auth/failure', to: redirect('/'), via: [:get, :post]
match 'signout', to: 'sessions#destroy', as: 'signout', via: [:get, :post]

seguido creo un Omniauth initializer :

OmniAuth.config.logger = Rails.logger

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, '160603117xxxxxx', 'xxxxxxxxxxxx'
end

seguido agrego facebook.js.coffee :

jQuery ->
  $('body').prepend('<div id="fb-root"></div>')

  $.ajax
    url: "#{window.location.protocol}//connect.facebook.net/en_US/all.js"
    dataType: 'script'
    cache: true


window.fbAsyncInit = ->
  FB.init(appId: '160603117xxxxxxx', cookie: true)

  $('#sign_in').click (e) ->
    e.preventDefault()
    FB.login (response) ->
      window.location = '/auth/facebook/callback' if response.authResponse

  $('#sign_out').click (e) ->
    FB.getLoginStatus (response) ->
      FB.logout() if response.authResponse
    true

al final agrego los link a la vista:

<% if current_user %>
    Welcome <strong><%= current_user.name %></strong>!
    <%= link_to "Sign out", signout_path, id: "sign_out" %>
  <% else %>
    <%= link_to "Sign in with Facebook", "/auth/facebook", id: "sign_in" %>
  <% end %>

pero cuando hago el click al botón de autentificar me manda este error en consola :

(facebook) Authentication failure! invalid_credentials: OAuth2::Error, {"access_token":"EAACSEVPJ0okBABW9mLSozOYLcsol9QINB8bwd1fiXeDdmPFzZA91Q2DDUzt9l9DtO775zhJmZBMbcAn8KyThM6GhfjBA9l17upZCic27NZCizudDZAi9PfSTfzawEKlzcUAA9s5w3CVA1xx4ZArvhUceZCqspQ56L0OZArubjmyIhgZDZD","token_type":"bearer","expires_in":5177375}

y en la vista del navegador me envia esto:

introducir la descripción de la imagen aquí

¿Alguna idea por qué no esta validando el usuario?

2
  • Considerando el tiempo desde la publicación de la pregunta ¿Lograste hacerlo funcionar?
    – fredyfx
    el 20 nov. 2017 a las 17:22
  • desafortunadamente solo pude hacer la conexion por medio del plugin proporcionado por facebook, lo siento no tener una solucion precisa el 21 nov. 2017 a las 18:17

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.