import requests from datetime import datetime # Your App credentials (get these from Facebook Developer Dashboard) APP_ID = '1859143184977991' # Your App ID from the token info APP_SECRET = '9460c2417c450ceb1fdfc20e1bde90c1' # You need to get this from your Facebook App settings # Your current short-lived token SHORT_LIVED_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')}") return data except Exception as e: print(f"Error verifying {token_name}: {e}") return None def exchange_for_long_lived_token(app_id, app_secret, short_lived_token): """ Exchange a short-lived User Access Token for a long-lived one (60 days). """ url = 'https://graph.facebook.com/oauth/access_token' params = { 'grant_type': 'fb_exchange_token', 'client_id': app_id, 'client_secret': app_secret, 'fb_exchange_token': short_lived_token } try: response = requests.get(url, params=params) response.raise_for_status() data = response.json() if 'access_token' in data: long_lived_token = data['access_token'] print("\n✓ SUCCESS! Long-lived token obtained!\n") print("=" * 80) print("LONG-LIVED USER ACCESS TOKEN (Valid for ~60 days):") print("=" * 80) print(long_lived_token) print("=" * 80) return long_lived_token else: print(f"❌ Error: {data}") return None except requests.exceptions.RequestException as e: print(f"❌ Error exchanging token: {e}") if hasattr(e, 'response') and e.response is not None: print(f"Response: {e.response.text}") return None def get_never_expiring_page_token(long_lived_user_token, page_id): """ Get a never-expiring Page Access Token using the long-lived user token. """ url = f'https://graph.facebook.com/{page_id}' params = { 'fields': 'access_token', 'access_token': long_lived_user_token } try: response = requests.get(url, params=params) response.raise_for_status() data = response.json() if 'access_token' in data: page_token = data['access_token'] print("\n✓ SUCCESS! Never-expiring Page Access Token obtained!\n") print("=" * 80) print("PAGE ACCESS TOKEN (Never expires!):") print("=" * 80) print(page_token) print("=" * 80) print("\n📝 Copy this token to your fb_post.py file!") return page_token else: print(f"❌ Error: {data}") return None except requests.exceptions.RequestException as e: print(f"❌ Error getting page token: {e}") if hasattr(e, 'response') and e.response is not None: print(f"Response: {e.response.text}") return None if __name__ == "__main__": print("=" * 80) print("FACEBOOK LONG-LIVED TOKEN GENERATOR") print("=" * 80) # Check if APP_SECRET is set if APP_SECRET == 'YOUR_APP_SECRET_HERE': print("\n⚠️ SETUP REQUIRED!\n") print("To get your App Secret:") print("1. Go to https://developers.facebook.com/apps/") print(f"2. Click on your app (ID: {APP_ID})") print("3. Go to 'Settings' → 'Basic'") print("4. Click 'Show' next to 'App Secret'") print("5. Copy the App Secret and paste it in this script") print("\nThen run this script again.") exit(1) print("\n📋 PROCESS OVERVIEW:") print("1. Verify your current short-lived token") print("2. Exchange it for a long-lived User token (~60 days)") print("3. Use that to get a never-expiring Page token") # Step 1: Verify current token print("\n" + "=" * 80) print("STEP 1: Verifying Current Token") print("=" * 80) verify_token(SHORT_LIVED_TOKEN, "Current Short-lived Token") # Step 2: Exchange for long-lived user token print("\n" + "=" * 80) print("STEP 2: Exchanging for Long-lived User Token") print("=" * 80) long_lived_token = exchange_for_long_lived_token(APP_ID, APP_SECRET, SHORT_LIVED_TOKEN) if long_lived_token: # Verify the new long-lived token verify_token(long_lived_token, "New Long-lived Token") # Step 3: Get never-expiring page token print("\n" + "=" * 80) print("STEP 3: Getting Never-expiring Page Access Token") print("=" * 80) page_id = '419648507899848' # Your page ID page_token = get_never_expiring_page_token(long_lived_token, page_id) if page_token: # Verify the page token verify_token(page_token, "Page Access Token") print("\n" + "=" * 80) print("✅ ALL DONE!") print("=" * 80) print("\n📝 NEXT STEPS:") print("1. Copy the Page Access Token above") print("2. Replace ACCESS_TOKEN in fb_post.py with this token") print("3. This token will never expire (as long as you remain page admin)!") else: print("\n❌ Failed to get long-lived token. Please check your App Secret.")