Facebook C# SDK,用图片创建事件
Posted
技术标签:
【中文标题】Facebook C# SDK,用图片创建事件【英文标题】:Facebook C# SDK, Create Event With Picture 【发布时间】:2013-06-10 12:37:07 【问题描述】:我想创建一个活动,但我不知道如何更改活动图片。我知道这是一个非常古老的问题,但我仍然找不到任何解决方案,我很快就会放弃......请至少告诉我这是来自 Facebook 的错误还是其他什么?
这是我的代码:
Facebook.FacebookClient fb = new Facebook.FacebookClient(accessToken);
Dictionary<string, object> ev = new Dictionary<string, object>();
ev.Add("name", model.name);
ev.Add("start_time", model.start_time);
ev.Add("end_time", model.end_time);
ev.Add("description", model.description);
ev.Add("location", model.location);
ev.Add("privacy_type", model.privacy_type);
ev.Add("is_date_only", model.is_date_only);
//ev.Add("picture", "@https://fbcdn-photos-g-a.akamaihd.net/hphotos-ak-ash3/c0.0.50.50/p50x50/601514_10151470263757778_629077232_s.jpg"); //NOT WORKING
//ev.Add("source", "@https://fbcdn-photos-g-a.akamaihd.net/hphotos-ak-ash3/c0.0.50.50/p50x50/601514_10151470263757778_629077232_s.jpg"); //NOT WORKING
//ev.Add("picture", HttpUtility.UrlEncode("@https://fbcdn-photos-g-a.akamaihd.net/hphotos-ak-ash3/c0.0.50.50/p50x50/601514_10151470263757778_629077232_s.jpg")); //NOT WORKING
//ev.Add("source", HttpUtility.UrlEncode("https://fbcdn-photos-g-a.akamaihd.net/hphotos-ak-ash3/c0.0.50.50/p50x50/601514_10151470263757778_629077232_s.jpg")); //NOT WORKING
//ev.Add("picture", model.picture); //NOT WORKING
object EventId = fb.Post("/me/events", ev);
Dictionary<string, string> p = (new javascriptSerializer()).Deserialize<Dictionary<string, string>>(EventId.ToString());
Dictionary<string, object> pic = new Dictionary<string, object>();
//pic.Add("source", model.picture); //NOT WORKING
//pic.Add("picture", HttpUtility.UrlEncode("https://fbcdn-photos-g-a.akamaihd.net/hphotos-ak-ash3/c0.0.50.50/p50x50/601514_10151470263757778_629077232_s.jpg")); //NOT WORKING
//pic.Add("source",HttpUtility.UrlEncode("https://fbcdn-photos-g-a.akamaihd.net/hphotos-ak-ash3/c0.0.50.50/p50x50/601514_10151470263757778_629077232_s.jpg")); //NOT WORKING
object objPicture = fb.Post("/" + p["id"] + "/picture", pic);
访问令牌、图片 URI 和创建新事件工作正常,但图片不行。
【问题讨论】:
【参考方案1】:首先,您需要了解 Facebook 活动有两种类型的图片。旧风格图片和新封面照片。
如果你想做旧式图片你不给它一个url,你需要将其中一个参数中的数据传递给图片参数。
var arguments = new Dictionary<string, object>();
var mediaSource = new FacebookMediaObject
FileName = "image.jpg",
ContentType = "image/jpeg"
;
mediaSource.SetValue(webClient.DownloadData(url));
arguments.Add("picture", mediaSource);
Object value = client.Post(String.Format("/0/events/", "facebook_pageId_here"), arguments);
如果你想更新那张图片,最好这样做
var arguments = new Dictionary<string, object>();
var mediaSource = new FacebookMediaObject
FileName = "image.jpg",
ContentType = "image/jpeg"
;
mediaSource.SetValue(webClient.DownloadData(url));
arguments.Add("source", mediaSource);
Object value = client.Post(String.Format("/0/picture/", "event_id_here"), arguments);
现在如果你想做封面照片,到目前为止我唯一能做的就是将一个 url 传递给 cover_url 参数。
arguments.Add("cover_url", "http://domain.com/image.jpg");
【讨论】:
以上是关于Facebook C# SDK,用图片创建事件的主要内容,如果未能解决你的问题,请参考以下文章
尝试使用 Graph API (Facebook C# SDK) 更新 Facebook 事件
使用 Facebook IOS SDK 3.1 和 Graph API 发布带有标题的图片