import requests import json from datetime import datetime # Your current user access token USER_ACCESS_TOKEN = 'EAAaa4YEgkEcBPs4IxQdEMm4Q9Au6xvseEs3GXnTDOmQBcZC4NPNfev4LJvuIz9Af3rLBHN2XcAek8dZBdbcQGLCx4ZA2l4R7edjMY3m2SbQZAm3rfTwvmWWIDGl3P0j4ymoBZAZCGZB3h5lVSiqw8aGXLxz75eX26TTc3C47JPKj4sZCcL7XkF3kZAqkjTFEd935Agoy4JeYQlGDzDS0L89kOnMt8UZAZC71ge0EDd6DZAc2vAmRMAZDZD' def verify_token(access_token, token_name="Token"): """ Verify the access token and display its validity information. """ url = 'https://graph.facebook.com/debug_token' params = { 'input_token': access_token, 'access_token': access_token } try: response = requests.get(url, params=params) data = response.json() if 'data' in data: token_info = data['data'] print(f"\n=== {token_name} Validity ===") is_valid = token_info.get('is_valid', False) print(f"Is Valid: {'✓ YES' if is_valid else '✗ NO'}") if not is_valid: print("⚠️ TOKEN IS INVALID!") return data # Check expiration if 'expires_at' in token_info: expires_at = token_info['expires_at'] if expires_at == 0: print("Expiration: ♾️ Never expires (long-lived token)") else: expiry_date = datetime.fromtimestamp(expires_at) print(f"Expires At: {expiry_date.strftime('%Y-%m-%d %H:%M:%S')}") # Calculate time remaining now = datetime.now() if expiry_date > now: time_left = expiry_date - now days_left = time_left.days hours_left = time_left.seconds // 3600 print(f"Time Remaining: {days_left} days, {hours_left} hours") else: print("Status: ⚠️ TOKEN EXPIRED!") # Display data access expiration if 'data_access_expires_at' in token_info: data_expires_at = token_info['data_access_expires_at'] if data_expires_at > 0: data_expiry = datetime.fromtimestamp(data_expires_at) print(f"Data Access Expires: {data_expiry.strftime('%Y-%m-%d %H:%M:%S')}") print(f"Token Type: {token_info.get('type', 'N/A')}") print(f"App ID: {token_info.get('app_id', 'N/A')}") scopes = token_info.get('scopes', []) if scopes: print(f"Permissions: {', '.join(scopes)}") return data except Exception as e: print(f"Error verifying {token_name}: {e}") return None def get_page_access_tokens(user_token): """ Get all pages managed by the user and their page access tokens. """ url = 'https://graph.facebook.com/me/accounts' params = {'access_token': user_token} try: response = requests.get(url, params=params) response.raise_for_status() data = response.json() print("\n=== Your Facebook Pages ===\n") if 'data' in data and len(data['data']) > 0: for i, page in enumerate(data['data'], 1): print(f"Page {i}:") print(f" Name: {page.get('name')}") print(f" ID: {page.get('id')}") print(f" Access Token: {page.get('access_token')}") print(f" Category: {page.get('category')}") print() # If this is the page we're trying to post to if page.get('id') == '419648507899848': print("👉 THIS IS YOUR TARGET PAGE!") print(f" Copy this access token to fb_post.py: {page.get('access_token')}") print() else: print("No pages found or you don't have access to manage any pages.") print("\nMake sure:") print("1. You are an admin of the page") print("2. Your token has 'pages_show_list' permission") return data except requests.exceptions.RequestException as e: print(f"Error: {e}") if hasattr(e, 'response') and e.response is not None: print(f"Response: {e.response.text}") return None if __name__ == "__main__": print("=== Facebook Page Token Checker ===") # Step 1: Verify the user access token print("\nStep 1: Checking User Access Token validity...") verify_token(USER_ACCESS_TOKEN, "User Access Token") # Step 2: Get page access tokens print("\nStep 2: Fetching Page Access Tokens...") get_page_access_tokens(USER_ACCESS_TOKEN)