4

I don't want to do anything fancy on Twitter except post to it via my site once a day. I have searched around a bit and there are all sorts of super-complex ways to do every little thing that Twitter does, but there seems to be little documentation on how to do the simplest thing, which is make a post!

Does anyone know how to do this? Or can you at least point me in the right direction? I don't need full wrappers or anything (http://apiwiki.twitter.com/Libraries#C/NET), just one simple function that will post to Twitter.

Thanks!

Jason
  • 51,583
  • 38
  • 133
  • 185
  • Do you know how to make REST calls via ASP.NET? – dirkgently May 11 '09 at 21:21
  • I don't know ASP.NET either. However, that's something you should Google for. The next step would be to simply replace the URLs. – dirkgently May 11 '09 at 21:27
  • i have googled it, that's why i'm here... there's no real clear answer. could you give me your solution in your language of choice and i'll see if i can translate? thanks :) – Jason May 11 '09 at 21:32

4 Answers4

4

This is the easiest implementation ever. Up and running in under 2 minutes: Twitterizer

Sled
  • 18,541
  • 27
  • 119
  • 168
Jason
  • 51,583
  • 38
  • 133
  • 185
  • FYI, this uses option 1 from my answer. In future projects I would take a look at WCF if your using .NET v3+ – bendewey May 12 '09 at 01:15
  • NOTE: TWITTERIZER DOES NOT WORK IN MEDIUM TRUST ENVIRONMENTS... CONSIDER YOURSELF WARNED – Jason Jun 05 '09 at 21:14
  • Twitterer has been discontinued - and several dependencies no longer work with the it. Any alternatives? – niico Dec 06 '14 at 19:42
0

There are a couple different ways of doing this, they vary depending on the tools you want to use and have access to. Option 1 will work right out of the box, but the coding can be complicated. Option 3 you will have to download tools for, but once there installed and loaded you should be able to consume the twitter api very quickly.

  1. Use WebRequest.Create to create/send messages to remote endpoints
  2. Use WCF, create a mirror endpoint and access the twitter api using client only endpoint.
  3. Use the WCF REST Starter Kit Preview 2, which has a new class called the HttpClient. I would have to recommend this technique if you can. Here is a great video Consuming a REST Twitter Feed in under 3 minutes.

Here is a sample of using the WCF REST Starter Kit's HttpClient:

    public void CreateFriendship(string friend)
    {
        using (var client = new HttpClient())
        {
            var url = string.Format("http://www.twitter.com/friendships/create/{0}.xml?follow=true", friend);
            client.Post(url)
                .CheckForTwitterError()
                .EnsureStatusIs(HttpStatusCode.OK);
        }
    }

Add a comment if you'd like more info about a particular method.

Update:

For Option #1 see this question: Remote HTTP Post with C#

Community
  • 1
  • 1
bendewey
  • 39,709
  • 13
  • 100
  • 125
  • yes, i would. i don't care about friends. i want to update my status! – Jason May 11 '09 at 21:50
  • See my update. Let me know how that helps you, if not, I can provide some code. – bendewey May 11 '09 at 22:09
  • no, it doesn't really. i need to know twitter-specific stuff... i don't know anything about REST or webrequests or anything, although i have learned a lot about it today! is there any way i could just get a simple function that posts to twitter? that's it, really: Sub UpdateTwitterStatus(status as string) ... End Sub – Jason May 11 '09 at 22:27
0

Its fairly simple; you just need to post an xml file to a web page using webrequest.create. This example is close (assumes you have the xml for the message in another place and just pass it into twitterxml variable as a string. The url might not be the right one; found it on this [page][1] which defines the interface

WebRequest req = null;
   WebResponse rsp = null;
   try
   {
    string twitterXML = "xml as string";
    string uri = "http://twitter.com/statuses/update.format";
    req = WebRequest.Create(uri);
    //req.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy
    req.Method = "POST";        // Post method
    req.ContentType = "text/xml";     // content type
    // Wrap the request stream with a text-based writer
    StreamWriter writer = new StreamWriter(req.GetRequestStream());
    // Write the XML text into the stream
    writer.WriteLine(twitterXML);
    writer.Close();
    // Send the data to the webserver
    rsp = req.GetResponse();

   }

[1]: http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses update

u07ch
  • 13,324
  • 5
  • 42
  • 48
  • this helps take me some of the way, but what is the format for your twitterXML variable? i can't just sent them "status update!" as a string... i'm sure they need it in some format – Jason May 11 '09 at 22:04
0

There are a few ways of doing this, you can check out http://restfor.me/twitter and it will give you the code from RESTful documentation.

Essentially making any authenticated call you can follow this logic:


        /// 
        /// Executes an HTTP POST command and retrives the information.     
        /// This function will automatically include a "source" parameter if the "Source" property is set.
        /// 
        /// The URL to perform the POST operation
        /// The username to use with the request
        /// The password to use with the request
        /// The data to post 
        /// The response of the request, or null if we got 404 or nothing.
        protected string ExecutePostCommand(string url, string userName, string password, string data) {
            WebRequest request = WebRequest.Create(url);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";
            if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password)) {
                request.Credentials = new NetworkCredential(userName, password);



                byte[] bytes = Encoding.UTF8.GetBytes(data);

                request.ContentLength = bytes.Length;
                using (Stream requestStream = request.GetRequestStream()) {
                    requestStream.Write(bytes, 0, bytes.Length);

                    using (WebResponse response = request.GetResponse()) {
                        using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
                            return reader.ReadToEnd();
                        }
                    }
                }
            }

            return null;
        }
Sheraz
  • 569
  • 5
  • 11