Is it possible to post a status update and adding an image file with facebook graph API?

别等时光非礼了梦想. 提交于 2020-01-01 19:17:09

问题


I want to post a status update to my feed and add a photo residing on disk, using the facebook graph api. Is this possible or must I provide be a hyperlink address to the image? I know it's possible to add a photo to an album using the Post object's source parameter but I'm looking for a way to do a status update with a image file attached.


回答1:


It is definitely possible as we have published images directly to Albums and Walls using the Facebook C# API which posts to the graph API

We use the FacebookMediaObject to achieve this but I believe this simply creates the post form data required. Here is out code so you can take a look if it helps:

var facebookClient = new FacebookClient(entry.AccessToken);
var mediaObject = new FacebookMediaObject
            {
                FileName = filename,
                ContentType = "image/jpeg"
            };
var fileBytes = System.IO.File.ReadAllBytes(filename);
mediaObject.SetValue(fileBytes);

IDictionary<string, object> upload = new Dictionary<string, object>();
upload.Add("name", imagename);
upload.Add("message", message);
upload.Add("@file.jpg", mediaObject);

var result = facebookClient.Post("/" + albumId + "/photos", upload) as JsonObject;

Note the field name of "@file.jpg" which the mediaObject is assigned.

hope this helps.

Update

You may have to post the image to the users wall first and recieve an object ID which you can then reference in the post with the object_id field.




回答2:


You can do it Without Using Facebook SDK. Here is the code to upload image via direct Facebook API.

            var ImagePath = "480.jpg";
            var caption = "Description of Photo";
            string fullurl = string.Format("https://graph.facebook.com/me/photos?caption={0}&access_token={1}", System.Web.HttpUtility.UrlEncode(caption), accessToken);
            WebClient client = new WebClient();
            var returnVar = client.UploadFile(fullurl, ImagePath);


来源:https://stackoverflow.com/questions/8225601/is-it-possible-to-post-a-status-update-and-adding-an-image-file-with-facebook-gr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!