Toni Wheeler
Toni Wheeler

Reputation: 81

How to post an adset to Facebook Marketing api using python

I am trying to post an adset to the facebook marketing api using the python sdk.

I am getting this error.

FacebookBadObjectError: This AdSet object was already created.

From this line:

adset.remote_create(params={'status': 'PAUSED'})

Here is the function:

adset = AdSet(campaign_result["id"])
adset.update({
    'name': 'test adset ex_2',
    'campaign_id': campaign_result["id"],
    'daily_budget': 150,
    'billing_event': 'IMPRESSIONS',
    'optimization_goal': 'REACH',
    'bid_amount': 10,
    'targeting': {'geo_locations': {'countries': ['US']},
                  'publisher_platforms': ['facebook']},
    'start_time': start_time,
    'end_time': end_time
})

adset.remote_create(params={'status': 'PAUSED'})

The documentation does not cover this. I am following this guide: https://medium.com/@gokhang1327/publishing-facebook-ads-with-python-step-by-step-5c2a98ee4d58 With this code: https://github.com/gokhangerdan/publish_facebook_ads/blob/master/script.py

My parameters work on the graph api explorer.

How can i post an adset in python?

Upvotes: 0

Views: 1111

Answers (1)

Jadin Press
Jadin Press

Reputation: 46

This is slightly different than how you were going about it, but I have found it to be much easier to perform all "create" actions at the AdAccount class level, regardless of whether you're creating a campaign, ad set, or ad. This is a function I created for creating new ad sets, assuming the campaign you want to create it in already exists:

my_adaccount = AdAccount([INSERT AD ACCOUNT ID HERE])
    
def create_ad_set(name, campaign_id, optimization_goal, status, daily_budget, billing_event, targeting, bid_strategy):
        params = {
            'name': name,
            'campaign_id': campaign_id,
            'optimization_goal': optimization_goal,
            'status': status,
            'daily_budget': daily_budget,
            'billing_event': billing_event,
            'targeting': targeting,
            'bid_strategy': bid_strategy
        }
        response = my_adaccount.create_ad_set(params=params)
        print(response)
        newest_adset = response['id']

After lots of trial and error, I determined that those are all the required parameters to successfully create an ad set. You can obviously edit them them or add more however you like within the function arguments. Your options can be found here for easy reference.

Upvotes: 1

Related Questions