C# code to publish, delete, retrieve tweets using oauth

The following C#.net class can be used to update statuses, delete, retweet statuses, obtain information regarding a particular status and any body who retweeted a particular status using status id.

This class implements all the methods that are under the “Tweets Resources” section in Twitter API documentation

 

using System;
using System.Text;
using System.Collections.Generic;

namespace TwitterAPI
{
    public class Tweets
    {
        #region Class-Level-Declarations

        private oAuthTwitter OAuth;
        /// <summary>
        /// Create, Update, retrieve, delete tweets(status messages) using this class
        /// </summary>
        /// <param name="_oauth">An authorized and authenticated oAuth token</param>
        public Tweets(oAuthTwitter _oauth)
        {
            this.OAuth = _oauth;
        }
        public enum ResponseFormat { JSON, XML };

        #endregion

        #region Show:ID

        /// <summary>
        /// Returns a single status, specified by the id parameter below. The status's author will be returned inline.
        /// This does not require authentication as long as the status is not protected
        /// This is a rate limited call
        /// </summary>
        /// <param name="response_format">The format in which you want twitter to respond</param>
        /// <param name="statusid">The numerical ID of the desired status.</param>
        /// <param name="optionalparameters">Any other optional parameters.Use an empty string if you dont want to pass any optional parameters</param>
        /// <returns>Response string from twitter in user selected format</returns>
        public string Show_By_ID(ResponseFormat response_format, string statusid, string optionalparameters)
        {
            if (string.IsNullOrEmpty(statusid))
                throw new ArgumentNullException(statusid, "Status Id cannot be null");
            return OAuth.oAuthWebRequest(oAuthTwitter.Method.GET, "http://api.twitter.com/1/statuses/show/" + statusid + "." + response_format.ToString(), optionalparameters);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the authenticating user's status. A status update with text identical to the authenticating user's current status will be ignored to prevent duplicates.
        /// Authentication is required and this call is not rate limited
        /// </summary>
        /// <param name="tweet_message">The text of your status update, up to 140 characters.</param>
        /// <param name="reponse_format">The format in which you want twitter to respond</param>
        /// <param name="optionalparameters">Any optional paramters you want to pass</param>
        /// <returns>Response string from twitter in user selected format </returns>
        public string UpdateStatus(ResponseFormat reponse_format, string tweet_message, string optionalparameters)
        {
            if (string.IsNullOrEmpty(tweet_message))
                throw new ArgumentNullException(tweet_message, "The status message cannot be null");
            return OAuth.oAuthWebRequest(oAuthTwitter.Method.POST, "http://api.twitter.com/1/statuses/update." + reponse_format.ToString(), "status=" + tweet_message + optionalparameters);
        }

         #endregion

        #region Destroy:Id

        /// <summary>
        /// Destroys the status specified by the required ID parameter.In other words deletes the specified tweet
        /// Requires authentication, and rate limited is false
        /// </summary>
        /// <param name="response_format">The format in which you want twitter to respond</param>
        /// <param name="statusid">The numerical ID of the desired status.</param>
        /// <param name="optionalparameters">Any other optional parameters.Use an empty string if you dont want to pass any optional parameters</param>
        /// <returns>Response string from twitter in user selected format</returns>
        public string Destroy_By_Id(ResponseFormat response_format, string statusid, string optionalparameters)
        {
            if (string.IsNullOrEmpty(statusid))
                throw new ArgumentNullException(statusid, "Status Id cannot be null");
            return OAuth.oAuthWebRequest(oAuthTwitter.Method.POST, "http://api.twitter.com/1/statuses/destroy/" + statusid + "." + response_format.ToString(), optionalparameters);
        }

        #endregion

        #region Retweet:Id

        /// <summary>
        /// Retweets a tweet. Returns the original tweet with retweet details embedded.
        /// Does not require authentication, and rate limited is false
        /// </summary>
        /// <param name="response_format">The format in which you want twitter to respond</param>
        /// <param name="statusid">The numerical ID of the desired status.</param>
        /// <param name="optionalparameters">Any other optional parameters.Use an empty string if you dont want to pass any optional parameters</param>
        /// <returns>Response string from twitter in user selected format</returns>
        public string Retweet_By_Id(ResponseFormat response_format, string statusid, string optionalparameters)
        {
            if (string.IsNullOrEmpty(statusid))
                throw new ArgumentNullException(statusid, "Status Id cannot be null");
            return OAuth.oAuthWebRequest(oAuthTwitter.Method.POST, "http://api.twitter.com/1/statuses/retweet/" + statusid + "." + response_format.ToString(), optionalparameters);
        }

        #endregion

        #region Show Retweets:Id

        /// <summary>
        ///Returns up to 100 of the first retweets of a given tweet.
        /// Does not require authentication, and rate limited is false
        /// </summary>
        /// <param name="response_format">The format in which you want twitter to respond</param>
        /// <param name="statusid">The numerical ID of the desired status.</param>
        /// <param name="optionalparameters">Any other optional parameters.Use an empty string if you dont want to pass any optional parameters</param>
        /// <returns>Response string from twitter in user selected format</returns>
        public string Show_Retweets_By_Id(ResponseFormat response_format, string statusid, string optionalparameters)
        {
            if (string.IsNullOrEmpty(statusid))
                throw new ArgumentNullException(statusid, "Status Id cannot be null");
            return OAuth.oAuthWebRequest(oAuthTwitter.Method.GET, "http://api.twitter.com/1/statuses/retweets/" + statusid + "." + response_format.ToString(), optionalparameters);
        }

        #endregion

        #region Show Retweeted By:Id

        /// <summary>
        /// Show user objects of up to 100 members who retweeted the status.
        /// Requires authentication, and rate limited
        /// </summary>
        /// <param name="response_format">The format in which you want twitter to respond</param>
        /// <param name="statusid">The numerical ID of the desired status.</param>
        /// <param name="optionalparameters">Any other optional parameters.Use an empty string if you dont want to pass any optional parameters</param>
        /// <returns>Response string from twitter in user selected format</returns>
        public string Show_Retweetedby_By_Id(ResponseFormat response_format, string statusid, string optionalparameters)
        {
            if (string.IsNullOrEmpty(statusid))
                throw new ArgumentNullException(statusid, "Status Id cannot be null");
            return OAuth.oAuthWebRequest(oAuthTwitter.Method.GET, "http://api.twitter.com/1/statuses/" + statusid + "/retweeted_by." + response_format.ToString(), optionalparameters);
        }

        #endregion

        #region Show Retweeted By:Id

        /// <summary>
        /// Show user ids of up to 100 users who retweeted the status.
        /// Requires authentication, and rate limited
        /// </summary>
        /// <param name="response_format">The format in which you want twitter to respond</param>
        /// <param name="statusid">The numerical ID of the desired status.</param>
        /// <param name="optionalparameters">Any other optional parameters.Use an empty string if you dont want to pass any optional parameters</param>
        /// <returns>Response string from twitter in user selected format</returns>
        public string Show_Retweetedby_By_Id(ResponseFormat response_format, string statusid, string optionalparameters)
        {
            if (string.IsNullOrEmpty(statusid))
                throw new ArgumentNullException(statusid, "Status Id cannot be null");
            return OAuth.oAuthWebRequest(oAuthTwitter.Method.GET, "http://api.twitter.com/1/statuses/" + statusid + "/retweeted_by/ids." + response_format.ToString(), optionalparameters);
        }        #endregion
    }
}

 

This class file uses OAuth implementation by shannon whitley (for more information see my previous post Implementing oauth twitter search in C# and JSON).

You can download the complete source code along with this class from https://sites.google.com/site/coderbuddy/downloads/TwitterAPI.zip?attredirects=0&d=1

TwitterAPI.zip (43,25 kb)