SetValue() public method

Sets the value of the media object.
public SetValue ( byte value ) : FacebookMediaObject
value byte The media object value.
return FacebookMediaObject
 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
     // upload to facebook
     FacebookMediaObject image = new FacebookMediaObject
     {
         FileName = imgPath,
         ContentType = "image/jpeg"
     };
     byte[] fileBytes = File.ReadAllBytes(imgPath);
     image.SetValue(fileBytes);
     IDictionary<string, object> upload = new Dictionary<string, object>();
     upload.Add("name", imgName);
     upload.Add("message", imgMsg);
     upload.Add("@file.jpg", image);
     fb.UploadProgressChanged += uploadProgressChanged;
     fb.PostAsync("/me/photos", new Dictionary<string, object> {{"source", image}});
 }
Esempio n. 2
0
        /// <summary>
        /// 上傳照片方法
        /// </summary>
        /// <param name="fbApp"></param>
        /// <param name="ImagePath"></param>
        /// <returns></returns>
        private JsonObject UploadPhoto(Facebook.FacebookClient fbApp, string ImagePath, byte[] filebytes, string ContentType)
        {
            Facebook.FacebookMediaObject media = new Facebook.FacebookMediaObject();
            if (ContentType == null)
            {
                ContentType = "image/jpeg";
            }
            media.ContentType = ContentType;
            media.FileName    = ImagePath;

            media.SetValue(filebytes);
            Dictionary <string, object> upload = new Dictionary <string, object>();

            upload.Add("name", "照片名稱");
            upload.Add("message", "照片描述");
            upload.Add("no_story", "1"); // 是否要發佈訊息
            upload.Add("access_token", Session["PageAccessToken"]);
            upload.Add("@file.jpg", media);

            return(fbApp.Post(_pageID + "/photos", upload) as JsonObject);
        }
Esempio n. 3
0
        private void ShareToFacebook()
        {
            if( _fbClient == null || _fbClient.AccessToken == null)
            {
                LoginToFacebook();
                Interlocked.Exchange(ref uploadInProgres, 0);
                return;
            }

            loading.IsIndeterminate = true;
            byte[] contents = null;
            try
            {
                contents = GetLastSavedPictureContents();
                if (contents == null || contents.Length == 0)
                {
                    UploadFinished(false, SocialNetwork.FACEBOOK);
                    MessageBox.Show("Could not load last saved picture. Sorry.", "Error", MessageBoxButton.OK);
                    return;
                }
            }
            catch (Exception e)
            {
                UploadFinished(false, SocialNetwork.FACEBOOK);
                MessageBox.Show("Error occurred during reading saved image.", "Error", MessageBoxButton.OK);
                return;
            }

            var mediaObject = new FacebookMediaObject
            {
                FileName = filename,
                ContentType = "image/jpeg",
            };
            mediaObject.SetValue(contents);

            var parameters = new Dictionary<string, object>();
            parameters["access_token"] = _fbClient.AccessToken;

            parameters["source"] = mediaObject;
            if (titleBar.Text != null && !titleBar.Text.Equals(shareText))
            {
                parameters["message"] = titleBar.Text;
            }

            _fbClient.PostAsync("me/photos", parameters);
        }
        public string SheduleFacebookGroupMessage(string FacebookId, string UserId, string sscheduledmsgguid)
        {
            string str = string.Empty;
            int facint = 0;
            try
            {
                objScheduledMessage = objScheduledMessageRepository.GetScheduledMessageDetails(Guid.Parse(sscheduledmsgguid));
                GroupScheduleMessageRepository grpschedulemessagerepo = new GroupScheduleMessageRepository();
                Domain.Socioboard.Domain.GroupScheduleMessage _GroupScheduleMessage = grpschedulemessagerepo.GetScheduleMessageId(objScheduledMessage.Id);
                if (objFacebookAccountRepository.checkFacebookUserExists(FacebookId, Guid.Parse(UserId)))
                {
                    objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(FacebookId, Guid.Parse(UserId));
                }
                else
                {
                    objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(FacebookId);
                }
                if (objFacebookAccount != null)
                {
                    FacebookClient fbclient = new FacebookClient(objFacebookAccount.AccessToken);
                    var args = new Dictionary<string, object>();
                    args["message"] = objScheduledMessage.ShareMessage;
                    string imagepath = objScheduledMessage.PicUrl;

                    var facebookpost = "";
                    try
                    {
                        if (!string.IsNullOrEmpty(imagepath))
                        {
                            var media = new FacebookMediaObject
                            {
                                FileName = "filename",
                                ContentType = "image/jpeg"
                            };
                            byte[] img = System.IO.File.ReadAllBytes(imagepath);
                            media.SetValue(img);
                            args["source"] = media;
                            facebookpost = fbclient.Post("v2.0/" + _GroupScheduleMessage.GroupId + "/photos", args).ToString();
                        }
                        else
                        {
                            facebookpost = fbclient.Post("v2.0/" + _GroupScheduleMessage.GroupId + "/feed", args).ToString();
                        }
                    }
                    catch (Exception ex)
                    {
                        objFacebookAccountRepository = new FacebookAccountRepository();
                        objFacebookAccount.IsActive = 2;
                        objFacebookAccountRepository.updateFacebookUserStatus(objFacebookAccount);
                        Domain.Socioboard.Domain.SocialProfile objSocialProfile = new Domain.Socioboard.Domain.SocialProfile();
                        SocialProfilesRepository objSocialProfilesRepository = new SocialProfilesRepository();
                        string errormsg = ex.Message;
                        if (errormsg.Contains("access token"))
                        {
                            objSocialProfile.UserId = objFacebookAccount.UserId;
                            objSocialProfile.ProfileId = objFacebookAccount.FbUserId;
                            objSocialProfile.ProfileStatus = 2;
                            objSocialProfilesRepository.updateSocialProfileStatus(objSocialProfile);
                        }
                        Console.WriteLine(ex.Message);
                        str = ex.Message;
                    }

                    if (!string.IsNullOrEmpty(facebookpost))
                    {
                        facint++;
                        str = "Message post on facebook for Id :" + objFacebookAccount.FbUserId + " and Message: " + objScheduledMessage.ShareMessage;
                        ScheduledMessage schmsg = new ScheduledMessage();
                        schmsg.UpdateScheduledMessageByMsgId(Guid.Parse(sscheduledmsgguid));
                        logger.Error("SheduleFacebookGroupMessageCount" + facint);
                    }
                }
                else
                {
                    str = "facebook account not found for id" + objScheduledMessage.ProfileId;
                }
            }
            catch (Exception ex)
            {
                str = ex.Message;
            }
            return str;
        }
        public string FacebookComposeMessageForPage(String message, String profileid, string userid, string currentdatetime, string imagepath)
        {
            string ret = "";
            Domain.Socioboard.Domain.FacebookAccount objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(profileid, Guid.Parse(userid));
            FacebookClient fb = new FacebookClient();

            fb.AccessToken = objFacebookAccount.AccessToken;
            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
            var args = new Dictionary<string, object>();
            args["message"] = message;
            if (!string.IsNullOrEmpty(imagepath))
            {
                var media = new FacebookMediaObject
                {
                    FileName = "filename",
                    ContentType = "image/jpeg"
                };
                byte[] img = System.IO.File.ReadAllBytes(imagepath);
                media.SetValue(img);
                args["source"] = media;
                ret = fb.Post("v2.0/" + objFacebookAccount.FbUserId + "/photos", args).ToString();
            }
            else
            {
                ret = fb.Post("v2.0/" + objFacebookAccount.FbUserId + "/feed", args).ToString();
                //   ret = fb.Post("/" + objFacebookAccount.FbUserId + "/photos", args).ToString();

            }


            //Domain.Socioboard.Domain.FacebookAccount objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(profileid, Guid.Parse(userid));
            //FacebookClient fb = new FacebookClient();

            //fb.AccessToken = objFacebookAccount.AccessToken;
            //fb.AccessToken = "CAACEdEose0cBANZCpywnaiXtamEZBNRpaZAj3zY9y6e62hTfHQ9oKy5LSc9MiZATY4RV5F19CgLNadYuL1neTaCU3ikhQwB7x6FtFcYOuQlQZBNL1xjQuohzHGlL9xrzHd8lQZBcK4KPwObj2ALLgnbNLmqZAywMs2JL3Ke0Vk36lm8fPzHbeode7Kndw9gbgLC5o5CmhdtzWwWzhxxjljk";
            //System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
            //ret = fb.Post("/" + objFacebookAccount.FbUserId + "/feed", args).ToString();
            return ret;
        }
Esempio n. 6
0
        public static string UploadPictureToWall(string accessToken, string filePath)
        {
            // sample usage: UploadPictureToWall(accessToken, @"C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg");

            var mediaObject = new FacebookMediaObject
                                  {
                                      FileName = System.IO.Path.GetFileName(filePath),
                                      ContentType = "image/jpeg"
                                  };

            mediaObject.SetValue(System.IO.File.ReadAllBytes(filePath));

            try
            {
                var fb = new FacebookClient(accessToken);

                var result = (IDictionary<string, object>)fb.Post("me/photos", new Dictionary<string, object>
                                       {
                                           { "source", mediaObject },
                                           { "message","photo" }
                                       });

                var postId = (string)result["id"];

                Console.WriteLine("Post Id: {0}", postId);

                // Note: This json result is not the orginal json string as returned by Facebook.
                Console.WriteLine("Json: {0}", result.ToString());

                return postId;
            }
            catch (FacebookApiException ex)
            {
                // Note: make sure to handle this exception.
                throw;
            }
        }
Esempio n. 7
0
 /// <summary>
 /// This method uploads the stored photo to Facebook on the given album Id.
 /// </summary>
 /// <param name="accessToken">the OAuth access token</param>
 /// <param name="AlbumId">The album Id to which the photo will belong.</param>
 private void UploadPhoto(string accessToken, string AlbumId)
 {
     FacebookApp facebookClient = new FacebookApp(accessToken);
     FacebookMediaObject mediaObject = new FacebookMediaObject
     {
         FileName = "image",
         ContentType = "image/png"
     };
     byte[] fileBytes = bytes;
     mediaObject.SetValue(fileBytes);
     IDictionary<string, object> upload = new Dictionary<string, object>();
     upload.Add("name", "photo name");
     upload.Add("message", PhotoLegend);
     upload.Add("@file.jpg", mediaObject);
     facebookClient.PostAsync("/" + AlbumId + "/photos", upload, UploadPhotoAsyncCallback);
 }
        private static FacebookInfo UploadToFacebook(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string title, string filename, bool repeated)
        {
            FacebookCredentials oAuth = createSession(true);
            if (oAuth == null)
            {
                return null;
            }

            SurfaceContainer container = new SurfaceContainer(surfaceToUpload, outputSettings, filename);
            var client = new FacebookClient(config.Token);
            var img = new FacebookMediaObject {FileName = filename};
            img.SetValue(container.ToByteArray());
            img.ContentType = "multipart/form-data";

            var param = new Dictionary<string, object> {{"attachment", img}};
            if (!string.IsNullOrEmpty(title))
                param.Add("message", title);

            // Not on timeline?
            if (config.NotOnTimeline)
                param.Add("no_story", "1");

            JsonObject result;
            try
            {
                result = (JsonObject) client.Post("me/photos", param);
            }
            catch (FacebookOAuthException ex)
            {
                if (!repeated && ex.ErrorCode == 190) // App no long authorized, reauthorize if possible
                {
                    config.Token = null;
                    return UploadToFacebook(surfaceToUpload, outputSettings, title, filename, true);
                }
                throw;
            }
            catch (Exception ex)
            {
                LOG.Error("Error uploading to Facebook.", ex);
                throw;
            }

            FacebookInfo facebookInfo = FacebookInfo.FromUploadResponse(result);
            LOG.Debug("Upload to Facebook was finished");
            return facebookInfo;
        }
        private void PostToWall()
        {
            IsCalculating = true;
            var parameters = new Dictionary<string, object>
            {
                { "message", Message }
            };

            using (var memoryStream = new MemoryStream())
            {
                Image.SaveJpeg(memoryStream, Image.PixelWidth, Image.PixelHeight, 0, 100);
                memoryStream.Seek(0, 0);
                var data = new byte[memoryStream.Length];
                memoryStream.Read(data, 0, data.Length);
                memoryStream.Close();

                var fbUpl = new FacebookMediaObject
                {
                    ContentType = "image/jpeg",
                    FileName = "image.jpeg",
                };
                fbUpl.SetValue(data);
                parameters.Add("file", fbUpl);
            }           

            client.PostAsync("me/photos", parameters);
        }
Esempio n. 10
0
 public bool AddPhoto(PhotoDetails photoDetails)
 {
     var mediaObject = new FacebookMediaObject
     {
         FileName = photoDetails.FacebookName,
         ContentType = "image/jpeg"
     };
     byte[] fileBytes = photoDetails.ImageStream;
     mediaObject.SetValue(fileBytes);
     IDictionary<string, object> upload = new Dictionary<string, object>();
     upload.Add("name", photoDetails.FacebookName);
     upload.Add("message", photoDetails.PhotoDescription);
     upload.Add("@file.jpg", mediaObject);
     Post("/" + photoDetails.AlbumId + "/photos", upload);
     return true;
 }
Esempio n. 11
0
        public string SheduleFacebookMessage(string FacebookId, string UserId, string sscheduledmsgguid)
        {
            string str = string.Empty;
            try
            {
                objScheduledMessage = objScheduledMessageRepository.GetScheduledMessageDetails(Guid.Parse(sscheduledmsgguid));

                if (objFacebookAccountRepository.checkFacebookUserExists(FacebookId, Guid.Parse(UserId)))
                {
                    objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(FacebookId, Guid.Parse(UserId));
                }
                else
                {
                    objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(FacebookId);
                }
                if (objFacebookAccount != null)
                {
                    FacebookClient fbclient = new FacebookClient(objFacebookAccount.AccessToken);
                    var args = new Dictionary<string, object>();
                    args["message"] = objScheduledMessage.ShareMessage;
                    string imagepath = objScheduledMessage.PicUrl;

                    var facebookpost = "";
                    try
                    {


                        if (!string.IsNullOrEmpty(imagepath))
                        {
                            try
                            {
                                Uri u = new Uri(imagepath);
                                string filename = string.Empty;
                                string extension = string.Empty;
                                extension = Path.GetExtension(u.AbsolutePath).Replace(".", "");
                                var media = new FacebookMediaObject
                                {
                                    FileName = "filename",
                                    ContentType = "image/" + extension
                                };
                                //byte[] img = System.IO.File.ReadAllBytes(imagepath);
                                var webClient = new WebClient();
                                byte[] img = webClient.DownloadData(imagepath);
                                media.SetValue(img);
                                args["source"] = media;
                                facebookpost = fbclient.Post("v2.0/" + objFacebookAccount.FbUserId + "/photos", args).ToString();
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(imagepath + "not Found");
                                if (!string.IsNullOrEmpty(objScheduledMessage.Url))
                                {
                                    args["link"] = objScheduledMessage.Url;
                                }
                                facebookpost = fbclient.Post("v2.0/" + objFacebookAccount.FbUserId + "/feed", args).ToString();
                            }
                        }
                        else
                        {
                            facebookpost = fbclient.Post("v2.0/" + objFacebookAccount.FbUserId + "/feed", args).ToString();
                        }
                    }

                    catch (Exception ex)
                    {
                        //FacebookAccount ObjFacebookAccount = new FacebookAccount();
                        //objFacebookAccount = new Domain.Socioboard.Domain.FacebookAccount();
                        objFacebookAccountRepository = new FacebookAccountRepository();
                        //objFacebookAccount.IsActive = 2;
                        objFacebookAccountRepository.updateFacebookUserStatus(objFacebookAccount);
                        Domain.Socioboard.Domain.SocialProfile objSocialProfile = new Domain.Socioboard.Domain.SocialProfile();
                        SocialProfilesRepository objSocialProfilesRepository = new SocialProfilesRepository();
                        //logger.Error(ex.Message);
                        string errormsg = ex.Message;
                        if (errormsg.Contains("access token"))
                        {
                            objSocialProfile.UserId = objFacebookAccount.UserId;
                            objSocialProfile.ProfileId = objFacebookAccount.FbUserId;
                            objSocialProfile.ProfileStatus = 2;
                            objSocialProfilesRepository.updateSocialProfileStatus(objSocialProfile);
                        }
                        Console.WriteLine(ex.Message);
                        str = ex.Message;
                    }

                    if (!string.IsNullOrEmpty(facebookpost))
                    {
                        str = "Message post on facebook for Id :" + objFacebookAccount.FbUserId + " and Message: " + objScheduledMessage.ShareMessage;
                        ScheduledMessage schmsg = new ScheduledMessage();
                        schmsg.UpdateScheduledMessageByMsgId(Guid.Parse(sscheduledmsgguid));
                    }
                }
                else
                {
                    str = "facebook account not found for id" + objScheduledMessage.ProfileId;
                }
            }
            catch (Exception ex)
            {
                str = ex.Message;
            }
            return str;
        }
Esempio n. 12
0
        public string FacebookComposeMessageForPage(String message, String profileid, string userid, string currentdatetime, string imagepath, string link)
        {
            string ret = "";
            Domain.Socioboard.Domain.FacebookAccount objFacebookAccount = objFacebookAccountRepository.getFacebookAccountDetailsById(profileid, Guid.Parse(userid));
            FacebookClient fb = new FacebookClient();

            fb.AccessToken = objFacebookAccount.AccessToken;
            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
            var args = new Dictionary<string, object>();
            if (!string.IsNullOrEmpty(message))
            {
                args["message"] = message;
            }
            try
            {
                if (!string.IsNullOrEmpty(imagepath))
                {
                    Uri u = new Uri(imagepath);
                    string filename = string.Empty;
                    string extension = string.Empty;
                    extension = Path.GetExtension(u.AbsolutePath).Replace(".", "");
                    var media = new FacebookMediaObject
                    {
                        FileName = "filename",
                        ContentType = "image/" + extension
                    };
                    //byte[] img = System.IO.File.ReadAllBytes(imagepath);
                    var webClient = new WebClient();
                    byte[] img = webClient.DownloadData(imagepath);
                    media.SetValue(img);
                    args["source"] = media;
                    ret = fb.Post("v2.0/" + objFacebookAccount.FbUserId + "/photos", args).ToString();
                }
                else
                {
                    if (!string.IsNullOrEmpty(link))
                    {
                        args["link"] = link;
                    }
                    ret = fb.Post("v2.0/" + objFacebookAccount.FbUserId + "/feed", args).ToString();
                }
                ret = "success";
            }
            catch (Exception ex)
            {
                ret = "failure";
            }
            return ret;
        }
Esempio n. 13
0
        public static string UploadVideo(string accessToken, string filePath)
        {
            // sample usage: UploadVideo(accessToken, @"C:\video.3gp");

            var mediaObject = new FacebookMediaObject
            {
                FileName = System.IO.Path.GetFileName(filePath),
                ContentType = "video/3gpp"
            };

            mediaObject.SetValue(System.IO.File.ReadAllBytes(filePath));

            try
            {
                var fb = new FacebookClient(accessToken);

                var parameters = new Dictionary<string, object>();
                parameters["method"] = "video.upload";
                parameters["caption"] = "video upload using rest api";
                parameters["source"] = mediaObject;

                var result = (IDictionary<string, object>)fb.Post(parameters);

                var videoId = (string)result["vid"];

                Console.WriteLine("Video Id: {0}", videoId);

                // Note: This json result is not the orginal json string as returned by Facebook.
                Console.WriteLine("Json: {0}", result.ToString());

                return videoId;
            }
            catch (FacebookApiException ex)
            {
                // Note: make sure to handle this exception.
                throw;
            }
        }