开发者

Ads API - MediaObject - howto?

开发者 https://www.devze.com 2023-02-16 17:06 出处:网络
The Facebook Ads API has a call for the posting of ads - and it requires attaching images to the post, and setting the filename in a creative_spec and then attaching the image with that name in the mu

The Facebook Ads API has a call for the posting of ads - and it requires attaching images to the post, and setting the filename in a creative_spec and then attaching the image with that name in the multipart.

The FacebookMedia object seems right for this, but then I see in the code that only 1 media object can be attached per post. Is there a reason for this?

Here's some (crappy, first-pass) code:

        dynamic adgroup = new ExpandoObject();
        adgroup.method = "ads.createAdGroups";
        adgroup.account_id = AdAccount.FbAdAccountId;
        adgroup.adgroup_specs = new List<dynamic>();


            dynamic adgroup_specs = new ExpandoObject();
            adgroup_specs.campaign_id = campaignid;
            adgroup_specs.name = ad.campaign_name;
            adgroup_specs.status = 1;
            adgroup_specs.bid_type = 1;
            adgroup_specs.max_bid = ad.max_bid;
            adgroup_specs.targeting = (dynamic)(new ExpandoObject());
            adgroup_specs.targeting.countries = new List<string>();
            adgroup_specs.targeting.countries.Add("US");

            adgroup_specs.creative = (dynamic)(new ExpandoObject());
            adgroup_specs.creative.title = ad.AdTitleText;
            adgroup_specs.creative.body = ad.AdC开发者_JAVA技巧opyText;
            adgroup_specs.creative.link_url = ad.url; // TODO: ASk Bill
            adgroup_specs.creative.file_name = ad.ImageId + "_" + ad.ImageName;

            var image = dc.Images.Where(r => r.ImageId == ad.ImageId).FirstOrDefault();

            adgroup.adgroup_specs.Add(adgroup_specs);

Then I need to attach all the images, not sure how to do this. They have to be in the post with their name, so I figure I can attach them anywhere:

        var images = (from image in dc.Images
                      where image_ids.Contains(image.ImageId)
                      select image).Distinct();


        var p = adgroups as IDictionary<String, object>;

        foreach (var i in images)
        {
            FacebookMediaObject fmo = new FacebookMediaObject();
            fmo.FileName = i.ImageId + "_" + i.Name;
            fmo.SetValue(i.ImageData);
            fmo.ContentType = "image/png";
            p.Add(i.ImageId + "_" + i.Name, fmo);
        }

        dynamic dresult = FbApp.Post(adgroup);

EDIT: So this wasn't even working with one image, I have a list of creatives and when posted without the image, it posts correct data, when posted with an image, it looks like this (look at adgroup_specs):

Host: api.facebook.com Content-Length: 5945 Expect: 100-continue

--8cdad9aeb95b79c Content-Disposition: form-data; name="method"

ads.createAdGroups --8cdad9aeb95b79c Content-Disposition: form-data; name="account_id"

106925396 --8cdad9aeb95b79c Content-Disposition: form-data; name="adgroup_specs"

System.Collections.Generic.List`1[System.Object] --8cdad9aeb95b79c Content-Disposition: form-data; name="api_key"

--8cdad9aeb95b79c Content-Disposition: form-data; name="format"

json-strings --8cdad9aeb95b79c Content-Disposition: form-data; filename="667_Screen shot 2011-02-24 at 3.46.29 PM.png" Content-Type: image/png

It's converting the List<> incorrectly. This works in the normal post without multipart. Is this a bug?


This looks like it is a bug. Please file it in the issue tracker on codeplex. http://facebooksdk.codeplex.com/workitem/list/basic


In general this does work with the Facebook Ads API. It can be a little confusing though because the JSON for each adgroup has to reference the name of the image that it is using for its creative like this:

[
  {
    "campaign_id": 12345,
    "bid_type": 1,                                                                                                                                                                               
    "max_bid": 150,                                                                                                                                                 
    "name": "My New Ad",
    "adgroup_status": 2,                                                                                                                                                                      
    "creative": {
      "title": "My New Ad",
      "body": "Body text for my new ad",
      "link_url": "http://www.example.com/",
      "image_file": "sample.gif"
    },
    "targeting": {
      "country": ["US"],
      "age_min": 25,
      "age_max": 66,
      "genders": [1],
    }
  }
]

The spot where it says "sample.gif" tells Facebook too look into this POST for a file attachment with filename = "sample.gif". To include multiple images, attach them all with different names, and specify them in each of the ad groups.

I hope this is helpful for people trying to create multiple ad groups at once using different images.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号