Reputation: 13286
I have found it very challenging to write a Python script to post ads to Facebook in Python. In particular, I want to post ads that show up in mobile news feeds. Can you provide boilerplate code to post one ad, that I can expand on?
My goal is to write a fuller Python script post thousands of ads to Facebook. Most parts of the ads are the same, but they vary base on gender, interest, or device in some specific way.
Upvotes: 0
Views: 1895
Reputation: 1470
You use the example code i wrote with Python. Edit the code for your purpose and with your tokens, ids, etc. I tested and currently using this same script.
Github link to the script here!
from facebook_business.adobjects.adaccount import AdAccount
from facebook_business.adobjects.campaign import Campaign
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.targetingsearch import TargetingSearch
from facebook_business.adobjects.targeting import Targeting
import datetime
from facebook_business.adobjects.adset import AdSet
from facebook_business.adobjects.adimage import AdImage
access_token = ''
app_secret = ''
app_id = ''
ad_account_id = 'act_'
page_id = ''
FacebookAdsApi.init(access_token=access_token)
params = {
'name': 'ENTER CAMPAIGN NAME HERE',
'objective': 'POST_ENGAGEMENT',
'status': 'ACTIVE',
}
campaign_result = AdAccount(ad_account_id).create_campaign(params=params)
print(campaign_result)
today = datetime.date.today()
start_time = str(today)
end_time = str(today + datetime.timedelta(weeks=1))
adset = AdSet(parent_id=ad_account_id)
adset.update({
'name': 'ENTER ADSET NAME HERE',
'campaign_id': campaign_result["id"],
'daily_budget': 150,
'billing_event': 'IMPRESSIONS',
'optimization_goal': 'REACH',
'bid_amount': 10,
'targeting': {'geo_locations': {'countries': {'TR'}},
'publisher_platforms': 'facebook'},
'start_time': start_time,
'end_time': end_time,
})
adset.remote_create(params={'status': 'ACTIVE'})
print(adset)
image = AdImage(parent_id=ad_account_id)
image[AdImage.Field.filename] = 'ENTER AD IMAGE PATH HERE'
image.remote_create()
image_hash = image[AdImage.Field.hash]
print(image)
fields = [
]
params = {
'name': 'ENTER CREATIVE NAME HERE',
'object_story_spec': {'page_id':page_id,'link_data':{'image_hash':image_hash,'link':'ENTER FACEBOOK PAGE LINK-PAGE_ID HERE','message':'ENTER AD MESSAGE HERE'}},
}
adcreative = AdAccount(ad_account_id).create_ad_creative(fields=fields, params=params)
print(adcreative)
fields = [
]
params = {
'name': 'ENTER AD NAME HERE',
'adset_id': adset['id'],
'creative': {'creative_id': adcreative['creative_id']},
'status': 'ACTIVE'
}
print(AdAccount(ad_account_id).create_ad(fields=fields, params=params))
Upvotes: 1
Reputation: 106
This is a year too late, but maybe it'll help someone who comes in through Google or something. :P
Try the Facebook Python Ads API SDK (which wasn't previously available): https://github.com/facebook/facebook-python-ads-sdk
There are some examples there on how to bootstrap your session and create objects, but here's a quick snippet:
from facebookads.session import FacebookSession
from facebookads.api import FacebookAdsApi
from facebookads.objects import AdGroup
my_app_id = '<APP_ID>'
my_app_secret = '<APP_SECRET>'
my_access_token = '<ACCESS_TOKEN>'
my_session = FacebookSession(my_app_id, my_app_secret, my_access_token)
my_api = FacebookAdsApi(my_session)
FacebookAdsApi.set_default_api(my_api)
new_adgroup = AdGroup(parent_id='act_<AD_ACCOUNT_ID>')
new_adgroup[AdGroup.Field.name] = 'My Adgroup'
new_adgroup[AdGroup.Field.campaign_id] = <AD_SET_ID>
new_adgroup[AdGroup.Field.creative] = {
'creative_id': <CREATIVE_ID>
}
new_adgroup.remote_create()
Upvotes: 1
Reputation: 1245
This https://github.com/facebook/fbconsole/blob/master/src/fbconsole.py was a great starting point for me when I did what I needed to do with Facebook, although I did not do ads with it.
Upvotes: 0