Search code examples
ruby-on-railsfacebookfacebook-graph-apidevisekoala-gem

Rails, Koala: facebook access token expires very soon, how to make it longer


Im new with all this about Koala and Facebook, Im having a problem with facebook access token.

I have to generate a new access token for my app avery 60 minutes, because after that time I get the error that my access token has expired.

I user Devise + Omniauth-facebook + Koala for make my integrations.

I have a facebook.yml file where I have the app_id, secret and my access_token.

for generate the access token I folow the "Koala's read me" that says that I have to go to the Graph API Explorer and generate the access_token for my app, well I click on "Get access token" button then select the permissions I want and then click generate. then I copy and paste the access token to my facebook.yml file so I can make requests like

@graph = Koala::Facebook::API.new(FACEBOOK_CONFIG['access_token'])
        @profile = @graph.get_object("me")
        @friends = @graph.get_connections("me", "friends?fields=id,name,picture.type(normal)")

The problem that Im facing is that the access token I generate at Graph API Explorer always is 60 minutes available, after that time I have to do the process again and generate another a new token for make everything work again.

Can someone tell me how can I generate an access token longer? not only 60 minutes...

Thanks in advance!


Solution

  • Get your app_id, your secret and your current access token, you can see the current access token going to https://developers.facebook.com/tools/access_token

    and copy the current access token of the app you want to extend it

    then compose this url

    https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=APP_ID&client_secret=APP_SECRET&fb_exchange_token=CURRENT_ACCESS_TOKEN
    

    now replace the capitalized words with your values, then copy and paste the url in the browser, and whoala! you have an access token that will be available during 2 months

    by the way, you will get something like this access_token=ACCESS_TOKEN&expires=5182578

    only copy the ACCESS_TOKEN in your application, do not copy the '&expires' part.

    For check that everything is ok go to the Debugger and paste your access token, it should show you that will expire in 2 months, if you still see 60 minutes check the steps again.

    For some weird reason (we still do not know why), our access token remains active after the 2 months, it seems that if your app is active it gets renewed automatically.