Rails - Facebook with Omniauth and Koala: How to Extend Expired Token

I have an application in which users can link their Facebook accounts. They can log in using their email, but they can link their Facebook account.

In the view where I show related social networks (Facebook and others), I have something like this:

<%= image_tag @facebook.get_facebook_picture %> 

This will call an instance method similar to this:

 def get_facebook_picture unless self.token.nil? facebook_graph = Koala::Facebook::GraphAPI.new(self.token) fb_picture = facebook_graph.get_picture("me", { :type => "small" }) end end 

This will work well if the Facebook token I saved in my database has expired. Therefore, I added this exception handler to the specified controller:

 def facebook_exception_handler exception if exception.fb_error_type.eql? 'OAuthException' # Let get a new auth token... How? else logger.debug "Damn it. We don't know what error is coming from FB" raise exception end end 

I understand the exception correctly, but I don’t see how to update the access token that I have in my database. Please note that the access token that I have was inserted using OmniAuth. So my question is:

Given that I have an OAuthException , how can I update a specific user access token (UID) using Omniauth ?

+8
ruby-on-rails facebook access-token omniauth koala
source share
2 answers

The simple case is that you reauthorize the user using FB, just as you first allowed them. To get the token in the first place, I assume that you are using omniauth (and onmiauth-facebook) to authenticate against FB. This means that you have a route and controller action to handle the auth callback, as well as a function that inserts a token into db.

The access token that you initially obtained with omniauth may become invalid for various reasons - after the expiration date or because the user has changed his FB password and, possibly, others. In this case, another OAuth call will return a valid token. Just call again (as it was at the first permission of the user) and replace the invalid token with a new one in your database, and you are fine.

This meaning ( my own answer to the corresponding question I asked here) has some code coverage, but it looks like you already got this. Save enough state to retry the exception that is triggered and you are kind.

It is also possible that the token is now invalid because the user changed the settings of his FB application to unauthorize your application. In this case, the user will see the FB permissions dialog box, as if they were first authenticated with FB. ( Fb )

It makes sense?

+9
source share

You can change the connection of RallyCasts koala with this:

 def facebook if self.facebook_expires_at < Time.now oauth = Koala::Facebook::OAuth.new(ENV["FACEBOOK_KEY"], ENV["FACEBOOK_SECRET"]) new_access_info = oauth.exchange_access_token_info self.facebook_token new_access_token = new_access_info["access_token"] new_access_expires_at = DateTime.now + new_access_info["expires"].to_i.seconds self.update_attributes!(:facebook_token => new_access_token, :facebook_expires_at => new_access_expires_at ) end @facebook ||= Koala::Facebook::API.new(self.facebook_token) block_given? ? yield(@facebook) : @facebook rescue Koala::Facebook::APIError => e logger.info e.to_s nil end 
+2
source share

All Articles