你能用Python演示如何将广告发布到Facebook的广告API吗?

时间:2013-10-15 17:32:22

标签: python facebook

我发现编写Python脚本以在Python中向Facebook发布广告非常具有挑战性。特别是,我想发布在移动新闻Feed中显示的广告。你能提供样板代码来发布一个我可以展开的广告吗?

我的目标是编写一个更全面的Python脚本,将数千个广告发布到Facebook。广告的大多数部分都是相同的,但它们会以某种特定的方式根据性别,兴趣或设备而有所不同。

3 个答案:

答案 0 :(得分:1)

这是一年太晚了,但也许它可以帮助那些通过谷歌或其他东西进来的人。 :P

尝试使用Facebook Python广告API SDK(之前不可用): https://github.com/facebook/facebook-python-ads-sdk

有一些关于如何引导会话和创建对象的示例,但这是一个快速代码段:

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()

答案 1 :(得分:0)

这个https://github.com/facebook/fbconsole/blob/master/src/fbconsole.py对我来说是一个很好的起点,当我做我需要做的Facebook时,虽然我没有用它做广告。

答案 2 :(得分:0)

您使用我用Python编写的示例代码。根据您的目的以及您的令牌,ID等来编辑代码。我已经测试过并且当前正在使用相同的脚本。

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))