广告 API - MediaObject - 如何?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这看起来像是一个错误。请将其归档到 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
一般来说,这确实适用于 Facebook Ads API。不过,这可能有点令人困惑,因为每个广告组的 JSON 都必须引用其创意所使用的图像名称,如下
所示:“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:
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.