How to renew/prolong Facebook access token with Koala Gem from SERVER (!) with NO users interaction at all

1.5k views Asked by At

My situation and what i have:

  1. Rails app (last version of Rails)
  2. Koala gem (last version)
  3. 1 (one) Facebook user (for example - it's me), and that is all, no more users (!)
  4. Page on a website without any FB login forms/similar stuff

Now situation:

I am creating page at one website, my code should pull last posts from my Facebook account and display it on that page.

What i have already done:

  1. I added Koala gem
  2. Then i created app under my developer account at FB and grant to that app full access to my profile (it the same profile - developer and from i want to pull posts)
  3. Then i created app access token from here (https://developers.facebook.com/tools/accesstoken) - when i debugged it, FB told me, that THIS token is a short-live token, that expire in 1 hour.
  4. Then i created that code in my model:

    oauth = Koala::Facebook::OAuth.new(APP_ID, APP_SECRET)
    new_access_info = oauth.exchange_access_token_info self.get_token <- here i pull token from DB
    
    new_access_token = new_access_info["access_token"]
    new_access_expires_at = DateTime.now + new_access_info["expires"].to_i.seconds
    
    self.update_all(:token => new_access_token) ***<- saving token in db***
    self.update_all(:exp => new_access_expires_at) ***<- saving expiration of token in db***
    
    @facebook ||= Koala::Facebook::API.new(self.get_token)
    

So, first time i put my short-live token directly in db, so my code can pull it from there, then, when i reload page, i got NEW, long-live token (it have expiration date 12/01/2015).

BUT! When each time i reload my page, i get NEW token but with SAME expiration date (FB's debugger says me the same, when i copied token from my page and paste it in debugger).

I even tried to do this in different days, because i read "somewhere" that token renovation period is one time per day, but:

Token ALWAYS different, but expiration date always the same.

What i need:

To renew that token or write the code so that the token is updated automatically without actions from my side, because there is NO login windows or something similar. It should work ONLY from serverside.

Any thoughts? Already read i LOT of info and similar questions, but most questions is related with user interactions, like logins in appropriate forms, etc, so - no luck :/

1

There are 1 answers

1
Mike P. On

You can try this method.

@oauth = Koala::Facebook::OAuth.new(ENV['fb_app_id'], ENV['fb_app_secret'])
oauth_access_token = @oauth.get_app_access_token

@graph = Koala::Facebook::API.new(oauth_access_token)