Search code examples
pythonfacebook-marketing-api

Facebook python marketing API AttributeError


I am trying to use Facebook Marketing API through its python package facebookapi.

here's a code snippet:

from facebookads.adobjects.customaudience import CustomAudience

audience = CustomAudience(parent_id='act_10336...')
audience[CustomAudience.Field.subtype] = CustomAudience.Subtype.custom
audience[CustomAudience.Field.name] = 'My new CA'
audience[CustomAudience.Field.description] = 'People who bought on my website'

audience.remote_create()

It is taken directly from Facebook's example for custom audience creation given here.

I put the appropriate AD_ACCOUNT_ID in the second line.

I get an error:

AttributeError: 'NoneType' object has no attribute 'call'

After much investigation I found that this line from api.py returns None, which I think is at the heart of the problem:

FacebookAdsApi.get_default_api()

In fact, any api call I tried to perform returned a similar error.

Anyone knows anything about that?


Solution

  • You didn't bootstrap the API? Try this:

    from facebookads.api import FacebookAdsApi
    from facebookads import objects
    
    my_app_id = '<APP_ID>'
    my_app_secret = '<APP_SECRET>'
    my_access_token = '<ACCESS_TOKEN>'
    proxies = {'http': '<HTTP_PROXY>', 'https': '<HTTPS_PROXY>'} # add proxies if needed
    FacebookAdsApi.init(my_app_id, my_app_secret, my_access_token, proxies)