How to refresh Facebook access token

Mohammed Hammoud
1 min readJan 27, 2017

--

Ok, so I’ve recently or honestly really often have the problem with the access token provided by Facebook. It expires every 2 months and I always find myself retrieving a new one. So now I’ve created a simple python library which refreshes my access token.

GitHub: https://github.com/iktw/python-facebook-token

Basically what you need to do before using the library is to retrieve your App ID, App Secret, and Short lived access token (Read the README on GitHub for more information). Then you need to install the package to your enviroment with pip install facebooktoken and use the package to refresh your token.

from facebooktoken import FacebookTokenRefresher

ftr = FacebookTokenRefresher(
app_id=700797440051611,
app_secret="485c92c8220b87badbe8f6bb5cd02be7",
short_access_token="EAAJ9Xx55SZA0BAHmtrLHE3mvthKHW5mbBDXpkW6haI62UBevj8bZB1DWdoGKKtYhevbZBvtyOBHVdC7i3cFmxbO7PaUpjS2yovRO4BWPsNcmRqLzUCcAcU70dkl3WrdrqZAvG1jPWrdcnVJZANKiZCJmqf44vXfNU9kAzA9uqRM0FTzYZBk6P6QYlpQJ2LJiNQZD"
)

results = ftr.refresh()

If the results is OK then you’ll have a dictionary containing two keys — “access_token” and “expires”. Now save your new access token anywhere and use it the next time you want to refresh your token. My recommendation is that you create a cronjob that refreshes your token every day.

Happy hacking…

--

--

Mohammed Hammoud

Digital Designer, Backend and Frontend developer from Sweden with experience in usability.