How to make a post request to Twitter in C# to add people to follow
Ok many developer problems can be avoid by reading all the documentation, yet, I normally try to jump into writing code as soon as I can, and figure it out by myself. This method got me in a lot of trouble and made me waste my time as well.
Twitter is really peculiar on how wants the post to follow and leave people. After playing for a while I was successful so I would like to share with you how to do that as the twitter API documentation does not explain it. Yet won’t be difficult for you to figure it reading all the documentation page.
Friendship Methods
create
Befriends the user specified in the ID parameter as the authenticating user. Returns the befriended user in the requested format when successful. Returns a string describing the failure condition when unsuccessful.
URL: http://twitter.com/friendships/create/id.format
Formats: xml, json
Method(s): POST
Parameters:
- id. Required. The ID or screen name of the user to befriend. Ex: http://twitter.com/friendships/create/12345.json or http://twitter.com/friendships/create/bob.xml
- follow. Optional. Enable notifications for the target user in addition to becoming friends. Ex: http://twitter.com/friendships/create/bob.json?follow=true
Make sure you read the authentication section as well to find out how to send your credentials and then how to create the request. Setting up the request took me longer than expected, I needed to set the all this in order to work, otherwise will return a 417 error.
request.Method = "POST"; request.ContentType = "application/xml"; request.AllowWriteStreamBuffering = true; request.UserAgent = "GeoTwitter"; request.Headers.Add("Authorization", "Basic " + Credentials);
request.ServicePoint.Expect100Continue = false; To create the credentials is pretty simple:
Convert.ToBase64String(Encoding.ASCII.GetBytes(UserName + ":" + Password));
That will return a string with your username and password in base64 to added into the header.
The data for the post is empty. Happy Twitting.
Cheers
Al
Follow me in twitter | bookmark me | Subscribe to my feed | Check out GeoTwitter | Create GeoRss content
Posted from http://weblogs.asp.net/albertpascual



Comments
Bashar Kokash said on 12.30.2008 at 4:28 AM
Thanks,
Community Blogs said on 12.31.2008 at 3:01 AM
I have been playing with the Bot to get some information out of Twitter, the main problem the restriction