import requests # Replace these with your actual values ACCESS_TOKEN = 'EAAaa4YEgkEcBPuLyNFFn7UZAGTHzpPShyzelK0sXgQrSjRcYGXxmE1EPwWBrPYOT1xjgnwwMn3vQrMJ4vVJeNiZCbZBkj39AQXfNfk7tGZA7VZCrrp9hyk9ftNZC1bSkT0NjG0c3Ijfgw7pqqoeoi18jkeqkdpSxT9vVZAo9XeT5ZAuBzJmb2RXZBSDJ2myEOmEwwZBGIEVqI6ZCZBzQ1tJdtSyZCbNuxhU3oOTTRfaHFxgsZD' # Page Access Token for "Product Maker" PAGE_ID = '419648507899848' # The ID of your Facebook page MESSAGE = 'Hello, world! This is a test post from my Python script.' # The message you want to post def verify_token(access_token): """ Verify the access token and check its permissions. """ 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() print("\n=== Token Debug Info ===") print(f"Response: {data}") # Extract and display token validity information if 'data' in data: token_info = data['data'] print("\n=== Token Validity ===") print(f"Is Valid: {token_info.get('is_valid', False)}") # 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: from datetime import datetime expiry_date = datetime.fromtimestamp(expires_at) print(f"Expires At: {expiry_date}") # 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 other relevant info if 'data_access_expires_at' in token_info: data_expires_at = token_info['data_access_expires_at'] if data_expires_at > 0: from datetime import datetime data_expiry = datetime.fromtimestamp(data_expires_at) print(f"Data Access Expires At: {data_expiry}") print(f"App ID: {token_info.get('app_id', 'N/A')}") print(f"User ID: {token_info.get('user_id', 'N/A')}") print(f"Scopes: {', '.join(token_info.get('scopes', []))}") return data except Exception as e: print(f"Error verifying token: {e}") return None def get_page_info(access_token, page_id): """ Get information about the page to verify access. """ url = f'https://graph.facebook.com/{page_id}' params = { 'fields': 'id,name,access_token', 'access_token': access_token } try: response = requests.get(url, params=params) data = response.json() print("\n=== Page Info ===") print(f"Response: {data}") return data except Exception as e: print(f"Error getting page info: {e}") return None def post_to_facebook_page(access_token, page_id, message): """ Posts a message to a Facebook page using the Graph API. """ url = f'https://graph.facebook.com/{page_id}/feed' data = { 'message': message, 'access_token': access_token } try: response = requests.post(url, data=data) response.raise_for_status() # Raise an exception for bad status codes # If successful, print the post ID post_data = response.json() print(f"\n✓ Post successful! Post ID: {post_data.get('id')}") return post_data except requests.exceptions.RequestException as e: print(f"\n✗ Error posting to Facebook: {e}") if response: print(f"Response status: {response.status_code}") print(f"Response text: {response.text}") # Try to parse error details try: error_data = response.json() if 'error' in error_data: print(f"\nError details:") print(f" Type: {error_data['error'].get('type')}") print(f" Message: {error_data['error'].get('message')}") print(f" Code: {error_data['error'].get('code')}") except: pass return None if __name__ == "__main__": print("=== Facebook Page Poster ===\n") # Step 1: Verify token print("Step 1: Verifying access token...") verify_token(ACCESS_TOKEN) # Step 2: Get page info print("\nStep 2: Getting page information...") page_info = get_page_info(ACCESS_TOKEN, PAGE_ID) # If page has its own access token, use that instead page_access_token = ACCESS_TOKEN if page_info and 'access_token' in page_info: print("\n✓ Found page-specific access token, using that instead.") page_access_token = page_info['access_token'] # Step 3: Post to page print("\nStep 3: Attempting to post...") post_to_facebook_page(page_access_token, PAGE_ID, MESSAGE)