广告 API - MediaObject - 如何?

发布于 2024-10-21 23:28:06 字数 2688 浏览 1 评论 0原文

Facebook 广告 API 需要发布广告 - 它需要将图像附加到帖子中,并在 Creative_spec 中设置文件名,然后在多部分中附加具有该名称的图像。

FacebookMedia 对象似乎适合此操作,但后来我在代码中看到每个帖子只能附加 1 个媒体对象。这是有原因的吗?

这是一些(蹩脚的、第一次通过的)代码:

        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);

然后我需要附加所有图像,不知道如何执行此操作。他们必须在帖子中附上他们的名字,所以我想我可以将它们附加到任何地方:

        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);

编辑:所以这甚至不适用于一张图像,我有一个创意列表,当没有图像发布时,它会发布正确的数据,当与图像一起发布时,它看起来像这样(查看 adgroup_specs):

主机:api.facebook.com 内容长度:5945 预期:100-继续

--8cdad9aeb95b79c 内容处置:表单数据;名称=“方法”

ads.createAdGroups --8cdad9aeb95b79c 内容处置:表单数据;名称=“账户ID”

106925396 --8cdad9aeb95b79c 内容处置:表单数据; name="adgroup_specs"

System.Collections.Generic.List`1[System.Object] --8cdad9aeb95b79c 内容处置:表单数据;名称=“api_key”

--8cdad9aeb95b79c 内容处置:表单数据;名称=“格式”

json字符串 --8cdad9aeb95b79c 内容处置:表单数据; filename="667_屏幕截图2011-02-24下午3.46.29.png" Content-Type: image/png

它正在将 List<> 转换为错误地。这适用于没有多部分的普通帖子。这是一个错误吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呢古 2024-10-28 23:28:06

这看起来像是一个错误。请将其归档到 codeplex 上的问题跟踪器中。 http://facebooksdk.codeplex.com/workitem/list/basic

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

忘年祭陌 2024-10-28 23:28:06

一般来说,这确实适用于 Facebook Ads API。不过,这可能有点令人困惑,因为每个广告组的 JSON 都必须引用其创意所使用的图像名称,如下

[
  {
    "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],
    }
  }
]

所示:“sample.gif”所在的位置告诉 Facebook 也请查看此帖子以获取文件附件的文件名=“sample.gif”。要包含多个图片,请为所有图片附加不同的名称,并在每个广告组中指定它们。

我希望这对尝试使用不同图像同时创建多个广告组的人们有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文