显示已注销用户的Facebook信息

时间:2013-09-22 18:02:32

标签: ruby-on-rails facebook facebook-graph-api

创建与Facebook大量交互的Rails应用程序。我希望用户能够查看其他用户的个人资料,并查看从Facebook上提取的用户信息。

我正在使用omniauth-facebook和考拉宝石。我跟随Ryan Bates在Facebook身份验证和Facebook Graph API上的Railscasts来到我所在的位置。

我不想在我的数据库中存储用户的Facebook信息,但这似乎是唯一的方法,因为否则,当我尝试查看可能已注销的其他用户的个人资料页面时,我得到:“Koala :: Facebook :: AuthenticationError in Profiles #show” type: OAuthException, code: 190, error_subcode: 467, message: Error validating access token: The session is invalid because the user logged out. [HTTP 400]

有没有办法从Facebook引入这些数据,即使该用户已退出?

这是必要的代码:

user.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.image = auth.info.image
      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

sessions_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

application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery

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

profiles_controller.rb

class ProfilesController < ApplicationController
  def show
    @user = User.find(params[:id])
  end
end

profiles_helper.rb:

module ProfilesHelper
  def facebook_profile_info
    @user.facebook.get_object("#{@user.uid}",fields:'gender, locale, bio, birthday,
               picture, relationship_status')   
  end
end

此外,如果您有关于更好地完成这些任务的建议而不是我前往的轨迹,我欢迎您的建议。

0 个答案:

没有答案