Post pictures on users facebook wall using facebook c# sdk

In this tutorial I will show how we can post pictures on facebook wall of user using Facebook C #SDK.
Firstly you need to pass image from font end to back end.
Here I am using mvc , so I am passing file using HttpPostedFileBase from view to controller.
Also we need access token for posting picture.
Bellow is the code

        [HttpPost]
        public ActionResult PostPhotoOnWall(HttpPostedFileBase file)
        {
            var filename = Path.GetFileName(file.FileName);

            var client = new FacebookClient(Session["access_token"].ToString());

            var postparameters = new Dictionary<string, object>();
            var media = new FacebookMediaObject
            {
                FileName = filename,
                ContentType = "image/jpeg"
            };
            var path = Path.Combine(Server.MapPath("~/Content/themes/base/images"),filename);
            file.SaveAs(path);

            byte[] img = System.IO.File.ReadAllBytes(path);
            media.SetValue(img);
            postparameters["source"] = media;
            postparameters["access_token"] = Session["access_token"].ToString();
            var result = client.Post("/me/photos", postparameters);

            return View("PostPhoto");
        }

Like this we can post picture on user’s wall using facebook c# sdk.
Here picture will be uploaded to album named as your application name on facebook. If album is not present then it will be automatically created.
To post pictures on particular album we need to do some modifications to above code.
Bellow is modified code where I am uploading picture to “Timeline Photos” album.

        [HttpPost]
        public ActionResult PostPhotoOnWall(HttpPostedFileBase file)
        {
            var filename = Path.GetFileName(file.FileName);

            var client = new FacebookClient(Session["access_token"].ToString());

            dynamic albums = client.Get("/me/albums");
            var albumData = albums.data;
            long timelineId = 0;
            foreach (var data in albumData)
            {
                if (data.name == "Timeline Photos")
                {
                    timelineId = long.Parse(data.id);
                    break;
                }
            }
            // Post to user's wall
            var postparameters = new Dictionary<string, object>();
            var media = new FacebookMediaObject
            {
                FileName = filename,
                ContentType = "image/jpeg"
            };
            var path = Path.Combine(Server.MapPath("~/Content/themes/base/images"),filename);
            file.SaveAs(path);

            byte[] img = System.IO.File.ReadAllBytes(path);
            media.SetValue(img);

            postparameters["source"] = media;
            postparameters["access_token"] = Session["access_token"].ToString();
            var result = client.Post("/" + timelineId + "/photos", postparameters);

            return View("PostPhoto");
        }

Like this we can post pictures on users wall using facebook c# sdk.

admin

  • Hoan Nguyen

    Hi, i have a problem, i can not get albums because i don not have permission “user_photos”, so where i will set that permission to get albums. And when i set id’s timeline albums of me, i posted but the photo don’t display on feed, i must click approve photo button in Timeline album, then it show on feed

  • kiquenet kiquenet

    How can I get access_token ?

    How can I do it in Windows Forms application ?