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.AdCopyText;
            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?