private bool Tweet(string textMsg, string filePath)
        {
            try
            {
                var _consumerKey       = "sKYbEuMWcgHVstzMNI2LOGzPP";                          // "rbZAEqnJlTuOALvQuJVwTv4Gr";
                var _consumerSecret    = "JOGniXtq1ozm4x5Kg5HpaJKw4TCpuKcD7RNFVIGuGfda9sZFWN"; //"8aKU3z8WfuZocaBsQx50sw16fgnptX3nGzNnPLluge2fF8sITk";
                var _accessToken       = "3244195770-fBcXK8QFz7zQSyNyfq6YkLYrtl8l16Q0bIiA9Sr"; //"21481943-ugmojCmL3nd3lX9IM5elidGBpxqREifI5FJSDFXRW";
                var _accessTokenSecret = "J2JQqupHCZOHYdD2TU6bMrh2yiIFYD2YdIaQFcCw48uv4";      //"r5neeSY6xM5JZsEQwxJgIn6bBxAvMLepIm73zF15ze6wq";

                var service = new TwitterService(_consumerKey, _consumerSecret);
                service.AuthenticateWith(_accessToken, _accessTokenSecret);

                TwitterStatus twitterStatus;

                if (!string.IsNullOrWhiteSpace(filePath))
                {
                    var dicImages = new Dictionary <string, Stream>();

                    dicImages.Add("temp", new FileStream(filePath, FileMode.Open));

                    var twitterTask = service.BeginSendTweetWithMedia(new SendTweetWithMediaOptions
                    {
                        Status = textMsg,
                        Images = dicImages
                    });
                    twitterStatus = service.EndSendTweetWithMedia(twitterTask);
                }
                else
                {
                    var twitterTask = service.BeginSendTweet(new SendTweetOptions
                    {
                        Status = textMsg,
                    });
                    twitterStatus = service.EndSendTweet(twitterTask);
                }

                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
示例#2
0
        public bool PublishToTwitter(string post, byte[] imageBytes)
        {
            try
            {
                var service = new TwitterService(Properties.Settings.Default.TwitterConsumerId, Properties.Settings.Default.TwitterConsumerSecret);
                service.AuthenticateWith(Properties.Settings.Default.TwitterToken, Properties.Settings.Default.TwitterTokenSecret);

                if (imageBytes != null && imageBytes.Length > 0)
                {
                    Image  postImage = Image.FromStream(new MemoryStream(imageBytes));
                    string fileName  = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + @"\" + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".jpg";

                    postImage.Save(fileName);

                    Dictionary <string, Stream> dic = new Dictionary <string, Stream>();

                    FileStream ms = new FileStream(fileName, FileMode.Open);

                    dic.Add("", ms);
                    service.BeginSendTweetWithMedia(new SendTweetWithMediaOptions()
                    {
                        Status = post, Images = dic
                    });
                }
                else
                {
                    service.BeginSendTweet(new SendTweetOptions()
                    {
                        Status = post
                    });
                }

                return(true);
            }
            catch
            {
                return(false);
            }
        }