Пример #1
0
        /// <summary>
        ///     Sets an user status.
        /// </summary>
        /// <param name="status">Status entity.</param>
        public async Task SetStatusAsync(TwitterStatus status)
        {
            // Pass your credentials to the service
            string consumerKey = _settings.TwitterConsumerKey;
            string consumerSecret = _settings.TwitterConsumerSecret;

            // Authorize
            var service = new TwitterService(consumerKey, consumerSecret);
            service.AuthenticateWith(status.Token, status.TokenSecret);

            // Send message
            TweetSharp.TwitterStatus result;
            if (string.IsNullOrEmpty(status.ScreenshotUrl))
            {
                var tweet = new SendTweetOptions { Status = status.Message };
                result = service.SendTweet(tweet);
            }
            else
            {
                using (var httpClient = new HttpClient())
                {
                    HttpResponseMessage response = await httpClient.GetAsync(status.ScreenshotUrl);
                    if (!response.IsSuccessStatusCode)
                    {
                        throw new BadRequestException(response.ReasonPhrase);
                    }

                    Stream stream = await response.Content.ReadAsStreamAsync();
                    var tweet = new SendTweetWithMediaOptions
                    {
                        Status = status.Message,
                        Images = new Dictionary<string, Stream>
                        {
                            { "media", stream }
                        }
                    };

                    result = service.SendTweetWithMedia(tweet);
                }
            }

            // Check result status
            if (result != null)
            {
                return;
            }

            // Check response status code
            switch (service.Response.StatusCode)
            {
                case HttpStatusCode.Unauthorized:
                case HttpStatusCode.BadRequest:
                    // Invalid credentials or request data
                    throw new BadRequestException(service.Response.Response);

                case HttpStatusCode.Forbidden:
                    throw new ForbiddenException(service.Response.Response);

                case (HttpStatusCode)429:
                    throw new TooManyRequestsException(service.Response.Response);
            }

            // Twitter internal errors
            if ((int)service.Response.StatusCode >= 500)
            {
                throw new BadGatewayException(service.Response.Response);
            }

            string message = string.Format("Unable to send tweet. Status code {0}: {1}", service.Response.StatusCode, service.Response);
            throw new InternalServerErrorException(message);
        }
Пример #2
0
        /// <summary>
        ///     Sets an user status.
        /// </summary>
        /// <param name="status">Status entity.</param>
        public async Task SetStatusAsync(TwitterStatus status)
        {
            // Pass your credentials to the service
            string consumerKey    = _settings.TwitterConsumerKey;
            string consumerSecret = _settings.TwitterConsumerSecret;

            // Authorize
            var service = new TwitterService(consumerKey, consumerSecret);

            service.AuthenticateWith(status.Token, status.TokenSecret);

            // Send message
            TweetSharp.TwitterStatus result;
            if (string.IsNullOrEmpty(status.ScreenshotUrl))
            {
                var tweet = new SendTweetOptions {
                    Status = status.Message
                };
                result = service.SendTweet(tweet);
            }
            else
            {
                using (var httpClient = new HttpClient())
                {
                    HttpResponseMessage response = await httpClient.GetAsync(status.ScreenshotUrl);

                    if (!response.IsSuccessStatusCode)
                    {
                        throw new BadRequestException(response.ReasonPhrase);
                    }

                    Stream stream = await response.Content.ReadAsStreamAsync();

                    var tweet = new SendTweetWithMediaOptions
                    {
                        Status = status.Message,
                        Images = new Dictionary <string, Stream>
                        {
                            { "media", stream }
                        }
                    };

                    result = service.SendTweetWithMedia(tweet);
                }
            }

            // Check result status
            if (result != null)
            {
                return;
            }

            // Check response status code
            switch (service.Response.StatusCode)
            {
            case HttpStatusCode.Unauthorized:
            case HttpStatusCode.BadRequest:
                // Invalid credentials or request data
                throw new BadRequestException(service.Response.Response);

            case HttpStatusCode.Forbidden:
                throw new ForbiddenException(service.Response.Response);

            case (HttpStatusCode)429:
                throw new TooManyRequestsException(service.Response.Response);
            }

            // Twitter internal errors
            if ((int)service.Response.StatusCode >= 500)
            {
                throw new BadGatewayException(service.Response.Response);
            }

            string message = string.Format("Unable to send tweet. Status code {0}: {1}", service.Response.StatusCode, service.Response);

            throw new InternalServerErrorException(message);
        }