Exemple #1
0
        public async Task PostPhotoAsync(PostBody info, FacebookClient client)
        {
            if (!System.IO.File.Exists(info.FilePath))
            {
                throw new FileNotFoundException("Filepath is invalid.");
            }

            string extension = Path.GetExtension(info.FilePath).Substring(1);

            if (!possibleExtensions.Contains($"{extension}"))
            {
                throw new FileLoadException("File extension is invalid.");
            }

            string attachementPath = info.FilePath;

            using (var file = new FacebookMediaStream
            {
                ContentType = $"image/{extension}",
                FileName = System.IO.Path.GetFileName(attachementPath)
            }.SetValue(System.IO.File.OpenRead(attachementPath)))
            {
                dynamic result = await client.PostTaskAsync($"{info.PageNameOrId}/photos",
                                                            new
                {
                    message = info.DescriptionAndHashtags == null ? "" :
                              info.DescriptionAndHashtags,
                    file
                });
            }
        }
Exemple #2
0
    private async Task DoSomething()
    {
        var fb = new FacebookClient(AccessToken);

        using (var file = new FacebookMediaStream
        {
            ContentType = imgType,
            FileName = Path.GetFileName(fPath)
        }.SetValue(File.OpenRead(fPath)))
        {
            dynamic result = await fb.PostTaskAsync("me/photos",
                                                    new { message = userMsg, file });
        }
    }
Exemple #3
0
        public void postImage(string path, string message)
        {
            var fb = new FacebookClient(_appSettings.Secret);


            using (var file = new FacebookMediaStream
            {
                ContentType = "image/jpeg",
                FileName = Path.GetFileName(path)
            }.SetValue(File.OpenRead(path)))
            {
                dynamic result = fb.Post("me/photos",
                                         new { message = message, file });
            }
        }
Exemple #4
0
        /// <summary>
        /// Adds photo collection to album in social network. If album does not exist, creates album first.
        /// </summary>
        /// <param name="photos">Collection of photo</param>
        /// <param name="albumName">Album name</param>
        /// <param name="token">User's access token for social network</param>
        public static void AddPhotosToAlbum(IEnumerable <string> photos, string albumName, string token)
        {
            var facebookClient = new FacebookClient(token);
            var albumId        = "";

            //Reads album infos
            dynamic albums = facebookClient.Get("/me/albums");

            foreach (dynamic albumInfo in albums.data)
            {
                if (albumInfo.name != albumName)
                {
                    continue;
                }
                albumId = albumInfo.id;
                break;
            }

            if (string.IsNullOrEmpty(albumId))
            {
                albumId = CreateAlbum(albumName, "Created by Bingally", token);
            }

            foreach (var photo in photos)
            {
                Stream attachement = new FileStream(photo, FileMode.Open);
                var    parameters  = new Dictionary <string, object>();
                parameters["message"] = "uploaded using Bingally";
                parameters["file"]    = new FacebookMediaStream
                {
                    ContentType = "image/jpeg",
                    FileName    = Randomizer.GetString(8) + ".jpg"
                }.SetValue(attachement);

                facebookClient.PostTaskAsync(albumId + "/photos", parameters);
            }

            /*//Get the Pictures inside the album this gives JASON objects list that has photo attributes
             * // described here http://developers.facebook.com/docs/reference/api/photo/
             * dynamic albumsPhotos = facebookClient.Get(albumInfo.id + "/photos");*/
        }
        private void btnPostVideo_Click(object sender, EventArgs args)
        {
            var ofd = new OpenFileDialog
            {
                Filter = "MP4 Files|*.mp4",
                Title  = "Select video to upload"
            };

            if (ofd.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            var fb         = new FacebookClient(_accessToken);
            var attachment = new FacebookMediaStream
            {
                ContentType = "video/mp4",
                FileName    = Path.GetFileName(ofd.FileName)
            }.SetValue(File.OpenRead(ofd.FileName));

            // make sure to add event handler for PostCompleted.
            fb.PostCompleted += (o, e) =>
            {
                attachment.Dispose();

                // incase you support cancellation, make sure to check
                // e.Cancelled property first even before checking (e.Error!=null).
                if (e.Cancelled)
                {
                    // for this example, we can ignore as we don't allow this
                    // example to be cancelled.

                    // you can check e.Error for reasons behind the cancellation.
                    var cancellationError = e.Error;
                }
                else if (e.Error != null)
                {
                    // error occurred
                    this.BeginInvoke(new MethodInvoker(
                                         () =>
                    {
                        MessageBox.Show(e.Error.Message);
                    }));
                }
                else
                {
                    // the request was completed successfully

                    // make sure to be on the right thread when working with ui.
                    this.BeginInvoke(new MethodInvoker(
                                         () =>
                    {
                        MessageBox.Show("Video uploaded successfully");
                    }));
                }
            };

            dynamic parameters = new ExpandoObject();

            parameters.message = txtMessage.Text;
            parameters.source  = attachment;

            fb.PostAsync("me/videos", parameters);
        }