Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending expiry date of facebook access token in graph api beyond 2 months

I am working on facebook page wallpost automation using python

I have automated posting on a fb page that i own by using facebook graph api post So i do this by sending a HTTP POST request to https://graph.facebook.com/mypagename/feed

with access_token and message as POST paramaters

I generate the access token by using graph api explorer by selecting my app that i am using and giving it permission to manage my pages

Intitially the access_token use to expire in 2 hours

To extend the expiry date to 2 months i send HTTP GET request to this url

https://graph.facebook.com/oauth/access_token?client_id=my_app_id&client_secret=my_app_secret&grant_type=fb_exchange_token&fb_exchange_token=old_access_token

the response is a json string with new access_token and expiry time in seconds

the expiry time of new access token is now 60 days.

Now is it possible to extend expiry time beyond 60 days??

like image 407
HimalayanCoder Avatar asked Jan 17 '13 14:01

HimalayanCoder


2 Answers

You can use following api from facebook to refresh token life to 60 days and just when the token is about to expire, call the same api again with-in 60 days to refresh its life back to 60 days from that point of time Token expire is present in expires parameter and its value is in seconds

Replace CLIENT_ID and CLIENT_SECRET with their actual value

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

in ACCESS_TOKEN, put the actual token value without appending "access_token="

like image 120
Harsh Gupta Avatar answered Oct 12 '22 14:10

Harsh Gupta


Short-Term and Long-Term Tokens

User access tokens come in two forms: short-lived tokens and long-lived tokens. Short-lived tokens usually have a lifetime of about an hour or two, while long-lived tokens usually have a lifetime of about 60 days. You should not depend on these lifetimes remaining the same - the lifetime may change without warning or expire early. See more under handling errors.

So the only way left is to regenerate using code just before 60 days.

Make a GET request to graph api and filter out the access token from the JSON response

Get your user id here http://findmyfacebookid.com/

Now

Request

GET /{user-id}/accounts

Response

{
  "data": [
    {
      "category": "Product/service",
      "name": "Sample Page",
      "access_token": "{access-token}",
      "id": "1234567890",
      "perms": [
        "ADMINISTER",
        "EDIT_PROFILE",
        "CREATE_CONTENT",
        "MODERATE_CONTENT",
        "CREATE_ADS",
        "BASIC_ADMIN"
      ]
    }, 
}

https://developers.facebook.com/docs/facebook-login/access-tokens#pagetokens

like image 24
HimalayanCoder Avatar answered Oct 12 '22 14:10

HimalayanCoder