Reputation: 26
I wanna create a creative with api. When i post object_story_spec parameter, i got this error 'Creative spec must be an associative array (optionally json-encoded)'
this is my json value it is valid.
{ "page_id" : "103830656322074", "link_data": { "call_to_action": {"type":"LEARN_MORE","value":{"link":"facebook.com/"}}, "caption": "Reklam #1", "name": "Reklam #1", "link": "facebook.com/", "message": "facebook.com/" }}
developers.facebook.com/docs/marketing-api/reference/ad-creative#Creating
Upvotes: 1
Views: 3266
Reputation: 309
Just cast the targeting field to string, For example, if you are using requests:
import requests
params = {
'name': 'My Ad Set',
'optimization_goal': 'LINK_CLICKS',
'billing_event': 'IMPRESSIONS',
'bid_amount': 2,
'daily_budget': 100,
'campaign_id': campaign_id,
"targeting": str({
"age_max": 65,
"age_min": 18,
"flexible_spec": [..]
}),
'start_time': datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ"),
'status': 'PAUSED',
'access_token': access_token,
}
response = requests.post(
url=f'https://graph.facebook.com/v11.0/act_{ad_account_id}/adsets',
params=params
)
Upvotes: 0
Reputation: 379
you should url enocde the $object_story_spec before passing into the creative like below.
$object_story_spec = urlencode($object_story_spec);
$creative = new AdCreative(null, 'ad_Acount_id');
$creative->setData(array(
AdCreativeFields::NAME => 'Sample Creative',
AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec,
));
Upvotes: 1
Reputation: 3957
It should be like something.
object_story_spec={
"page_id": "<PAGE_ID>",
"video_data": {
"call_to_action": {"type":"LIKE_PAGE","value":{"page":"<PAGE_ID>"}},
"description": "try it out",
"image_url": "<THUMBNAIL_URL>",
"video_id": "<VIDEO_ID>"
}
}
Or
$object_story_spec = new ObjectStorySpec();
$object_story_spec->setData(array(
ObjectStorySpecFields::PAGE_ID => <PAGE_ID>,
ObjectStorySpecFields::LINK_DATA => <LINK_DATA>,
));
$creative = new AdCreative(null, 'ad_Acount_id');
$creative->setData(array(
AdCreativeFields::NAME => 'Sample Creative',
AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec,
));
Upvotes: 0