Created
August 10, 2016 18:02
-
-
Save kezadias/a4509cd84813530496d4bc0a267b8bed to your computer and use it in GitHub Desktop.
A script that turns a short-lived access token into a one that never expires.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import requests | |
import json | |
def main(): | |
app_id = raw_input('App id: ') | |
app_secret = raw_input('App secret: ') | |
short_lived_access_token = raw_input('Short lived access token (obtained by using Graph API): ') | |
long_lived_access_token = extend_short_lived_access_token(app_id, app_secret, short_lived_access_token) | |
user_id = find_user_id(long_lived_access_token) | |
never_expiring_access_token_data = get_never_expiring_access_token_data(user_id, long_lived_access_token) | |
print get_pages(never_expiring_access_token_data) | |
user_page_name = raw_input("What page do you want the access token to be from? Above are the options. ") | |
never_expiring_access_token = find_page_access_token(never_expiring_access_token_data, user_page_name) | |
print "Congratulations! Here is your never-expiring access token for the page '%s': %s" % (user_page_name, never_expiring_access_token) | |
def extend_short_lived_access_token(app_id, app_secret, access_token): | |
extend_url = "https://graph.facebook.com/v2.7/oauth/access_token?grant_type=fb_exchange_token&client_id=%s&client_secret=%s&fb_exchange_token=%s" % (app_id, app_secret, access_token) | |
extended_json = retrieve_json(extend_url) | |
return extended_json['access_token'] | |
def find_user_id(access_token): | |
user_information_url = "https://graph.facebook.com/v2.7/me?access_token=%s" % access_token | |
user_information_json = retrieve_json(user_information_url) | |
return user_information_json['id'] | |
def get_pages(access_token_data): | |
page_names = "" | |
for page in access_token_data: | |
page_name = page['name'] | |
page_names += "%s || " % page_name | |
return page_names.encode("utf-8") | |
def get_never_expiring_access_token_data(user_id, access_token): | |
never_expiring_access_token_url = "https://graph.facebook.com/v2.7/%s/accounts?access_token=%s" % (user_id, access_token) | |
never_expiring_access_token_json = retrieve_json(never_expiring_access_token_url) | |
return never_expiring_access_token_json['data'] | |
def find_page_access_token(access_token_data, page_name): | |
for page in access_token_data: | |
if page['name'] == page_name: | |
return page['access_token'] | |
def retrieve_json(url): | |
request = requests.get(url) | |
request_content = request.text | |
request_json = json.loads(request_content) | |
return request_json | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment