Skip to content

Instantly share code, notes, and snippets.

@vaibhavpandeyvpz
Created April 19, 2020 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaibhavpandeyvpz/27faf5a5fca35fb6bf798eb7f8f8c05b to your computer and use it in GitHub Desktop.
Save vaibhavpandeyvpz/27faf5a5fca35fb6bf798eb7f8f8c05b to your computer and use it in GitHub Desktop.
Use Facebook's Graph API with Python.
import facebook
import os
import requests
APP_ID = os.getenv('FACEBOOK_APP_ID')
APP_SECRET = os.getenv('FACEBOOK_APP_SECRET')
LLT_ENDPOINT = 'https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=%s&client_secret' \
'=%s&fb_exchange_token=%s '
def get_long_lived_token(token):
response = requests.get(LLT_ENDPOINT % (APP_ID, APP_SECRET, token))
token = response.json()
return token['access_token']
def run_app():
slt = ''
while len(slt) == 0:
slt = input('Please copy-paste the token from Graph Explorer:')
slt = slt.strip()
llt = get_long_lived_token(slt)
api = facebook.GraphAPI(access_token=llt)
me = api.get_object('me')
print(me)
if __name__ == '__main__':
run_app()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment