I'm just trying to query the api but I get a permissions error
2

My app is live and I've confirmed that I have all the correct permissions, but whne I query the api I get the following error:

Exception: Error 400: {"error":{"message":"Application does not have permission for this action","type":"OAuthException","code":10,"error_subcode":2332002,"is_transient":false,"error_user_title":"Authorization and login needed","error_user_msg":"To access the API, you'll need to follow the steps at facebook.com\/ads\/library\/api.","fbtrace_id":"AEnhzN93pfrnXVL4o5x9jOA"}}

code:

import requests import json

Facebook Ad Library API credentials

ACCESS_TOKEN = 'REDACTED'

API base URL

BASE_URL = 'https://graph.facebook.com/v16.0/ads_archive'

Function to query the Ad Library API

def get_ad_data(search_terms, country='US'): # Set up the parameters params = { 'search_terms': search_terms, # Keywords to search ads 'ad_type': 'POLITICAL_AND_ISSUE_ADS', # Political and issue ads 'ad_active_status': 'ALL', # Active and inactive ads 'ad_reached_countries': [country], # The country where the ad was delivered 'access_token': ACCESS_TOKEN, # Your access token 'fields': 'ad_creative_body,spend,ad_delivery_start_time,ad_delivery_stop_time,advertiser_name', # Fields to return 'limit': 10 # Number of ads to return }

# Make the API request
response = requests.get(BASE_URL, params=params)

# Check if the request was successful
if response.status_code == 200:
    return response.json()
else:
    raise Exception(f"Error {response.status_code}: {response.text}")

Example usage: Search for ads from "Kamala Harris"

ads_data = get_ad_data(search_terms="Kamala Harris")

Print the results

for ad in ads_data['data']: advertiser = ad['advertiser_name'] spend = ad.get('spend', 'N/A') start_time = ad.get('ad_delivery_start_time', 'N/A') stop_time = ad.get('ad_delivery_stop_time', 'N/A') ad_body = ad.get('ad_creative_body', 'No text available')

print(f"Advertiser: {advertiser}")
print(f"Spend: {spend}")
print(f"Ad Start Time: {start_time}")
print(f"Ad Stop Time: {stop_time}")
print(f"Ad Text: {ad_body}\n")
Mark
Asked about 2 months ago
Selected Answer
1

Hello Mark, The same thing is happening to me as to you, I've done the 3 steps that they tell you at https://www.facebook.com/ads/library/api but I still get the error message. Have you been able to find a solution? Regards.

November 5 at 8:01 AM
Marc