Make a Visual C# program tweet to
You will need:
1. Microsoft Visual C# Express or not (2008 recommended)
2. Internet
3. Use to any Twitter.com account
4. Some skill with C#
1. Open C#
2. Make a new project.
3. Select "Windows Forms Application"
4. Name your project something like "Tweeting Application" Anything without the word "twitter" in it. (Really, don't put "twitter" in the name)
This seems like an easy tutorial, huh? Not!
5. Insert 3 Textboxes. Name them these:
usernameBox
passwordBox
tweetBox
Edit the textboxes. Now, we're not making them save, that's not needed now.
5a. In the properties, set the passwordBox's "PasswordChar" to * (shift + 8)
5b. Set the tweetBox's "MultiLine" to True, and make the box taller.
6. Add a button below the tweet box, name it "tweetButton", and set it's text to "Update status".
Random: Make a group box and put the tweetBox into it, and set the group box's Text to "What are you doing?" just to be funny.
You don't have to.
Here it comes! The "Fun" part has come! Coding!
7. Double-click the tweetButton. You will see a code editor.
Warning! Check where it says stuff like "using System.blahblah;". Insert the following below all the "using" lines:
using System.Net; using System.Web; using System.IO;
If you do NOT include those, the tweeting will not work.
8. Do NOT edit anything that says this yet:
private void tweetButton_Click(object sender, EventArgs e)
{
}Below this part:
public Form1()
{
InitializeComponent();
}Insert these lines of code: (Warning: Very long) All lines needed, don't edit/miss anything in these
/// <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;
// 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)
{
MessageBox.Show("An error occured when posting your tweet.\n\nError: " + ex.Message);
}
}*phew* We got the important part done...
9. Now where it says this:
private void tweetButton_Click(object sender, EventArgs e)
{
}Type in something to make it look like this:
private void tweetButton_Click(object sender, EventArgs e)
{
PostTweet(usernameBox.Text, passwordBox.Text, tweetBox.Text);
}10. Type in a Twitter.com username into the usernameBox, password into the passwordBox, and text into the tweetBox, then send it by pressing the tweet button!
Check twitter.com, and look.
Comments? Questions? Post below!
Last edited by ihaveamac (2009-08-30 03:11:58)
Offline
Offline
how do you do it in python?
Offline
hirandomperson wrote:
how do you do it in python?
i don't know, google it. i only know how to do it for C#
Offline
i figered out
Offline