Meta Ads (Facebook/Instagram) MCP server for Claude, ChatGPT, Perplexity & Cursor — the Meta node of Pipeboard’s 5-platform family (+ Google, TikTok, Snap, Reddit). Hosted remote MCP, badged Meta Business Partner, free plan — no self-hosting required.
Python advertisingai-agentschatgptclaudefacebook-adsinstagram-adsmcpmeta-adsmodel-context-protocolpipeboard
Enhance ad set update functionality and error handling
Yves Junqueiraeac4de311c26
diff --git a/meta_ads_mcp/core/adsets.py b/meta_ads_mcp/core/adsets.pyindex 1fa37cb..ee9a087 100644--- a/meta_ads_mcp/core/adsets.py+++ b/meta_ads_mcp/core/adsets.py@@ -7,6 +7,7 @@ from .accounts import get_ad_accountsfrom .server import mcp_serverimport asynciofrom .auth import start_callback_server, update_confirmation+import urllib.parse@mcp_server.tool()@@ -77,7 +78,7 @@ async def get_adset_details(access_token: str = None, adset_id: str = None) -> s@mcp_server.tool()@meta_api_toolasync def update_adset(adset_id: str, frequency_control_specs: List[Dict[str, Any]] = None, bid_strategy: str = None,- bid_amount: int = None, status: str = None, access_token: str = None) -> str:+ bid_amount: int = None, status: str = None, targeting: Dict[str, Any] = None, access_token: str = None) -> str:"""Update an ad set with new settings including frequency caps.@@ -88,6 +89,8 @@ async def update_adset(adset_id: str, frequency_control_specs: List[Dict[str, Anbid_strategy: Bid strategy (e.g., 'LOWEST_COST_WITH_BID_CAP')bid_amount: Bid amount in account currency (in cents for USD)status: Update ad set status (ACTIVE, PAUSED, etc.)+ targeting: Targeting specifications including targeting_automation+ (e.g. {"targeting_automation":{"advantage_audience":1}})access_token: Meta API access token (optional - will use cached token if not provided)"""if not adset_id:@@ -106,6 +109,32 @@ async def update_adset(adset_id: str, frequency_control_specs: List[Dict[str, Anif status is not None:changes['status'] = status++ if targeting is not None:+ # Get current ad set details to preserve existing targeting settings+ current_details_json = await get_adset_details(adset_id=adset_id, access_token=access_token)+ current_details = json.loads(current_details_json)++ # Check if the current ad set has targeting information+ current_targeting = current_details.get('targeting', {})++ if 'targeting_automation' in targeting:+ # Only update targeting_automation while preserving other targeting settings+ if current_targeting:+ merged_targeting = current_targeting.copy()+ merged_targeting['targeting_automation'] = targeting['targeting_automation']+ changes['targeting'] = merged_targeting+ else:+ # If there's no existing targeting, we need to create a basic one+ # Meta requires at least a geo_locations setting+ basic_targeting = {+ 'targeting_automation': targeting['targeting_automation'],+ 'geo_locations': {'countries': ['US']} # Using US as default location+ }+ changes['targeting'] = basic_targeting+ else:+ # Full targeting replacement+ changes['targeting'] = targetingif not changes:return json.dumps({"error": "No update parameters provided"}, indent=2)@@ -119,7 +148,8 @@ async def update_adset(adset_id: str, frequency_control_specs: List[Dict[str, An# Generate confirmation URL with properly encoded parameterschanges_json = json.dumps(changes)- confirmation_url = f"http://localhost:{port}/confirm-update?adset_id={adset_id}&token={access_token}&changes={changes_json}"+ encoded_changes = urllib.parse.quote(changes_json)+ confirmation_url = f"http://localhost:{port}/confirm-update?adset_id={adset_id}&token={access_token}&changes={encoded_changes}"# Reset the update confirmationupdate_confirmation.clear()diff --git a/meta_ads_mcp/core/api.py b/meta_ads_mcp/core/api.pyindex 819bf72..c2843fc 100644--- a/meta_ads_mcp/core/api.py+++ b/meta_ads_mcp/core/api.py@@ -59,9 +59,9 @@ async def make_api_request(if not access_token:logger.error("API request attempted with blank access token")return {- "error": "Authentication Required",- "details": {- "message": "A valid access token is required to access the Meta API",+ "error": {+ "message": "Authentication Required",+ "details": "A valid access token is required to access the Meta API","action_required": "Please authenticate first"}}@@ -89,7 +89,18 @@ async def make_api_request(if method == "GET":response = await client.get(url, params=request_params, headers=headers, timeout=30.0)elif method == "POST":- response = await client.post(url, json=request_params, headers=headers, timeout=30.0)+ # For Meta API, POST requests need data, not JSON+ if 'targeting' in request_params and isinstance(request_params['targeting'], dict):+ # Convert targeting dict to string for the API+ request_params['targeting'] = json.dumps(request_params['targeting'])++ # Convert lists and dicts to JSON strings+ for key, value in request_params.items():+ if isinstance(value, (list, dict)):+ request_params[key] = json.dumps(value)++ logger.debug(f"POST params (prepared): {masked_params}")+ response = await client.post(url, data=request_params, headers=headers, timeout=30.0)elif method == "DELETE":response = await client.delete(url, params=request_params, headers=headers, timeout=30.0)else:@@ -97,7 +108,16 @@ async def make_api_request(response.raise_for_status()logger.debug(f"API Response status: {response.status_code}")- return response.json()++ # Ensure the response is JSON and return it as a dictionary+ try:+ return response.json()+ except json.JSONDecodeError:+ # If not JSON, return text content in a structured format+ return {+ "text_response": response.text,+ "status_code": response.status_code+ }except httpx.HTTPStatusError as e:error_info = {}@@ -123,8 +143,7 @@ async def make_api_request(logger.error(f"Current app_id: {app_id}")# Provide a clearer error message without the confusing "Provide valid app ID" messagereturn {- "error": f"HTTP Error: {e.response.status_code}",- "details": {+ "error": {"message": "Meta API authentication configuration issue. Please check your app credentials.","original_error": error_obj.get("message"),"code": error_obj.get("code")@@ -132,11 +151,28 @@ async def make_api_request(}auth_manager.invalidate_token()- return {"error": f"HTTP Error: {e.response.status_code}", "details": error_info}+ # Include full details for technical users+ full_response = {+ "headers": dict(e.response.headers),+ "status_code": e.response.status_code,+ "url": str(e.response.url),+ "reason": getattr(e.response, "reason_phrase", "Unknown reason"),+ "request_method": e.request.method,+ "request_url": str(e.request.url)+ }++ # Return a properly structured error object+ return {+ "error": {+ "message": f"HTTP Error: {e.response.status_code}",+ "details": error_info,+ "full_response": full_response+ }+ }except Exception as e:logger.error(f"Request Error: {str(e)}")- return {"error": str(e)}+ return {"error": {"message": str(e)}}# Generic wrapper for all Meta API tools@@ -174,12 +210,14 @@ def meta_api_tool(func):logger.warning("No access token available, authentication needed")auth_url = auth_manager.get_auth_url()return json.dumps({- "error": "Authentication Required",- "details": {- "message": "You need to authenticate with the Meta API before using this tool",- "action_required": "Please authenticate first",- "auth_url": auth_url,- "markdown_link": f"[Click here to authenticate with Meta Ads API]({auth_url})"+ "error": {+ "message": "Authentication Required",+ "details": {+ "description": "You need to authenticate with the Meta API before using this tool",+ "action_required": "Please authenticate first",+ "auth_url": auth_url,+ "markdown_link": f"[Click here to authenticate with Meta Ads API]({auth_url})"+ }}}, indent=2)@@ -200,18 +238,24 @@ def meta_api_tool(func):logger.error(f"Current app_id: {app_id}")# Replace the confusing error with a more user-friendly onereturn json.dumps({- "error": "Meta API Configuration Issue",- "details": {- "message": "Your Meta API app is not properly configured",- "action_required": "Check your META_APP_ID environment variable",- "current_app_id": app_id,- "original_error": error_obj.get("message")+ "error": {+ "message": "Meta API Configuration Issue",+ "details": {+ "description": "Your Meta API app is not properly configured",+ "action_required": "Check your META_APP_ID environment variable",+ "current_app_id": app_id,+ "original_error": error_obj.get("message")+ }}}, indent=2)except Exception:- # Not JSON or other parsing error, just continue- pass-+ # Not JSON or other parsing error, wrap it in a dictionary+ return json.dumps({"data": result}, indent=2)++ # If result is already a dictionary, ensure it's properly serialized+ if isinstance(result, dict):+ return json.dumps(result, indent=2)+return resultexcept Exception as e:logger.error(f"Error in {func.__name__}: {str(e)}")diff --git a/meta_ads_mcp/core/auth.py b/meta_ads_mcp/core/auth.pyindex 384f0e3..1559810 100644--- a/meta_ads_mcp/core/auth.py+++ b/meta_ads_mcp/core/auth.py@@ -444,7 +444,7 @@ class CallbackHandler(BaseHTTPRequestHandler):return Noneelif self.path.startswith("/confirm-update"):- # Parse query parameters+ # Generate confirmation URL with properly encoded parametersquery = parse_qs(urlparse(self.path).query)adset_id = query.get("adset_id", [""])[0]token = query.get("token", [""])[0]@@ -457,13 +457,14 @@ class CallbackHandler(BaseHTTPRequestHandler):# Return confirmation pageself.send_response(200)- self.send_header("Content-type", "text/html")+ self.send_header("Content-type", "text/html; charset=utf-8")self.end_headers()html = """<html><head><title>Confirm Ad Set Update</title>+ <meta charset="utf-8"><style>body { font-family: Arial, sans-serif; margin: 20px; max-width: 1000px; margin: 0 auto; }.warning { color: #d73a49; margin: 20px 0; padding: 15px; border-left: 4px solid #d73a49; background-color: #fff8f8; }@@ -486,15 +487,14 @@ class CallbackHandler(BaseHTTPRequestHandler):<p>You are about to update Ad Set: <strong>""" + adset_id + """</strong></p><div class="warning">- <strong>⚠️ Warning:</strong> These changes will be applied immediately upon approval.- """ + ("<br><strong>🔴 Important:</strong> This update includes status changes that may affect billing." if "status" in changes_dict else "") + """+ <p><strong>Warning:</strong> This action will directly update your ad set in Meta Ads. Please review the changes carefully before approving.</p></div>- <h2>Proposed Changes:</h2><div class="changes">+ <h3>Changes to apply:</h3><table class="diff-table"><tr class="header">- <td>Setting</td>+ <td>Field</td><td>New Value</td><td>Description</td></tr>@@ -508,6 +508,15 @@ class CallbackHandler(BaseHTTPRequestHandler):if all(key in spec for key in ["event", "interval_days", "max_frequency"]):description = f"Cap to {spec['max_frequency']} {spec['event'].lower()} per {spec['interval_days']} days"+ # Special handling for targeting_automation+ elif k == "targeting" and isinstance(v, dict) and "targeting_automation" in v:+ targeting_auto = v.get("targeting_automation", {})+ if "advantage_audience" in targeting_auto:+ audience_value = targeting_auto["advantage_audience"]+ description = f"Set Advantage+ audience to {'ON' if audience_value == 1 else 'OFF'}"+ if audience_value == 1:+ description += " (may be restricted for Special Ad Categories)"+# Format the value for displaydisplay_value = json.dumps(v, indent=2) if isinstance(v, (dict, list)) else str(v)@@ -519,6 +528,9 @@ class CallbackHandler(BaseHTTPRequestHandler):</tr>"""+ # Create a properly escaped JSON string for JavaScript+ escaped_changes = json.dumps(changes).replace("'", "\\'").replace('"', '\\"')+html += """</table></div>@@ -531,6 +543,18 @@ class CallbackHandler(BaseHTTPRequestHandler):<div id="status" class="status"></div><script>+ // Enable debug logging+ const DEBUG = true;+ function debugLog(message, data) {+ if (DEBUG) {+ if (data) {+ console.log(`[DEBUG-CONFIRM] ${message}:`, data);+ } else {+ console.log(`[DEBUG-CONFIRM] ${message}`);+ }+ }+ }+function showStatus(message, isError = false) {const statusElement = document.getElementById('status');statusElement.textContent = message;@@ -546,21 +570,95 @@ class CallbackHandler(BaseHTTPRequestHandler):function approveChanges() {showStatus("Processing update...");+ debugLog("Approving changes");const buttons = document.querySelectorAll('button');buttons.forEach(button => button.disabled = true);- fetch('/update-confirm?' + new URLSearchParams({+ const params = new URLSearchParams({adset_id: '""" + adset_id + """',token: '""" + token + """',- changes: '""" + changes + """',+ changes: '""" + escaped_changes + """',action: 'approve'- }))- .then(response => response.json())+ });++ debugLog("Sending update request with params", {+ adset_id: '""" + adset_id + """',+ changes: JSON.parse('""" + escaped_changes + """')+ });++ fetch('/update-confirm?' + params)+ .then(response => {+ debugLog("Received response", { status: response.status });+ return response.text().then(text => {+ debugLog("Raw response text", text);+ try {+ return JSON.parse(text);+ } catch (e) {+ debugLog("Error parsing JSON response", e);+ return { status: "error", error: "Invalid response format from server" };+ }+ });+ }).then(data => {- if (data.error) {- showStatus('Error applying changes: ' + data.error, true);- buttons.forEach(button => button.disabled = false);+ debugLog("Parsed response data", data);++ if (data.status === "error") {+ // Build a properly encoded and detailed error message+ let errorMessage = data.error || "Unknown error";++ // Include any detailed errors if available+ if (data.errorDetails && data.errorDetails.length > 0) {+ errorMessage = data.errorDetails.join("; ");+ }++ // If we have an API error object, use its information+ if (data.apiError) {+ if (data.apiError.error_user_msg) {+ errorMessage = data.apiError.error_user_msg;+ } else if (data.apiError.message) {+ errorMessage = data.apiError.message;+ }++ if (data.apiError.error_data) {+ try {+ const errorData = JSON.parse(data.apiError.error_data);+ if (errorData.blame_field_specs && errorData.blame_field_specs.length > 0) {+ if (Array.isArray(errorData.blame_field_specs[0])) {+ const specs = errorData.blame_field_specs[0].filter(Boolean);+ if (specs.length > 0) {+ errorMessage = specs.join("; ");+ }+ }+ }+ } catch (e) {+ debugLog("Error parsing error_data", e);+ }+ }+ }++ // Create a detailed error object for the verification page+ const fullErrorData = {+ message: errorMessage,+ details: data.errorDetails || [],+ apiError: data.apiError || {},+ fullResponse: data.fullResponse || {}+ };++ debugLog("Redirecting with error message", errorMessage);+ debugLog("Full error data", fullErrorData);++ // Encode the stringified error object+ const encodedErrorData = encodeURIComponent(JSON.stringify(fullErrorData));++ // Redirect to verification page with detailed error information+ const errorParams = new URLSearchParams({+ adset_id: '""" + adset_id + """',+ token: '""" + token + """',+ error: errorMessage,+ errorData: encodedErrorData+ });+ window.location.href = '/verify-update?' + errorParams;} else {showStatus('Changes approved and will be applied shortly!');setTimeout(() => {@@ -572,6 +670,7 @@ class CallbackHandler(BaseHTTPRequestHandler):}}).catch(error => {+ debugLog("Fetch error", error);showStatus('Error applying changes: ' + error, true);buttons.forEach(button => button.disabled = false);});@@ -593,7 +692,7 @@ class CallbackHandler(BaseHTTPRequestHandler):</body></html>"""- self.wfile.write(html.encode())+ self.wfile.write(html.encode('utf-8'))returnelif self.path.startswith("/verify-update"):@@ -602,15 +701,28 @@ class CallbackHandler(BaseHTTPRequestHandler):adset_id = query.get("adset_id", [""])[0]token = query.get("token", [""])[0]+ # Check if there was an error in the update process+ error_message = query.get("error", [""])[0]+ error_data_encoded = query.get("errorData", [""])[0]++ # Try to decode detailed error data if available+ error_data = {}+ if error_data_encoded:+ try:+ error_data = json.loads(urllib.parse.unquote(error_data_encoded))+ except:+ logger.error("Failed to parse errorData parameter")+# Respond with a verification pageself.send_response(200)- self.send_header("Content-type", "text/html")+ self.send_header("Content-type", "text/html; charset=utf-8")self.end_headers()html = """<html><head><title>Verifying Ad Set Update</title>+ <meta charset="utf-8"><style>body { font-family: Arial, sans-serif; margin: 20px; max-width: 800px; margin: 0 auto; }.status { padding: 15px; margin-top: 20px; border-radius: 6px; }@@ -622,6 +734,14 @@ class CallbackHandler(BaseHTTPRequestHandler):pre { white-space: pre-wrap; word-break: break-all; }.spinner { display: inline-block; width: 20px; height: 20px; border: 3px solid rgba(0, 0, 0, 0.1); border-radius: 50%; border-top-color: #0366d6; animation: spin 1s ease-in-out infinite; margin-right: 10px; }@keyframes spin { to { transform: rotate(360deg); } }+ .fix-suggestion { background-color: #e6f6ff; border: 1px solid #79b8ff; padding: 15px; border-radius: 6px; margin-top: 10px; }+ .code-block { background-color: #f6f8fa; padding: 8px; border-radius: 4px; font-family: monospace; }+ .error-list { margin-top: 10px; }+ .error-list li { margin-bottom: 8px; }+ .debug-section { background-color: #f0f0f0; margin-top: 30px; padding: 15px; border: 1px dashed #666; }+ .debug-section h3 { color: #333; }+ .raw-response { font-family: monospace; font-size: 12px; max-height: 300px; overflow: auto; }+ .error-details { margin-top: 15px; background-color: #fff5f5; padding: 15px; border-left: 3px solid #d73a49; }</style></head><body>@@ -641,17 +761,244 @@ class CallbackHandler(BaseHTTPRequestHandler):<h3>Updated Ad Set Details:</h3><pre id="adset-details">Loading...</pre></div>++ <div id="debug-section" class="debug-section" style="display: none;">+ <h3>Debug Information</h3>+ <div>+ <h4>URL Parameters:</h4>+ <pre id="url-params">Loading...</pre>+ </div>+ <div>+ <h4>Error Data:</h4>+ <pre id="error-data-debug">""" + json.dumps(error_data, indent=2) + """</pre>+ </div>+ <div>+ <h4>Raw API Response:</h4>+ <pre id="raw-response" class="raw-response">Loading...</pre>+ </div>+ </div><script>+ // Enable debug mode+ const DEBUG = true;++ // Debug logging helper+ function debugLog(message, data) {+ if (DEBUG) {+ if (data) {+ console.log(`[DEBUG] ${message}:`, data);+ } else {+ console.log(`[DEBUG] ${message}`);+ }+ }+ }++ // Show debug section+ if (DEBUG) {+ document.getElementById('debug-section').style.display = 'block';+ }++ // Parse and display URL parameters+ const urlParams = new URLSearchParams(window.location.search);+ const urlParamsObj = {};+ for (const [key, value] of urlParams.entries()) {+ urlParamsObj[key] = value;+ }++ debugLog('URL parameters', urlParamsObj);+ document.getElementById('url-params').textContent = JSON.stringify(urlParamsObj, null, 2);++ // Try to parse error data if available+ let errorData = null;+ const errorDataParam = urlParams.get('errorData');+ if (errorDataParam) {+ try {+ errorData = JSON.parse(decodeURIComponent(errorDataParam));+ debugLog('Parsed error data', errorData);+ } catch (e) {+ debugLog('Failed to parse errorData', e);+ }+ }++ // Check if there was an error passed in the URL+ const errorParam = urlParams.get('error');+ debugLog('Error parameter found', errorParam);++ if (errorParam) {+ // If there's an error, show it immediately+ const errorMessage = decodeURIComponent(errorParam);+ debugLog('Decoded error message', errorMessage);++ const statusElement = document.getElementById('status');+ statusElement.classList.remove('loading');+ statusElement.classList.add('error');++ // Check if this is a Special Ad Category error+ const isSpecialAdCategoryError =+ errorMessage.includes("Special Ad Category") ||+ errorMessage.includes("Advantage+") ||+ errorMessage.includes("advantage_audience");++ debugLog('Is Special Ad Category error', isSpecialAdCategoryError);++ if (isSpecialAdCategoryError) {+ // Format special ad category errors with better explanation+ debugLog('Displaying Special Ad Category error');+ statusElement.innerHTML = `+ <h3>❌ Special Ad Category Restriction</h3>+ <p>${errorMessage}</p>+ <div class="note" style="margin-top:10px">+ <strong>What does this mean?</strong><br>+ Meta restricts certain targeting features like Advantage+ audience for ads in Special Ad Categories+ (housing, employment, credit, social issues, etc.). You need to use standard targeting options instead.+ </div>+ <div class="fix-suggestion">+ <strong>How to fix:</strong><br>+ To update this ad set, try setting <span class="code-block">targeting.targeting_automation.advantage_audience</span> to <span class="code-block">0</span> instead of <span class="code-block">1</span>.+ </div>+ `;+ } else {+ // Standard error display with more details+ debugLog('Displaying standard error');++ // Start with basic error display+ let errorHtml = `+ <h3>❌ Error updating ad set</h3>+ <p>${errorMessage}</p>+ `;++ // Add detailed error information if available+ if (errorData) {+ errorHtml += `<div class="error-details">`;++ if (errorData.details && errorData.details.length > 0) {+ errorHtml += `+ <strong>Error Details:</strong>+ <ul class="error-list">+ ${errorData.details.map(detail => `<li>${detail}</li>`).join('')}+ </ul>+ `;+ }++ if (errorData.apiError) {+ const apiError = errorData.apiError;+ if (apiError.error_user_title) {+ errorHtml += `<div><strong>Error Type:</strong> ${apiError.error_user_title}</div>`;+ }++ if (apiError.code) {+ errorHtml += `<div><strong>Error Code:</strong> ${apiError.code}</div>`;+ }++ if (apiError.error_subcode) {+ errorHtml += `<div><strong>Error Subcode:</strong> ${apiError.error_subcode}</div>`;+ }+ }++ errorHtml += `</div>`;+ }++ statusElement.innerHTML = errorHtml;+ }++ // Just display current state, don't try to verify the update since it failed+ document.getElementById('details').innerHTML = `+ <h3>Current Ad Set Details (Changes Not Applied):</h3>+ <p>Fetching current state...</p>+ <pre id="adset-details">Loading...</pre>+ `;+ document.getElementById('details').style.display = 'block';++ // Fetch the current ad set details to show what wasn't changed+ fetchAdSetDetails();+ } else {+ // Otherwise proceed with normal verification+ debugLog('No error parameter found, proceeding with verification');+ setTimeout(verifyUpdate, 3000);+ }++ // Function to just fetch the ad set details+ async function fetchAdSetDetails() {+ try {+ debugLog('Fetching ad set details');+ const apiUrl = '/api/adset?' + new URLSearchParams({+ adset_id: '""" + adset_id + """',+ token: '""" + token + """'+ });++ debugLog('Fetching from URL', apiUrl);+ const response = await fetch(apiUrl);++ // Log raw response+ const responseText = await response.text();+ debugLog('Raw API response text', responseText);+ document.getElementById('raw-response').textContent = responseText;++ // Parse JSON+ let data;+ try {+ data = JSON.parse(responseText);+ debugLog('Parsed API response', data);+ } catch (parseError) {+ debugLog('Error parsing JSON response', parseError);+ throw new Error(`Failed to parse API response: ${parseError.message}`);+ }++ const detailsElement = document.getElementById('details');+ const adsetDetailsElement = document.getElementById('adset-details');++ // Check if there's an error in the response+ if (data.error) {+ debugLog('Error in API response', data.error);+ adsetDetailsElement.textContent = JSON.stringify({+ error: "Error fetching ad set details",+ details: typeof data.error === 'object' ?+ data.error.message || JSON.stringify(data.error) :+ data.error+ }, null, 2);+ } else {+ // No errors, display the ad set details+ debugLog('Successfully fetched ad set details');+ detailsElement.style.display = 'block';+ adsetDetailsElement.textContent = JSON.stringify(data, null, 2);+ }+ } catch (error) {+ debugLog('Error in fetchAdSetDetails', error);+ console.error('Error fetching ad set details:', error);+ const adsetDetailsElement = document.getElementById('adset-details');+ if (adsetDetailsElement) {+ adsetDetailsElement.textContent = "Error fetching ad set details: " + error.message;+ }+ }+ }+// Function to fetch the ad set details and check if frequency_control_specs was updatedasync function verifyUpdate() {try {- const response = await fetch('/api/adset?' + new URLSearchParams({+ debugLog('Starting verification of update');+ const apiUrl = '/api/adset?' + new URLSearchParams({adset_id: '""" + adset_id + """',token: '""" + token + """'- }));+ });++ debugLog('Verifying update from URL', apiUrl);+ const response = await fetch(apiUrl);++ // Log raw response+ const responseText = await response.text();+ debugLog('Raw verification response text', responseText);+ document.getElementById('raw-response').textContent = responseText;++ // Parse JSON+ let data;+ try {+ data = JSON.parse(responseText);+ debugLog('Parsed verification response', data);+ } catch (parseError) {+ debugLog('Error parsing JSON verification response', parseError);+ throw new Error(`Failed to parse verification response: ${parseError.message}`);+ }- const data = await response.json();const statusElement = document.getElementById('status');const detailsElement = document.getElementById('details');const adsetDetailsElement = document.getElementById('adset-details');@@ -659,25 +1006,83 @@ class CallbackHandler(BaseHTTPRequestHandler):detailsElement.style.display = 'block';adsetDetailsElement.textContent = JSON.stringify(data, null, 2);- // Update success message to reflect API visibility limitations- statusElement.classList.remove('loading');- statusElement.classList.add('success');- statusElement.innerHTML = '✅ Update request was processed successfully. Please verify the changes in Meta Ads Manager UI or monitor ad performance metrics.';+ // Check if there's an error in the response+ if (data.error) {+ debugLog('Error in verification response', data.error);+ statusElement.classList.remove('loading');+ statusElement.classList.add('error');++ // Extract error message from various possible formats+ let errorMessage = "Unknown error occurred";++ if (typeof data.error === 'string') {+ errorMessage = data.error;+ debugLog('Error is string', errorMessage);+ } else if (data.error.message) {+ errorMessage = data.error.message;+ debugLog('Error has message property', errorMessage);+ } else if (data.error.error_message) {+ errorMessage = data.error.error_message;+ debugLog('Error has error_message property', errorMessage);+ } else {+ debugLog('Error format unknown', data.error);+ }++ // Check if this is a Special Ad Category error+ if (errorMessage.includes("Special Ad Category") ||+ errorMessage.includes("Advantage+") ||+ errorMessage.includes("advantage_audience")) {++ // Format special ad category errors with better explanation+ debugLog('Displaying Special Ad Category verification error');+ statusElement.innerHTML = `+ <h3>❌ Special Ad Category Restriction</h3>+ <p>${errorMessage}</p>+ <div class="note" style="margin-top:10px">+ <strong>What does this mean?</strong><br>+ Meta restricts certain targeting features like Advantage+ audience for ads in Special Ad Categories+ (housing, employment, credit, social issues, etc.). You need to use standard targeting options instead.+ </div>+ <div class="fix-suggestion">+ <strong>How to fix:</strong><br>+ To update this ad set, try setting <span class="code-block">targeting.targeting_automation.advantage_audience</span> to <span class="code-block">0</span> instead of <span class="code-block">1</span>.+ </div>+ `;+ } else {+ debugLog('Displaying standard verification error');+ statusElement.innerHTML = `+ <h3>❌ Error retrieving ad set details</h3>+ <p>${errorMessage}</p>+ <div class="raw-error" style="margin-top: 10px;">+ <strong>Raw Error:</strong>+ <pre>${JSON.stringify(data.error, null, 2)}</pre>+ </div>+ `;+ }+ } else {+ // Update success message to reflect API visibility limitations+ debugLog('Verification successful');+ statusElement.classList.remove('loading');+ statusElement.classList.add('success');+ statusElement.innerHTML = '✅ Update request was processed successfully. Please verify the changes in Meta Ads Manager UI or monitor ad performance metrics.';+ }} catch (error) {+ debugLog('Error in verifyUpdate', error);const statusElement = document.getElementById('status');statusElement.classList.remove('loading');statusElement.classList.add('error');- statusElement.textContent = 'Error verifying update: ' + error;+ statusElement.innerHTML = `+ <h3>❌ Error verifying update</h3>+ <p>${error.message}</p>+ <pre>${error.stack}</pre>+ `;}}-- // Wait a moment before verifying- setTimeout(verifyUpdate, 3000);</script></body></html>"""- self.wfile.write(html.encode())+ self.wfile.write(html.encode('utf-8'))returnelif self.path.startswith("/api/adset"):@@ -690,12 +1095,32 @@ class CallbackHandler(BaseHTTPRequestHandler):# Call the Graph API directlyasync def get_adset_data():- endpoint = f"{adset_id}"- params = {- "fields": "id,name,campaign_id,status,daily_budget,lifetime_budget,targeting,bid_amount,bid_strategy,optimization_goal,billing_event,start_time,end_time,created_time,updated_time,attribution_spec,destination_type,promoted_object,pacing_type,budget_remaining,frequency_control_specs"- }-- return await make_api_request(endpoint, token, params)+ try:+ endpoint = f"{adset_id}"+ params = {+ "fields": "id,name,campaign_id,status,daily_budget,lifetime_budget,targeting,bid_amount,bid_strategy,optimization_goal,billing_event,start_time,end_time,created_time,updated_time,attribution_spec,destination_type,promoted_object,pacing_type,budget_remaining,frequency_control_specs"+ }++ result = await make_api_request(endpoint, token, params)++ # Check if result is a string (possibly an error message)+ if isinstance(result, str):+ try:+ # Try to parse as JSON+ parsed_result = json.loads(result)+ return parsed_result+ except json.JSONDecodeError:+ # Return error object if can't parse as JSON+ return {"error": {"message": result}}++ # If the result is None, return an error object+ if result is None:+ return {"error": {"message": "Empty response from API"}}++ return result+ except Exception as e:+ logger.error(f"Error in get_adset_data: {str(e)}")+ return {"error": {"message": f"Error fetching ad set data: {str(e)}"}}# Run the async functionloop = asyncio.new_event_loop()@@ -739,17 +1164,128 @@ class CallbackHandler(BaseHTTPRequestHandler):# Function to perform the actual updateasync def perform_update():try:- changes_dict = json.loads(changes)+ # Handle potential multiple encoding of JSON+ decoded_changes = changes+ # Try multiple decode attempts to handle various encoding scenarios+ for _ in range(3): # Try up to 3 levels of decoding+ try:+ # Try to parse as JSON+ changes_obj = json.loads(decoded_changes)+ # If we got a string back, we need to decode again+ if isinstance(changes_obj, str):+ decoded_changes = changes_obj+ continue+ else:+ # We have a dictionary, break the loop+ changes_dict = changes_obj+ break+ except json.JSONDecodeError:+ # Try unescaping first+ import html+ decoded_changes = html.unescape(decoded_changes)+ try:+ changes_dict = json.loads(decoded_changes)+ break+ except:+ # Failed to parse, will try again in the next iteration+ pass+ else:+ # If we got here, we couldn't parse the JSON+ return {"status": "error", "error": f"Failed to decode changes JSON: {changes}"}+endpoint = f"{adset_id}"- # Create a copy of changes_dict for the API call- api_params = dict(changes_dict)+ # Create API parameters properly+ api_params = {}++ # Add each change parameter+ for key, value in changes_dict.items():+ api_params[key] = value++ # Add the access tokenapi_params["access_token"] = token+ # Log what we're about to send+ logger.info(f"Sending update to Meta API for ad set {adset_id}")+ logger.info(f"Parameters: {json.dumps(api_params)}")+# Make the API request to update the ad setresult = await make_api_request(endpoint, token, api_params, method="POST")++ # Log the result+ logger.info(f"Meta API update result: {json.dumps(result) if isinstance(result, dict) else result}")++ # Handle various result formats+ if result is None:+ logger.error("Empty response from Meta API")+ return {"status": "error", "error": "Empty response from Meta API"}++ # Check if the result contains an error+ if isinstance(result, dict) and 'error' in result:+ # Extract detailed error message from Meta API+ error_obj = result['error']+ error_msg = error_obj.get('message', 'Unknown API error')+ detailed_error = ""++ # Check for more detailed error messages+ if 'error_user_msg' in error_obj and error_obj['error_user_msg']:+ detailed_error = error_obj['error_user_msg']+ logger.error(f"Meta API user-facing error message: {detailed_error}")++ # Extract error data if available+ error_specs = []+ if 'error_data' in error_obj and isinstance(error_obj['error_data'], str):+ try:+ error_data = json.loads(error_obj['error_data'])+ if 'blame_field_specs' in error_data and error_data['blame_field_specs']:+ blame_specs = error_data['blame_field_specs']+ if isinstance(blame_specs, list) and blame_specs:+ if isinstance(blame_specs[0], list):+ error_specs = [msg for msg in blame_specs[0] if msg]+ else:+ error_specs = [str(spec) for spec in blame_specs if spec]++ if error_specs:+ logger.error(f"Meta API blame field specs: {'; '.join(error_specs)}")+ except Exception as e:+ logger.error(f"Error parsing error_data: {e}")++ # Construct most descriptive error message+ if detailed_error:+ error_msg = detailed_error+ elif error_specs:+ error_msg = "; ".join(error_specs)++ # Log the detailed error information+ logger.error(f"Meta API error: {error_msg}")+ logger.error(f"Full error object: {json.dumps(error_obj)}")++ return {+ "status": "error",+ "error": error_msg,+ "api_error": result['error'],+ "detailed_errors": error_specs,+ "full_response": result+ }++ # Handle string results (which might be error messages)+ if isinstance(result, str):+ try:+ # Try to parse as JSON+ result_obj = json.loads(result)+ if isinstance(result_obj, dict) and 'error' in result_obj:+ return {"status": "error", "error": result_obj['error'].get('message', 'Unknown API error')}+ except:+ # If not parseable as JSON, return as error message+ return {"status": "error", "error": result}++ # If we got here, assume successreturn {"status": "approved", "api_result": result}except Exception as e:+ # Log the exception for debugging+ logger.error(f"Error in perform_update: {str(e)}")+ import traceback+ logger.error(traceback.format_exc())return {"status": "error", "error": str(e)}# Run the async function@@ -759,9 +1295,51 @@ class CallbackHandler(BaseHTTPRequestHandler):result = loop.run_until_complete(perform_update())loop.close()- self.wfile.write(json.dumps(result).encode())+ # Ensure result is a dictionary+ if not isinstance(result, dict):+ logger.error(f"Unexpected result type: {type(result)}")+ self.wfile.write(json.dumps({"status": "error", "error": str(result)}).encode())+ return++ # Check if the API call returned an error+ if result.get("status") == "error":+ error_message = result.get("error", "Unknown error")+ detailed_errors = result.get("detailed_errors", [])++ # Log the detailed error+ logger.error(f"Meta API error during ad set update: {error_message}")+ if "api_error" in result:+ logger.error(f"Detailed API error: {json.dumps(result['api_error'])}")++ # Prepare error response with all available details+ error_response = {+ "status": "error",+ "error": error_message,+ "errorDetails": detailed_errors+ }++ # Include the full API error object for complete details+ if "api_error" in result:+ error_response["apiError"] = result["api_error"]++ # Include the full response if available+ if "full_response" in result:+ error_response["fullResponse"] = result["full_response"]++ logger.info(f"Returning error response: {json.dumps(error_response)}")+ self.wfile.write(json.dumps(error_response).encode())+ else:+ logger.info("Update successful, returning result")+ self.wfile.write(json.dumps(result).encode())except Exception as e:- self.wfile.write(json.dumps({"status": "error", "error": str(e)}).encode())+ logger.error(f"Exception in update-confirm handler: {str(e)}")+ import traceback+ logger.error(traceback.format_exc())+ self.wfile.write(json.dumps({+ "status": "error",+ "error": str(e),+ "traceback": traceback.format_exc()+ }).encode())else:# Store the cancellationupdate_confirmation = {