1

nil:NilClassの未定義のメソッド`[]'を取得しています

 NoMethodError in Users::OmniauthCallbacksController#facebook

  undefined method `[]' for nil:NilClass

  Rails.root: /home/krishna/picer
  Application Trace | Framework Trace | Full Trace

  app/models/user.rb:13:in `find_for_facebook_oauth'
  app/controllers/users/omniauth_callbacks_controller.rb:4:in `facebook'

これは私のuser.rbコードです

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable:omniauthable
devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable,
     :omniauthable

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me

def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = access_token['extra']['user_hash']
if user = User.find_by_email(data["email"])
  user
else # Create a user with a stub password.
  User.create(:email => data["email"], :password => Devise.friendly_token[0,20])
  end
end

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

そして、omniauthはコントローラーコードをコールバックします

 class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
 def facebook
 # You need to implement the method below in your model
 @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)

if @user.persisted?
  flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
  sign_in_and_redirect @user, :event => :authentication
else
  session["devise.facebook_data"] = env["omniauth.auth"]
  redirect_to new_user_registration_url
 end
end

def passthru
  render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
 end

end

エラー行4のuser.rbが表示されますこのhttps://github.com/ryanatwork/devise-omniauthの例を使用しました

4

1 に答える 1

1

access_token['info']['email']の代わりにを使用してユーザーの電子メールを取得する必要がありますaccess_token['extra']['user_hash']['email']

access_token['extra']のデータは他のデータのような規則に従っていないと思うので、特に注意して、アクセスしようとしているプロパティが存在するかどうかをテストする必要があります。プロバイダーが1つしかない場合はそれほど重要ではありませんが、複数のプロバイダーがある場合は重要です。

于 2012-09-01T17:14:25.257 に答える