Skip to content

Instantly share code, notes, and snippets.

@aparrish
Last active February 6, 2021 22:23
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aparrish/4683917 to your computer and use it in GitHub Desktop.
Save aparrish/4683917 to your computer and use it in GitHub Desktop.
simple python script to fetch an access token for a Facebook application (not a user token)
# fetch an access token for a Facebook application
#
# run like so:
#
# $ python facebook_app_token.py --app_id=<your app id> --secret=<your secret>
#
# ... where <your app id> is your Facebook application ID, and <your secret>
# is the application secret for that application (both can be retrieved from
# the Facebook developer app)
import urllib
def fetch_app_access_token(fb_app_id, fb_app_secret):
resp = urllib.urlopen(
'https://graph.facebook.com/oauth/access_token?client_id=' +
fb_app_id + '&client_secret=' + fb_app_secret +
'&grant_type=client_credentials')
if resp.getcode() == 200:
return resp.read().split("=")[1]
else:
return None
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser()
parser.add_option('--app_id', dest='fb_app_id')
parser.add_option('--secret', dest='fb_app_secret')
(options, args) = parser.parse_args()
print fetch_app_access_token(options.fb_app_id, options.fb_app_secret)
@joaolrpaulo
Copy link

How can i get a user token using a parser like this?

@Orest02
Copy link

Orest02 commented Sep 20, 2018

gets me urllib.error.HTTPError: HTTP Error 500: Internal Server Error

@rOOtsystem2010
Copy link

so for what the script?

you can get access toking by this ??

@rudSarkar
Copy link

Seems like issue patched.

@Umair4345
Copy link

How can I get user access_token using the same technique?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment