tags:

views:

1071

answers:

6

Hello,

I'm updating my user's Twitter status using the simple code below. My problem is that the Twitter updates resulting from this code are denoted as ".... ago from API". I would like to be able to control the "API" part to say a custom name. How can I do it? Preferably using an alteration to the code below...

Thanks

/*
 * A function to post an update to Twitter programmatically
 * Author: Danny Battison
 * Contact: [email protected]
 */

/// <summary>
/// Post an update to a Twitter acount
/// </summary>
/// <param name="username">The username of the account</param>
/// <param name="password">The password of the account</param>
/// <param name="tweet">The status to post</param>
public static void PostTweet(string username, string password, string tweet)
{
    try
    {
        // encode the username/password
        string user = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
        // determine what we want to upload as a status
        byte[] bytes = System.Text.Encoding.ASCII.GetBytes("status=" + tweet);
        // connect with the update page
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://twitter.com/statuses/update.xml");
        // set the method to POST
        request.Method = "POST";
        request.ServicePoint.Expect100Continue = false; // thanks to argodev for this recent change!
        // set the authorisation levels
        request.Headers.Add("Authorization", "Basic " + user);
        request.ContentType = "application/x-www-form-urlencoded";
        // set the length of the content
        request.ContentLength = bytes.Length;

        // set up the stream
        Stream reqStream = request.GetRequestStream();
        // write to the stream
        reqStream.Write(bytes, 0, bytes.Length);
        // close the stream
        reqStream.Close();
    }
    catch (Exception ex) 
    {
        // Log the error
    }
}
A: 

You might want to read this article: Coding the Tweet by James Devlin.

Rasmus Faber
+2  A: 

Just a small suggestion, you might want to take a look at the TweetSharp library, which could make your life easier rather than mucking around yourself at the REST API level.

Twitter has a list of other libraries you could use for C# here, if you don't like the look of TweetSharp.

Daniel Chambers
+3  A: 

The only way to get "from MyApp" is to use OAuth and set it up through your Twitter developer account. You used to be able to send an HTTP POST parameter with your request (source) but it's since been deprecated.

Daniel Crenna
A: 

You have to register you APP to change this name.

+1  A: 

Please register your app here http://twitter.com/apps/new

Once they approve you can pass a parameter 'source' to get your tweets marked as from your app.

mixdev
A: 

I have registered my App and they have approved it. Now how can I set the source name for status updates ? I am using BasicAuth.

Thanks, Suren

Surendran