How to upload image on Facebook fan page album & Create new if album doesn't exists & upload to it using C#

287 views Asked by At

I want to Upload images in Facebook fan page album by providing path of folder and if album does not exists i want to create new album and upload images into it.

I have assigned the no's to the exceptions i am getting in the code.

  1. (OAuthException - #2500) Unknown path components: /Page ID/photos

  2. Working but not posting into album instead directly posting it.

  3. (OAuthException - #100) (#100) source is not properly formatted

  4. (OAuthException - #100) (#100) Invalid ID for album owner

Please refer below code comments for the above exceptions.

    int flag = 0;
    string albumId = string.Empty;
    var app = new FacebookClient(AccessToken);

   //Get all the albums
    dynamic albums = app.Get("PageID/albums");
    foreach(dynamic albumInfo in albums.data)
    {
        if (albumInfo.name == "Album Name")
        {
            // Find the desired album id 
            albumId= albumInfo.id;
            link = albumInfo.link;
            flag = 1;
            break;
        }
    }

    if (flag == 1)
    {

        flag = 1;
        string path = txtDir.Text;
        var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)
            .Where(s => s.EndsWith(".jpeg") || s.EndsWith(".jpg"));

        // Search created album and upload images from folder
        foreach (string fpath in files)
        {
            byte[] photo = File.ReadAllBytes(fpath);
            dynamic parameters = new ExpandoObject();
            parameters.access_token = AccessToken;
            parameters.message = "Message";
            string f = Path.GetFileName(fpath);
            var mediaObject = new FacebookMediaObject
            {
                ContentType = "image/jpg",
                FileName = Path.GetFileName(fpath)

            };
            mediaObject.SetValue(photo);
            parameters.source = mediaObject;


            1.  //-------------- Not Working    
            //var result = app.Post("/" + albumId + "/PageID/photos", parameters);

            2. //---------------Working but directly posting to photos & not to album
            //var result = app.Post("/PageID/photos/" + albumId + "/", parameters);

            3. //---------------Not Working 
            //var result = app.Post("/PageID/photos/" + albumId + "/", parameters);


            //---------------Working for Account not for page
           //var result = app.Post("/" + albumId + "/photos", parameters);

        }
    }
   else
   {
       //Code to Create Album
        FacebookClient facebookClient = new FacebookClient(AccessToken);
        var albumParameters = new Dictionary<string, object>();
        string albumName = "Album Name";
        string albumMessage = "Album Message";
        albumParameters["message"] = albumMessage;
        albumParameters["name"] = albumName;

        4.   //Not Working
        //facebookClient.Post("/PageId/albums", albumParameters);

         //Working for facebook account not page
         //facebookClient.Post("/me/albums", albumParameters);

         // Get all albums
         dynamic albums = app.Get("PageID/albums");
         // Search created album and upload images from folder
         foreach (dynamic albumdata in album.data)
         {
            if (albumdata.name == albumName)
            {
                string albumid = albumdata.id;

                string path = txtDir.Text;
                //Get all the files from the directory
                var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)
                    .Where(s => s.EndsWith(".jpeg") || s.EndsWith(".jpg"));
                foreach (string fpath in files)
                {
                    byte[] photo = File.ReadAllBytes(fpath);
                    dynamic parameters = new ExpandoObject();
                    parameters.access_token = AccessToken;
                    parameters.message = "Message";
                    var mediaObject = new FacebookMediaObject
                    {
                        ContentType = "image/jpg",
                        FileName = Path.GetFileName(fpath)
                    };
                    mediaObject.SetValue(photo);
                    parameters.source = mediaObject;

                   1.  //-------------- Not Working    
                   //var result = app.Post("/" + albumId + "/PageID/photos", parameters);

                  2. //---------------Working but directly posting to photos & not to album
                   //var result = app.Post("/PageID/photos/" + albumId + "/", parameters);

                  3. //---------------Not Working 
                  //var result = app.Post("/PageID/photos/" + albumId + "/", parameters);


                  //---------------Working for Account not for page
                  //var result = app.Post("/" + albumId + "/photos", parameters);


                }
            }
        }
   }
0

There are 0 answers