我有一个 Rails 应用程序,用户使用 Facebook 登录。我想将他们的一些 Facebook 数据保存到我的数据库中,但我似乎无法访问和保存我请求的额外信息。例如,我想获取 Facebook 用户的“关于我”信息,您可以获得从 Facebook Graph API 访问user_about_me
并使用 调用的权限bio
。
我收到此错误:
undefined method 'raw_info' for nil:NilClass
这是必要的代码:
全域认证.rb
OmniAuth.config.logger = Rails.logger
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_SECRET'], scope: "email,publish_stream,user_about_me"
end
用户.rb
class User < ActiveRecord::Base
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.username = auth.info.nickname
user.image = auth.info.image
user.email = auth.info.email
user.bio = auth.info.extra.raw_info.bio
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
def facebook
@facebook ||= Koala::Facebook::API.new(oauth_token)
block_given? ? yield(@facebook) : @facebook
rescue Koala::Facebook::APIError
logger.info e.to_s
nil
end
end
session_controller.rb
class SessionsController < ApplicationController
def create
user = User.from_omniauth(env["omniauth.auth"])
session[:user_id] = user.id
redirect_to feed_path
end
def destroy
session[:user_id] = nil
redirect_to root_url
end
end
为什么我无法访问该信息?