Пример #1
0
        // private const string urlUpdate = "https://api.twitter.com/1.1/statuses/update.json";

        /// <summary>
        /// The content to tweet, limited to the requisite 140 chars
        /// </summary>
        /// <param name="o">The IPostable object</param>
        /// <param name="szHost">host to use, branding used if null</param>
        /// <returns>The content of the tweet</returns>
        public static string TweetContent(IPostable o, string szHost)
        {
            if (o == null)
            {
                throw new ArgumentNullException(nameof(o));
            }

            if (!o.CanPost)
            {
                return(string.Empty);
            }

            Uri    uriItem = o.SocialMediaItemUri(szHost);
            string szUri   = uriItem == null ? string.Empty : uriItem.AbsoluteUri;

            int           cch = 140;
            StringBuilder sb  = new StringBuilder(cch);

            cch -= szUri.Length + 1;
            sb.Append(o.SocialMediaComment.LimitTo(cch));
            if (szUri.Length > 0)
            {
                sb.AppendFormat(CultureInfo.CurrentCulture, " {0}", szUri);
            }

            return(sb.ToString());
        }
Пример #2
0
        public bool PostToSocialMedia(IPostable o, string szUser, string szHost = null)
        {
            if (o == null)
            {
                throw new ArgumentNullException("o");
            }

            if (!o.CanPost)
            {
                return(false);
            }

            if (String.IsNullOrEmpty(szUser))
            {
                throw new ArgumentNullException("szUser");
            }

            if (String.IsNullOrEmpty(szHost))
            {
                szHost = Branding.CurrentBrand.HostName;
            }

            Profile pf = Profile.GetUser(szUser);

            // Check for user not configured
            if (pf.FacebookAccessToken == null || String.IsNullOrEmpty(pf.FacebookAccessToken.AccessToken))
            {
                return(false);
            }

            // NOTE: We need to update the version code below periodically as the API updates
            const string szUrl = "https://graph.facebook.com/v2.10/me/feed";

            MultipartFormDataContent form = new MultipartFormDataContent();

            Uri uriItem = o.SocialMediaItemUri(szHost);

            MyFlightbook.Image.MFBImageInfo img = o.SocialMediaImage(szHost);

            // Add in the main parameters:
            Dictionary <string, string> dictParams = new Dictionary <string, string>()
            {
                { "access_token", pf.FacebookAccessToken.AccessToken },
                { "message", string.Empty }
            };

            if (uriItem != null)
            {
                dictParams.Add("link", uriItem.AbsoluteUri);
            }

            if (img != null)
            {
                dictParams.Add("picture", img.URLFullImage.ToAbsoluteURL("http", Branding.CurrentBrand.HostName).ToString());
            }

            foreach (string key in dictParams.Keys)
            {
                StringContent sc = new StringContent(dictParams[key]);
                sc.Headers.ContentDisposition = (new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
                {
                    Name = key
                });
                sc.Headers.ContentType        = new System.Net.Http.Headers.MediaTypeHeaderValue("text/plain")
                {
                    CharSet = "ISO-8859-1"
                };
                form.Add(sc);
            }

            // The remainder can run on a background thread, since we don't do anything that requires a result from here.
            new System.Threading.Thread(() =>
            {
                using (HttpClient httpClient = new HttpClient())
                {
                    try
                    {
                        HttpResponseMessage response = httpClient.PostAsync(new Uri(szUrl), form).Result;
                        string szResult = response.Content.ReadAsStringAsync().Result;

                        if (response.IsSuccessStatusCode)
                        {
                            // do a refresh, to extend as much as possible
                            if (MFBFacebook.ExchangeToken(pf.FacebookAccessToken))
                            {
                                pf.FCommit();
                            }
                        }
                        else
                        {
                            HandleFBFailure(szUser, szResult);
                        }
                    }
                    catch (MyFlightbookException) { }
                    catch (System.ArgumentNullException) { }
                    finally
                    {
                        form.Dispose();
                    }
                }
            }).Start();

            return(true);
        }