
CHALLENGE: Try working with the Twitter API to build an application that simplifies a specific task of your own.
INTRODUCE THE LANGUAGE/TECHNOLOGY: Twitter grows as a service exponentially, so does the number of applications that are using Twitter to achieve various goals. Countless Twitter clients, web applications that post tweets from blogs and whatnot. I've been working extensively with the Twitter API for a few months and I can say that it is not that hard as it seems, if you dive into it. I've released two projects of mine, that are using the Twitter API as their foundation (dotTweet - a Twitter API wrapper for .NET and PerformanceTweet - an application that sends computer performance reports via Twitter).
IDEAS:
- An application that reads a specific RSS feed and posts links to articles found there on Twitter
- An application that informs others about the weather in your city
- An application that automatically retweets posts with a specific hashtag
- An application that searches for tweets containing a specific phrase
There are more resources for Twitter API developers than you think. Even here on DIC, we do have some interesting code snippets and tutorials. When you start, it is a good idea to look at the official Twitter API documentation. In this way, you will get yourself familiar with what the Twitter API can do and how it is implemented.
There are some tips for those who want to use the Twitter API as well.
If you are a C# (or .NET) developer, you might want to take a look at these code snippets:
Post an update to a Twitter account
Get Twitter user image URL
Twitter API: Create friendship
Twitter API: Destroy direct message
Twitter API: Destroy status
Twitter API: Get global updates
Twitter API: Get mentions
Twitter API: Get rate limit status
Twitter API: Get recent public timeline
Twitter API: Get trending topics
Twitter API: Get user mentions
Twitter API: Send direct message
Twitter API: Update profile image
Twitter API: Update profile info
For Ruby developers, take a look at this code snippet that retrieves the list of public statuses.
For Java developers, take a look at Twitter4J - a library that allows you to access the Twitter API without any additionals dependencies (JARs). Or, there is JTwitter.
If you are thinking about building an automated Twitter bot, then take a look at this article.
For Haskell guys, there is hs-twitter. And of course, for Clojure users there is clojure-twitter (thanks to Raynes for providing the link).
For those developers who use Python, there is python-twitter. And there is Python Twitter Tools.
Twitter API calls can also be implemented in Perl thanks to Net::Twitter (link provided by dsherohman).
Now, if you feel like you need a tutorial, here is one for the PHP guys:
Building an Automated Twitter Bot with Twitters API functionality
If you are interested in using Twitter with OAuth in PHP, definitely check out this topic.
If you don't feel like writing every single Twitter API call from scratch (not me), take a look at TweetSharp, which is another good Twitter API wrapper for .NET developers. Sometimes it is also good to check out the Twitter Development Talk group as the primary source of Twitter API updates and implementation samples.
HOW TO GET STARTED:
Basically all you need to get started you already have. An IDE (or text editor) for your language of choice is going to be enough. You will need some knowledge about what XML, ATOM and JSON are. Although you don't need to be an expert in these, it will speed up your work a bit if you will know the fundamentals. You will also need some experience working with HTTP requests. The rest of the Twitter API is mainly about correctly building these request and processing the responses accordingly.
To show you how easy it is to implement some Twitter API calls, take a look at these C# code samples.
Here is how to verify whether the friendship beetween two users exists (User A is following User B ):
/// <param name="username">Twitter account username.</param>
/// <param name="password">Twitter account password.</param>
/// <param name="userA">The main user.</param>
/// <param name="userB">The user to check for friendship.</param>
public static bool Friendship_Exists(string username, string password, string userA, string userB)
{
try
{
XmlUrlResolver xmlResolver = new XmlUrlResolver();
xmlResolver.Credentials = new NetworkCredential(username, password);
XmlReaderSettings xmlSettings = new XmlReaderSettings();
xmlSettings.XmlResolver = xmlResolver;
XmlReader feedReader = XmlReader.Create("http://twitter.com/friendships/exists.xml?user_a=" + userA + "&user_b=" + userB, xmlSettings);
XmlDocument xmlDocument = new XmlDocument();
xmldocument.Load(feedReader);
string friends = xmldocument.SelectSingleNode("/friends").InnerText;
bool rValue = Convert.ToBoolean(friends);
return rValue;
}
catch
{
return null;
}
}
It simply gets the credentials the user inserts and retrieves the boolean value from the generated XML file. As you see, there are no complicated processes involved.
Now, here is another sample. Let's suppose that you wrote a Twitter status update you don't like. What about deleting it? This is deon in C# this way:
/// <summary>
/// Destroys the status specified by the required ID parameter.
/// The authenticating user must be the author of the specified status.
/// http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-statuses%C2%A0destroy
/// </summary>
/// <param name="username">Twitter account username.</param>
/// <param name="password">Twitter account password.</param>
/// <param name="id">Twitter status ID.</param>
public static string Statuses_Destroy(string username, string password, string id)
{
try
{
WebRequest request = HttpWebRequest.Create("http://twitter.com/statuses/destroy/" + id + ".xml");
request.Method = "DELETE";
request.ContentLength = 0;
request.Credentials = new NetworkCredential(username, password);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader responseStream = new StreamReader(response.GetResponseStream());
string responseString = responseStream.ReadToEnd();
return responseString;
}
catch (Exception ex)
{
return ex.Message;
}
}
Once you specify the status ID and the credentials, it will be removed.

New Topic/Question
Reply



MultiQuote












|