Facebook Business SDK:无法创建广告

Facebook Business SDK: cannot create an Ad

我无法使用移动应用程序的外部 link 创建简单的广告。我已正确设置访问权限,可以创建广告系列、广告集、加载图像,但在创建广告期间出现错误:

Ads and ad creatives must be associated with a Facebook Page. Try connecting your ad or ad creative to a Page and resubmit your ad.

但是我关联了一个页面!这是我的代码:

# No problem with these ones
adset = ...
image_hash = '...'
url = 'https://itunes.apple.com/app/96...'
page_id = '25036...'

# Create an Ad Creative
creative = AdCreative()
creative['_parent_id'] = my_ads_acc_id
creative[AdCreative.Field.title] = 'Aivan Test Creative'
creative[AdCreative.Field.body] = 'Aivan Test Ad Creative Body'
creative[AdCreative.Field.actor_id] = page_id
creative[AdCreative.Field.link_url] = url
creative[AdCreative.Field.object_url] = url
creative[AdCreative.Field.object_type] = AdCreative.ObjectType.domain
creative[AdCreative.Field.call_to_action_type] = AdCreative.CallToActionType.use_mobile_app
creative[AdCreative.Field.image_hash] = image_hash

# Create an Ad
ad = Ad()
ad['_parent_id'] = my_ads_acc_id
ad[Ad.Field.name] = 'Aivan Ad'
ad[Ad.Field.adset_id] = adset[AdSet.Field.id]
ad[Ad.Field.creative] = creative
# This line generates an exception:
ad.remote_create(params={
    'status': Ad.Status.paused,
})

我已经指定了 actor_id 字段,我也尝试了其他不同的代码示例,但没有任何效果。如何连接页面?

附加信息:

我很久以前就解决了这个问题,从那时起,我的服务器应用程序成功地为网站和移动应用程序创建了大量这两种类型的 Facebook 广告。解决问题的第一步是了解这些广告类型在 Facebook 上是完全不同的,它们需要对 Campaign、AdSet 和 Ad 进行不同的设置。这是我的移动广告创建代码。

1) 创建 Campaign objectaccount_id 必须是您的广告帐户的 ID。

campaign = Campaign()
campaign['_parent_id'] = account_id
campaign[Campaign.Field.name] = 'Some Campaign Name'
campaign[Campaign.Field.objective] = 'APP_INSTALLS'
campaign.remote_create()
campaign_id = str(campaign[Campaign.Field.id])

2) 创建 AdSet object.

adset = AdSet()
adset['_parent_id'] = account_id
adset.update({
    AdSet.Field.name: 'Some AdSet Name',
    AdSet.Field.campaign_id: campaign_id,
    AdSet.Field.lifetime_budget: budget * 100,
    AdSet.Field.bid_strategy: 'LOWEST_COST_WITHOUT_CAP',
    AdSet.Field.billing_event: AdSet.BillingEvent.link_clicks,
    AdSet.Field.optimization_goal: AdSet.OptimizationGoal.link_clicks,
    AdSet.Field.promoted_object: {
        'object_store_url': app_store_url,
        'application_id': ad_app_id,
    },
    AdSet.Field.targeting: targeting_object,
    AdSet.Field.start_time: '2018-12-01 00:00:00',
    AdSet.Field.end_time: '2018-12-30 23:59:00',
})
adset.remote_create()
adset_id = str(adset[AdSet.Field.id])

请注意,要制作移动广告,您首先需要将您的移动应用程序注册为 Facebook 应用程序(此处您将获得 ad_app_id)并指定指向 Apple App Store 和 Google Play Market 的链接.因此,app_store_url 的值必须等于您的 Facebook 应用程序设置中的其中一个链接。不幸的是,应用程序只能手动注册(如果您知道如何以编程方式注册 - 请发表评论)。

另请注意,billing_eventoptimization_goal 与广告类型 (mobile/web) 相关联并且相互关联,您不能只选择另一个。 (但是如果你知道这是可能的,或者有一些关于这个主题的文档 - 让我知道。)

budget 是以您广告帐户的货币表示的金额。您可以指定 lifetime_budget 或类似 day_budget 的内容,请阅读有关它的文档。

3) 然后,你必须创建 AdCreative object with some other sub objects. Note that some of these lines of code are necessary for FB ad only, others for IG, others for both of them, but together they work well for everything. You can find description for all the formats here.

link_data = AdCreativeLinkData()
link_data[AdCreativeLinkData.Field.name] = main_text
link_data[AdCreativeLinkData.Field.message] = title
link_data[AdCreativeLinkData.Field.link] = app_store_url
link_data[AdCreativeLinkData.Field.image_hash] = image_hash
link_data[AdCreativeLinkData.Field.call_to_action] = {
    'type': 'INSTALL_MOBILE_APP',
    'value': {
        'application': ad_app_id,
        'link': app_store_url,
    },
}

object_story_spec = AdCreativeObjectStorySpec()
object_story_spec[AdCreativeObjectStorySpec.Field.page_id] = page_id
object_story_spec[AdCreativeObjectStorySpec.Field.link_data] = link_data

creative = AdCreative()
creative['_parent_id'] = account_id
creative[AdCreative.Field.object_story_spec] = object_story_spec
creative[AdCreative.Field.title] = main_text
creative[AdCreative.Field.body] = title
creative[AdCreative.Field.actor_id] = page_id
creative[AdCreative.Field.link_url] = app_store_url
creative[AdCreative.Field.image_hash] = image_hash

要上传图片并获得 image_hash,请查看 this docpage_id 必须是页面的 ID,该页面的名称和徽标将显示为广告作者。

您必须注意,创建广告的用户必须有权访问此页面、在 FB 上注册的移动应用程序 (ad_app_id) 以及广告帐户 (account_id).在我的服务器应用程序中,我使用 Facebook system users 来处理 API.

的所有工作

4) 最后,创建 Ad object 本身:

ad = Ad()
ad['_parent_id'] = account_id
ad[Ad.Field.name] = 'Some Ad Name'
ad[Ad.Field.adset_id] = adset_id
ad[Ad.Field.creative] = creative
ad.remote_create(params={
    'status': Ad.Status.active,
})
ad_id = str(ad[Ad.Field.id])

就这些了!

也许有人在为网站创建 FB/IG 广告时需要使用或只是想看看区别,它稍微简单一些。所以,这是我用于 网站广告 创建的代码。

1) 创建 Campaign object。请注意,网站广告具有不同的 objectiveaccount_id 必须是您的广告帐户的 ID。

campaign = Campaign()
campaign['_parent_id'] = account_id
campaign[Campaign.Field.name] = 'Some Campaign Name'
campaign[Campaign.Field.objective] = 'LINK_CLICKS'
campaign.remote_create()
campaign_id = str(campaign[Campaign.Field.id])

2) 创建 AdSet object。请注意,billing_eventoptimization_goal 与广告类型 (mobile/web) 相互关联。此外,这里您不需要在 AdSet 中指定 promoted_object

adset = AdSet()
adset['_parent_id'] = account_id
adset.update({
    AdSet.Field.name: 'Some AdSet Name',
    AdSet.Field.campaign_id: campaign_id,
    AdSet.Field.lifetime_budget: budget * 100,
    AdSet.Field.bid_strategy: 'LOWEST_COST_WITHOUT_CAP',
    AdSet.Field.billing_event: AdSet.BillingEvent.impressions,
    AdSet.Field.optimization_goal: AdSet.OptimizationGoal.reach,
    AdSet.Field.targeting: targeting_object,
    AdSet.Field.start_time: '2018-12-01 00:00:00',
    AdSet.Field.end_time: '2018-12-30 23:59:00',
})
adset.remote_create()
adset_id = str(adset[AdSet.Field.id])

预算规则相同:budget 是以您广告帐户的货币表示的金额。您可以指定 lifetime_budget 或类似 day_budget 的内容,请阅读相关文档。

3) 然后,你必须创建 AdCreative object with some other sub objects. You can find description for all the formats here.

link_data = AdCreativeLinkData()
link_data[AdCreativeLinkData.Field.name] = main_text
link_data[AdCreativeLinkData.Field.message] = title
link_data[AdCreativeLinkData.Field.link] = website_url
link_data[AdCreativeLinkData.Field.image_hash] = image_hash

object_story_spec = AdCreativeObjectStorySpec()
object_story_spec[AdCreativeObjectStorySpec.Field.page_id] = page_id
object_story_spec[AdCreativeObjectStorySpec.Field.link_data] = link_data

creative = AdCreative()
creative['_parent_id'] = account_id
creative[AdCreative.Field.object_story_spec] = object_story_spec
creative[AdCreative.Field.title] = main_text
creative[AdCreative.Field.body] = title
creative[AdCreative.Field.actor_id] = page_id
creative[AdCreative.Field.link_url] = website_url
creative[AdCreative.Field.object_type] = AdCreative.ObjectType.domain
creative[AdCreative.Field.image_hash] = image_hash

要上传图片并获得 image_hash,请查看 this docpage_id 必须是页面的 ID,该页面的名称和徽标将显示为广告作者。请注意,创建广告的用户必须有权访问此页面、在 FB 上注册的移动应用程序 (ad_app_id) 以及广告帐户 (account_id)。

4) 最后,创建 Ad object 本身:

ad = Ad()
ad['_parent_id'] = account_id
ad[Ad.Field.name] = 'Some Ad Name'
ad[Ad.Field.adset_id] = adset_id
ad[Ad.Field.creative] = creative
ad.remote_create(params={
    'status': Ad.Status.active,
})
ad_id = str(ad[Ad.Field.id])

如您所见,要推广网站,您无需在 Facebook 上注册它们(与移动广告相反)。