I'm using Koala
gem on on Ruby on Rails application
And I have the following code on the model I'm using to the the data through Koala:
@graph = Koala::Facebook::GraphAPI.new(token_secret)
friends = @graph.get_connections("me", "friends")
where token_secret
comes from a field of my users
table, saved on the login.
It works fine but after a couple minutes I get:
Koala::Facebook::APIError (OAuthException: Error validating access token: Session has expired at unix time 1327438800. The current unix time is 1327442037.):
I found the way to renew this token in the front with the methods from the Facebook JS SDK
but this method where I'm getting the list of friends is called on the controller.
How can I renew the token_secret
using Koala? is this possible?
All access tokens need to be renewed every 90 days with the consent of the person using your app. This means that every 90 days you must send a person through the Facebook Login process, and the person must agree to specific data permissions by tapping the “continue” button.
When your app uses Facebook Login to authenticate someone, it receives a User access token. If your app uses one of the Facebook SDKs, this token lasts for about 60 days. However, the SDKs automatically refresh the token whenever the person uses your app, so the tokens expire 60 days after last use.
Facebook does not provide a refresh token. Facebook provides two kinds of access tokens, Short lived access token: A token that is expired after a short period of time (about 2 hours). Short lived access tokens are usually used on web clients.
I thought I'd answer this because it's something I just came across the need to do.
Koala added support for exchanging access tokens some time ago, here: https://github.com/arsduo/koala/pull/166
So my User model now has something like the following:
def refresh_facebook_token
# Checks the saved expiry time against the current time
if facebook_token_expired?
# Get the new token
new_token = facebook_oauth.exchange_access_token_info(token_secret)
# Save the new token and its expiry over the old one
self.token_secret = new_token['access_token']
self.token_expiry = new_token['expires']
save
end
end
# Connect to Facebook via Koala's oauth
def facebook_oauth
# Insert your own Facebook client ID and secret here
@facebook_oauth ||= Koala::Facebook::OAuth.new(client_id, client_secret)
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With