Sunjay Dhama


1,260 Experience
12 Lessons Completed
0 Questions Solved


Hey, I'm actually seeing an error occur every now and again when a user changes their Facebook password and I need to redirect to Facebook auth dialog. Would someone be able to answer my question:

Posted in How do I handle Koala error

After watching Facebook API and Auth Tokens with Koala, I decided to implement it. However, I made a mistake in my logic and went over the limit for the number of API calls. Now I am getting a Koala error

User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."provider" = $1 AND "users"."uid" = $2 ORDER BY "users"."id" ASC LIMIT $3  [["provider", "facebook"], ["uid", "#####hidden####"], ["LIMIT", 1]]
  ↳ app/models/user.rb:30
Completed 500 Internal Server Error in 166ms (ActiveRecord: 0.4ms)

Koala::Facebook::OAuthTokenRequestError (type: OAuthException, code: 190, error_subcode: 460, message: Error validating access token: The session has been invalidated because the user changed their password or Facebook has changed the session for security reasons. [HTTP 400]):

I looked through SO and found this post, which suggests adding a rescue_from:

# app/models/user.rb
def facebook_refresh_token
  # Get refreshed 60 day auth token
  new_access_info = oauth.exchange_access_token_info token
  update(token: new_access_info['access_token'], token_expires_at: + new_access_info['expires_in'].to_i.seconds)
rescue Koala::Facebook::APIError => exception
  if exception.fb_error_type == 190
    puts exception
    raise "Facebook Error: #{exception.fb_error_type}"

I am not sure if I am putting the rescue in the right place. Do I need to move it? I am also have another place where I get the longed lived access token.

# app/models/user.rb
def self.from_omniauth(auth)

     user = User.where(provider: auth.provider, uid: auth.uid).first
     # where('email = ?',
     if user.blank?

       # immediately get 60 day auth token
       new_access_info = oauth.exchange_access_token_info auth.credentials.token

       new_access_token = new_access_info["access_token"]
       # Facebook updated expired attribute
       new_access_expires_at = + new_access_info["expires_in"].to_i.seconds

       user =
       user.provider = auth.provider
       user.uid = auth.uid
         # some code remove for brevity
             # set all the user things

Obviously I want to handle the exception gracefully, but what do I put in if exception.fb_error_type == 190 ? I don't understand how I would redirect to auth dialog. Your comments, suggestions, and answers are much appreciated.

Facebook has a page explaining for how to handle this type of error, but it is only for PHP. I'm not sure how to translate that to Rails...