コード例 #1
0
        /// <summary>
        /// Post tweet
        /// </summary>
        /// <param name="type">Account type to post to</param>
        /// <param name="status">Text to post</param>
        public static void PostStatus(TwitterType type, string status)
        {
            Log.InfoFormat("Sending status to {0}: {1}", type, status);
            if (_twitterServices == null)
            {
                Log.Warn("We didn't log into Twitter, skipping.");
                return;
            }
            var response = _twitterServices[type].SendTweet(new SendTweetOptions {
                Status = status
            });
            var ratestatus = _twitterServices[type].Response.RateLimitStatus;

            Log.Info("Sent Twitter status.");
        }
コード例 #2
0
        public Twitter(TwitterType tp)
        {
            ConsumerCredentials = new MyConsumerCredentials();

            tType = tp;

            // Do the Authenticate
            var oAuthUrl = Constants.OAUTHURL;

            // You need to set your own keys and screen name
            switch (tType)
            {
            case TwitterType.Unfollow:
                authHeader = string.Concat(Constants.BASIC, Constants.SPACE,
                                           Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(ConsumerCredentials.oauth_consumer_key) + Constants.COLON +
                                                                                         Uri.EscapeDataString((ConsumerCredentials.oauth_consumer_secret)))
                                                                  ));
                break;

            case TwitterType.Friends:
                authHeader = string.Concat(Constants.BASIC, Constants.SPACE,
                                           Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(ConsumerCredentials.oauth_consumer_key) + Constants.COLON +
                                                                                         Uri.EscapeDataString((ConsumerCredentials.oauth_consumer_secret)))
                                                                  ));
                break;

            case TwitterType.TimeLine:
                authHeader = string.Concat(Constants.BASIC, Constants.SPACE,
                                           Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(ConsumerCredentials.oauth_consumer_key) + Constants.COLON +
                                                                                         Uri.EscapeDataString((ConsumerCredentials.oauth_consumer_secret)))
                                                                  ));
                break;

            case TwitterType.ByTweetId:
                authHeader = string.Concat(Constants.BASIC, Constants.SPACE,
                                           Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString(ConsumerCredentials.oauth_consumer_key) + Constants.COLON +
                                                                                         Uri.EscapeDataString((ConsumerCredentials.oauth_consumer_secret)))
                                                                  ));
                break;
            }
            var postBody = Constants.POSTBODY;

            HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl);

            authRequest.Headers.Add(Constants.AUTHORIZATION, authHeader);
            authRequest.Method                 = Constants.POST;
            authRequest.ContentType            = Constants.CONTENTTYPE;
            authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            using (Stream stream = authRequest.GetRequestStream()) {
                byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
                stream.Write(content, 0, content.Length);
            }
            authRequest.Headers.Add(Constants.ACCEPTENCODING, Constants.GZIP);
            WebResponse authResponse = authRequest.GetResponse();

            // deserialize into an object
            using (authResponse) {
                using (var reader = new StreamReader(authResponse.GetResponseStream())) {
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    var objectText          = reader.ReadToEnd();
                    twitAuthResponse = JsonConvert.DeserializeObject <TwitAuthenticateResponse>(objectText);
                }
            }
        }
コード例 #3
0
        public Twitter(Timeline timeline, TwitterType tp)
        {
            Credentials = new MyDestroyCredentials();

            // oauth implementation details
            var oauth_version          = "1.0";
            var oauth_signature_method = "HMAC-SHA1";

            // unique request details
            var oauth_nonce     = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
            var timeSpan        = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();

            string destroycreate = "https://api.twitter.com/1.1/statuses/destroy/{0}.json";

            // message api details
            string resource_url = string.Empty;

            switch (tp)
            {
            case TwitterType.TimeLine:
                resource_url = string.Format(destroycreate, timeline.ID);
                break;
            }

            // create oauth signature
            var baseFormat = "oauth_consumer_key={0}&" +
                             "oauth_nonce={1}&" +
                             "oauth_signature_method={2}&" +
                             "oauth_timestamp={3}&" +
                             "oauth_token={4}&" +
                             "oauth_version={5}";

            var baseString = string.Format(baseFormat,
                                           Credentials.oauth_consumer_key,
                                           oauth_nonce,
                                           oauth_signature_method,
                                           oauth_timestamp,
                                           Credentials.oauth_token,
                                           oauth_version
                                           );

            baseString = string.Concat("POST&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString));

            var compositeKey = string.Concat(Uri.EscapeDataString(Credentials.oauth_consumer_secret),
                                             "&", Uri.EscapeDataString(Credentials.oauth_token_secret));

            string oauth_signature;

            using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey))) {
                oauth_signature = Convert.ToBase64String(
                    hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
            }

            // create the request header
            var headerFormat = "OAuth oauth_consumer_key=\"{0}\", " +
                               "oauth_nonce=\"{1}\", " +
                               "oauth_signature=\"{2}\", " +
                               "oauth_signature_method=\"{3}\", " +
                               "oauth_timestamp=\"{4}\", " +
                               "oauth_token=\"{5}\", " +
                               "oauth_version=\"{6}\"";

            authHeader = string.Format(headerFormat,
                                       Uri.EscapeDataString(Credentials.oauth_consumer_key),
                                       Uri.EscapeDataString(oauth_nonce),
                                       Uri.EscapeDataString(oauth_signature),
                                       Uri.EscapeDataString(oauth_signature_method),
                                       Uri.EscapeDataString(oauth_timestamp),
                                       Uri.EscapeDataString(Credentials.oauth_token),
                                       Uri.EscapeDataString(oauth_version)
                                       );

            // make the request
            ServicePointManager.Expect100Continue = false;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);

            request.Headers.Add("Authorization", authHeader);
            request.Method      = "POST";
            request.ContentType = "application/x-www-form-urlencoded";

            WebResponse response     = request.GetResponse();
            string      responseData = new StreamReader(response.GetResponseStream()).ReadToEnd();

            response.Close();
        }