Facebook C#SDK,用图片创建活动

时间:2013-06-10 12:37:08

标签: facebook-graph-api facebook-events

我想创建一个活动,但我不知道如何更改活动图片。我知道这是一个非常古老的问题,但我仍然找不到任何解决方案,而且我很快就放弃了...请至少告诉我这是来自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 个答案:

答案 0 :(得分:0)

首先,您需要了解Facebook活动有两种类型的图片。旧样式图片和新封面照片。

如果你想做旧样式图片,你不要给它一个url,你需要将其中一个参数中的数据传递给picture参数。

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

现在,如果您想要制作封面照片,我到目前为止唯一能够实现的工作就是将一个网址传递给cover_url参数。

        arguments.Add("cover_url", "http://domain.com/image.jpg");