簡體   English   中英

使用omniauth的Facebook登錄無法正常工作

[英]Facebook login with omniauth not working

我是新手,嘗試使用omniauth&2和omniauth-facebook將facebook登錄名添加到我的Rails應用程序中。 我按照https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview上的說明進行操作。

但這現在向我顯示了以下錯誤,我已經在網上搜索並嘗試了許多不同的方法而沒有取得太大的成功,我不知道自己在做錯什么,因此有人可以幫助我嗎? 非常感激。

繼續

rails(3.2.12)使用oauth2(0.8.1)使用omniauth(1.1.4)使用omniauth-oauth2(1.1.1)使用omniauth-facebook(1.4.1)

錯誤:UsersController#show中的ActiveRecord :: RecordNotFound找不到ID = sign_up的用戶

我的密碼

 class User < ActiveRecord::Base
 # Include default devise modules. Others available are:
 # :token_authenticatable, :confirmable,
 # :lockable, :timeoutable and :omniauthable
   devise :database_authenticatable, :registerable, :recoverable, :rememberable,                  :trackable, :validatable, :omniauthable, :omniauth_providers => [:facebook]

 # Setup accessible (or protected) attributes for your model
 attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :provider, :uid, :name
 attr_accessible :title, :date_of_birth, :firstName, :lastName, :mailId, :phone, :provider, :uid

protected

 def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
user = User.find(: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  

def self.new_with_session(params, session)
super.tap do |user|
  if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
    user.email = data["email"] if user.email.blank?
  end
 end
end

 has_many :pins, :dependent => :destroy

結束

devise.rb

 require 'devise/orm/active_record'
 require 'omniauth-facebook'

 config.omniauth_path_prefix = "/users/auth"
 config.omniauth :facebook,  "XXXX", "XXXX", {:client_options => { :ssl => { :verify => false } }}
 end

routes.rb

Dine::Application.routes.draw do
 get "home/index"


 resources :pins
 resources :pin 
 resources :users

match '/auth/facebook' => 'omniauth#passthru'

devise_scope :user do
get '/users/auth/:provider' => 'users/omniauth_callbacks#passthru'

devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } , :strategy_class => OmniAuth::Strategies::Facebook
match 'users/:id' => 'users#show', as: :user

root :to => 'pins#index'
get 'about' => 'pages#about'
get 'weekly' => 'pages#weekly'
get 'shop' => 'pages#shop'
get 'service' => 'pages#service'
get 'privacy' => 'pages#privacy'
get 'test' => 'pages#test'
get 'recipies' => 'pages@recipies'  

match 'contact' => 'contact#new', :as => 'contact', :via => :get
match 'contact' => 'contact#create', :as => 'contact', :via => :post
end
end

OmniauthCallbacksController

 class OmniauthCallbacksController < Devise::OmniauthCallbacksController

def passthru
send(params[:provider]) if providers.include?(params[:provider])
end

protected

def facebook
raise "Implement me for facebook"
end

def twitter
raise "Implement me for twitter"
end

private

def providers
["facebook", "twitter"]
end
end

UsersController

class UsersController < ApplicationController
 def show
 @user = User.find(params[:id])
 @pins = @user.pins.page(params[:page]).per_page(20)
 end
end

new.html.erb

<h2>登入</h2>

<%= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div class="form-inputs">
<%= f.input :email, :required => false, :autofocus => true %>
<%= f.input :password, :required => false %>
<%= f.input :remember_me, :as => :boolean if devise_mapping.rememberable? %>
</div>

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

<div class="form-actions">
<%= f.button :submit, "Sign in" %>
 </div>
<% end %>

<%= render "devise/shared/links" %>
  1. 從終端:

    捆綁程式執行耙路徑>> r.txt

  2. 打開r.txt並查找包含users#show的行,記下該行的第一項,該行應類似於user_show(我們將其the_route )。

  3. 在路徑中添加_path (例如the_route_path ),然后在整個項目中搜索該字符串。

  4. 在項目的某個位置(最有可能在視圖中),將存在link_toredirect_to the_route_path ,我想您將在某個位置將'sign_up'作為:id參數傳遞。

  5. 那就是錯誤所在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM