Created
February 2, 2024 15:20
-
-
Save fearocanity/21af8bd20c391d20b84127dc93450a93 to your computer and use it in GitHub Desktop.
Python CLI that generates No Expiration Token on Facebook API
This file contains 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
#!/usr/bin/env python3 | |
""" | |
Script that Generates Never Expiring Token | |
Authored by: Fearocanity | |
Credit: https://stackoverflow.com/a/28418469 | |
""" | |
import os | |
import json | |
import requests | |
import signal | |
creds_file_path = os.path.expanduser('~/.fb_creds') | |
def handle_interrupt(signum, frame): | |
print("\nScript interrupted. Exiting...") | |
exit(1) | |
def get_input(prompt): | |
try: | |
value = input(prompt).strip() | |
if not value: | |
raise ValueError("Input cannot be empty.") | |
return value | |
except KeyboardInterrupt: | |
handle_interrupt(signal.SIGINT, None) | |
def get_access_token(client_id, client_secret, user_token): | |
params = { | |
'grant_type': 'fb_exchange_token', | |
'client_id': client_id, | |
'client_secret': client_secret, | |
'fb_exchange_token': user_token | |
} | |
try: | |
response = requests.get("https://graph.facebook.com/v2.10/oauth/access_token", params=params).json() | |
return response.get('access_token', '') | |
except (requests.RequestException, ValueError) as e: | |
print(f"Error: {e}") | |
exit(1) | |
def save_credentials(creds_file_path, data): | |
try: | |
with open(creds_file_path, 'a') as creds_file: | |
for page_data in data.get('data', []): | |
creds_file.write(f"\n\nPage: {page_data.get('name', '')}\nNon-Expiring Token: {page_data.get('access_token', '')}\n\n") | |
print(f"Saved on {os.path.realpath(creds_file_path)}") | |
except IOError as e: | |
print(f"Error: {e}") | |
exit(1) | |
def main(): | |
signal.signal(signal.SIGINT, handle_interrupt) | |
try: | |
client_id = get_input("The App ID of Application of Facebook Developers\nClient ID: ") | |
client_secret = get_input("Get the client_secret from [dashboard > settings > basic > app secret]\nClient Secret: ") | |
user_token = get_input("Get the User Token (not the page token) from Graph API Explorer\nUser Short-Lived token: ") | |
except ValueError as e: | |
print(f"Error: {e}") | |
exit(1) | |
if not all((client_id, client_secret, user_token)): | |
print("Something went wrong: Required credentials are missing.") | |
exit(1) | |
ll_tok = get_access_token(client_id, client_secret, user_token) | |
response = requests.get(f"https://graph.facebook.com/v2.10/me?access_token={ll_tok}").json() | |
usr_id = response.get('id', '') | |
if not usr_id: | |
print("Something went wrong while retrieving user ID.") | |
exit(1) | |
raw_c = requests.get(f"https://graph.facebook.com/v2.10/{usr_id}/accounts?access_token={ll_tok}").json() | |
save_credentials(creds_file_path, raw_c) | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment