public string UploadToFacebook(MemoryStream ms) { // TODO: Make progressbar functional for C# Facebook SDK. // Limitation in current C# Facebook SDK makes us unable to track progress of photo uploads. this.ProgressBar.Start("Facebook", ms.Length); string url = ""; try { ms.Seek(0, SeekOrigin.Begin); FacebookMediaStream img = new FacebookMediaStream(); img.ContentType = "image/png"; img.FileName = this.RandomFilename(5) + ".png"; img.SetValue(ms); Dictionary <string, object> photoParams = new Dictionary <string, object>(); // TODO: Some way to attach a message to an image //photoParams["message"] = message; photoParams["image"] = img; dynamic ret = this.facebookClient.Post("/me/photos", photoParams); string photoID = ret.id; url = "https://" + "www.facebook.com/photo.php?fbid=" + photoID; this.AddLog(url); } catch (Exception ex) { this.Debug("UploadToFacebook threw " + ex.GetType().FullName + ": '" + ex.Message + "'"); } this.ProgressBar.Done(); return(url); }
public async Task <JsonResult> PostVideo(IEnumerable <HttpPostedFileBase> VideosToUpload, string VideoDescription) { var access_token = HttpContext.Items["access_token"].ToString(); //Get Upload Photo Data var UploadedVideo = VideosToUpload.First(); var video_FBObject = new Facebook.FacebookMediaStream { FileName = UploadedVideo.FileName, ContentType = UploadedVideo.ContentType }; video_FBObject.SetValue(UploadedVideo.InputStream); //FB Graph API Parameters dynamic parameters = new ExpandoObject(); parameters.access_token = access_token; //If you have enabled requires app secret proof on FB app dashboard for your registerted application //parameters.appsecret_proof = access_token.GenerateAppSecretProof(); parameters.source = video_FBObject; if (!string.IsNullOrEmpty(VideoDescription)) { parameters.message = VideoDescription; } //region FB Graph API Add Photo var fb = new FacebookClient(); dynamic result = await fb.PostTaskAsync( "/me/videos", parameters); #region Add Photo Result if (result != null && result.id != null) { if (result.post_id != null) { return(Json(new { status = "posted" })); } else { return(Json(new { status = "pending" })); } } else { return(Json(new { status = "failed" })); } #endregion }
public async Task <JsonResult> PostPhoto(IEnumerable <HttpPostedFileBase> PhotosToUpload, string TargetAlbumId, string PhotoDescription) { var access_token = HttpContext.Items["access_token"].ToString(); if (!string.IsNullOrEmpty(access_token)) { if (PhotosToUpload != null && PhotosToUpload.Count() > 0 && PhotosToUpload.First().HasFile() && !string.IsNullOrEmpty(TargetAlbumId)) { #region Get Upload Photo Data var UploadedPhoto = PhotosToUpload.First(); var photo_FBObject = new Facebook.FacebookMediaStream { FileName = UploadedPhoto.FileName, ContentType = UploadedPhoto.ContentType }; photo_FBObject.SetValue(UploadedPhoto.InputStream); #endregion #region FB Graph API Parameters dynamic parameters = new ExpandoObject(); parameters.access_token = access_token; parameters.appsecret_proof = access_token.GenerateAppSecretProof(); parameters.source = photo_FBObject; if (!string.IsNullOrEmpty(PhotoDescription)) { parameters.message = PhotoDescription; } #endregion #region FB Graph API Add Photo var fb = new FacebookClient(); dynamic result = await fb.PostTaskAsync( string.Format("/{0}/photos", TargetAlbumId), parameters); #endregion #region Add Photo Result if (result != null && result.id != null) { if (result.post_id != null) { return(Json(new { status = "posted" })); } else { return(Json(new { status = "pending" })); } } else { return(Json(new { status = "failed" })); } #endregion } return(null); } else { throw new HttpException(404, "Missing Access Token"); } }
/// <summary> /// Post photo on wall of facebook user /// </summary> public void PostPhoto() { var facebookClient = new FacebookClient(_accessToken); FacebookMediaStream media = new FacebookMediaStream {ContentType = "image/jpeg", FileName = "test Image"}; media.SetValue(_imgStream); facebookClient.PostCompleted += (o, args) => { if (args.Error != null) { Dispatcher.BeginInvoke(() => MessageBox.Show(args.Error.Message)); return; } var result = (IDictionary<string, object>) args.GetResultData(); _lastMessageId = (string) result["id"]; Dispatcher.BeginInvoke(() => { MessageBox.Show( "Message Posted successfully"); btnDeletePost.IsEnabled = true; }); }; IDictionary<string, object> parameters = new Dictionary<string, object>(); //// to upload photo with link //parameters["name"] = "Check this out"; //parameters["link"] = "www.xyz.com"; //parameters["caption"] = "xyz dot com"; //parameters["description"] = "Test url hello how are you, Black Code , Media Test"; //parameters["picture"] = media; //parameters["message"] = "Check this out"; //parameters["actions"] = ""; //facebookClient.PostAsync("me/feed", parameters); // to upload only photo with message parameters["message"] = "Photo upload test " + DateTime.Now.ToString(); parameters["file"] = media; facebookClient.PostAsync("me/Photos", parameters); // post photo on wall }
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); }
public string UploadVideoToFacebook(Stream videoStream) { this.ProgressBar.Start("Facebook", videoStream.Length); string url = ""; FacebookMediaStream video = new FacebookMediaStream(); video.ContentType = "video/mp4"; video.FileName = this.RandomFilename(5) + ".mp4"; video.SetValue(videoStream); Dictionary<string, object> videoParams = new Dictionary<string, object>(); videoParams["video"] = video; dynamic ret = this.facebookClient.Post("/me/videos", videoParams); string videoID = ret.id; url = "https://www.facebook.com/video.php?v=" + videoID; this.ProgressBar.Done(); return url; }
public string UploadToFacebook(MemoryStream ms) { // TODO: Make progressbar functional for C# Facebook SDK. // Limitation in current C# Facebook SDK makes us unable to track progress of photo uploads. this.ProgressBar.Start("Facebook", ms.Length); string url = ""; ms.Seek(0, SeekOrigin.Begin); FacebookMediaStream img = new FacebookMediaStream(); img.ContentType = "image/png"; img.FileName = this.RandomFilename(5) + ".png"; img.SetValue(ms); Dictionary<string, object> photoParams = new Dictionary<string, object>(); // TODO: Some way to attach a message to an image //photoParams["message"] = message; photoParams["image"] = img; dynamic ret = this.facebookClient.Post("/me/photos", photoParams); string photoID = ret.id; url = "https://www.facebook.com/photo.php?fbid=" + photoID; this.ProgressBar.Done(); return url; }