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

﹥>﹥吖頭↗ 提交于 2019-12-30 01:20:17

问题


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??


回答1:


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




回答2:


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="




回答3:


Your Page Access Token will not actually expire. Please read the documentation:

Extending Page Access Tokens

Apps can retrieve a page access token from Page admin users when they authenticate with the manage_pages permission. If the user access token used to retrieve this page access token is short-lived, the page access token will also be short-lived.

To get a longer-lived page access token, exchange the User access token for a long-lived one, as above, and then request the Page access token. The resulting page access token will not have any expiry time.

So if you use a long-lived user access token, you will get an infinite page access token.



来源:https://stackoverflow.com/questions/14381454/extending-expiry-date-of-facebook-access-token-in-graph-api-beyond-2-months

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!