Facebook's Batch Request not picking up a JSON parameter

1.2k views Asked by At

My goal is to create a number of ads using Facebook's Batch API : https://developers.facebook.com/docs/marketing-api/asyncrequests/v2.9

I am sending a POST request at this URL:

https://graph.facebook.com/v2.9/act_158365238/ads

I am sending 2 parameters as per the documentation:

1- access_token

2- batch

The JSON in the batch parameter looks like :

{
  "method": "POST",
  "relative_url": "v2.9\/act_158365238\/ads",
  "attached_files": "test1",
  "body": "creative={\"title\":\"Test title 1\",\"body\":\"Test body 1\",\"object_url\":\"https:\/\/apps.facebook.com\/testapp\/\", \"image_file\":\"test1.jpg\"}&targeting={\"countries\":[\"US\"]}&name=test1"
}

The Problem

When I send this request with POSTman or my PHP code, it throws the following error

{
  "error": {
    "message": "(#100) The parameter creative is required",
    "type": "OAuthException",
    "code": 100,
    "fbtrace_id": "Gj2sG7N8l1f"
  }
}

However when I send the exact same request via Facebook's Graph API tool, it successfully creates the ads.

2

There are 2 answers

1
xtoik On

According to the API Documentation provided to create creatives you should be posting to the URL "v2.9/act_187687683/adcreatives"... The fragment of the batch that you are showing is used to create an Ad.

If, as you say, your intention is to create an AdCreative then you should be using something like the above, which differs in the body from what you are using:

         {
          "method": "POST",
          "name": "create_creative",
          "relative_url": "v2.9/act_187687683/adcreatives",
          "attached_files": "test1",
          "body": "title=Test title&body=Test body&link_url=http://www.test12345.com&image_file=test1.jpg"
         }

In the other hand, if what you are creating is an add, then you should consider referencing the AdCreative by its ID as is done in the examples, hence in the case of a creative added in the same batch you could use the relative reference to the name:

         creative={\"creative_id\":\"{result=create_creative:$.id}\"}

or if it is a creative already created you can reference it by the creative_id:

         creative={\"creative_id\":\"123456\"}
1
Aidan Hoolachan On

I think the message is a red herring -- it's not seeing your body's creative field because the OAuth isn't properly set in your POSTman requests, so it isn't parsing the body or seeing the creative field.

If you don't know how to set OAuth in POSTman, here's a good tutorial: https://docs.brightcove.com/en/video-cloud/concepts/postman/postman.html