Search code examples
pythonfacebookapiaudience

facebook python api add users to CustomAudience


By following https://developers.facebook.com/docs/marketing-api/audiences-api, I was able to add few users (MOBILE_ADVERTISER_ID) to a Custom Audience using cURL. However, since my goal is to do bulk loading (tens of thousands), I switched to use python SDK.

I'm getting the following error, this is the SCHEMA I've called in cUrl.

    audience.add_users(CustomAudience.Schema.MOBILE_ADVERTISER_ID, users)
AttributeError: type object 'Schema' has no attribute 'MOBILE_ADVERTISER_ID'

Here is my code

from facebookads.adobjects.customaudience import CustomAudience

audience = CustomAudience('custom_audience_id')
users = ['ID_1','ID_2','ID_3']

audience.add_users(CustomAudience.Schema.MOBILE_ADVERTISER_ID, users)

Also, in cUrl I had to pass in ACCESS_TOKEN, but in Python sample code, it's not mentioned at all, how come?


Solution

  • I think it should be in small letters: mobile_advertiser_id instead of MOBILE_ADVERTISER_ID.

    For the authentication:

    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)
    

    ref: https://github.com/facebook/facebook-python-ads-sdk