Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying Facebook info of logged out user
    primarykey
    data
    text
    <p>Creating a Rails app that interacts heavily with Facebook. I want to users to be able to look at profiles of other users and see information on those users that has been pulled in from Facebook.</p> <p>I'm using the omniauth-facebook and koala gems. I followed Ryan Bates' Railscasts on Facebook authentication and Facebook Graph API to get where I'm at.</p> <p>I would prefer not to have to store a user's Facebook info in my database, but that seems to be the only way of doing it, because otherwise, when I try to view the profile page of another user who may have logged out, I get: "Koala::Facebook::AuthenticationError in Profiles#show" <code>type: OAuthException, code: 190, error_subcode: 467, message: Error validating access token: The session is invalid because the user logged out. [HTTP 400]</code>.</p> <p>Is there a way to bring in this data from Facebook, even if that user has logged out?</p> <p>Here's the necessary code:</p> <p>user.rb:</p> <pre><code>class User &lt; 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 </code></pre> <p>sessions_controller.rb</p> <pre><code>class SessionsController &lt; 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 </code></pre> <p>application_controller.rb</p> <pre><code>class ApplicationController &lt; 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 </code></pre> <p>profiles_controller.rb</p> <pre><code>class ProfilesController &lt; ApplicationController def show @user = User.find(params[:id]) end end </code></pre> <p>profiles_helper.rb:</p> <pre><code>module ProfilesHelper def facebook_profile_info @user.facebook.get_object("#{@user.uid}",fields:'gender, locale, bio, birthday, picture, relationship_status') end end </code></pre> <p>Also, if you have suggestions on better ways to accomplish these tasks than the trajectory I'm headed in, I welcome your suggestions.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload