This edition of techblog guides you to develop a desktop client for twitter. For this tutorial, I’m going to use Twitter4J API which you can download from the Google code project page of this blog. You will also need an SQLite driver for storing the data in an SQLite database. You can get this from here.
Before we proceed with the development side, you need to register the application, that you are going to develop, with twitter. Earlier this step was not mandatory. You can ask the user to provide the username and password and use this information to authenticate. But twitter is no longer support this. Now you need to use OAuth for communicating and exchanging data with the server. This means that the user doesn’t need to provide you with their username and password.
All they (users) have to do is to allow the new application to access their twitter profile (directly @ twitter website) and provide the application with the authentication code.
Having said that, go to this page and register your application with twitter. For demonstrating this tutorial, I have created an application named “Twit Client for Linux”. Please note that even though the name says it is for Linux, it is works on all platforms as it based on Java.
You need to provide some details while registering the application. They are pretty much self explanatory. But here are two things you need to note:
1. The application should be registered as ‘Client’ application (since we are going to build a standalone twitter client).
2. You should select “Read & Write” as the access type.
About the Application
This section provides a preview of the application that we are going to write.
The screenshot shown below is the application that we are going to build. When the user starts the application for the first time, they will see a window similar (this will be a one-time only window and we will not ask for these details again, once the user has provided them) to the one shown below:
At the same time itself the default browser of the user will open up a new URL (attached to twitter.com) and ask them to sign in to twitter (if they are not signed in already).
Then the user will get a chance to allow the client to access their profile. User needs to click “Allow” here:
Then, twitter will give the user a code number (called PIN).
He needs to give this PIN number to the application (as shown in the screenshot of the application). Once he has done that, the application will refresh the window and will display something similar to the one shown below:
The application will list all your followers and the tweets from the people whom you follow. Please note that you also list the tweets of your followers as well.
The application will also allow you to post new tweets to your profile.
Please note that the application provides only the basic features. You can add more feature like:
- Reply to a tweets
- List retweets
- Send messages
- Update your profile
You can do this in the same way as you are going to do the basic stuffs. Since you are using the twitter4j API, the procedures are pretty much the same. All you need to do is to spend some more time!
Also, while posting a tweet from your application you can see that twitter is actually displaying the name of your application there !
Let’s start coding
The first thing that we need to worry about is the data that we need to store. For this, we are going to use SQLite. SQLite allows you to create small databases (which are not very ‘heavy’). This means that the user need not worry about installing any database software for running this application.
Let’s write the code first:
package twitterapp;import java.sql.*;import twitter4j.http.AccessToken;/**** @author vinayak*/public class AddAccount {public void addUser( String username,String password, String token, String tokensecret ) throws Exception{Class.forName("org.sqlite.JDBC");Connection conn = DriverManager.getConnection("jdbc:sqlite:ac.db");Statement stat = conn.createStatement();stat.executeUpdate("create table accounts(uname, tok, toksec);");PreparedStatement prep = conn.prepareStatement("insert into accounts values (?,?,?);");prep.setString(1, username);prep.setString(2, token);prep.setString(3, tokensecret);prep.addBatch();conn.setAutoCommit(false);prep.executeBatch();conn.setAutoCommit(true);//for debuggingconn.close();}}
You can see that we are creating a new database (if that is not present there) named ‘ac.db’ in the current directory (that of the application). Then we create a table named ‘accounts’ with three columns (uname, tok and toksec). Here uname is the username, tok is the PIN number and toksec is the secret key that twitter provides you when you authenticate the user using the PIN provided, for the first time. We need this secret to access the twitter account in future.
You can also see that we are using prepare statement method to prevent SQL injection. This is a topic which I discussed under security issues. This code will allow us to store the information. Since you are not asking for the password, there is no need to encrypt these information. But if you want to have added security you can add that feature as well. Also, you can see an unused import here – well, I stored the auth token as an object initially and later found that it is better to store that as a string!
Now let’s us create another class that we can use when the application starts up every time. This class will be used to read the account details (and the code is pretty much self explanatory).
package twitterapp;import java.sql.*;import twitter4j.http.AccessToken;/**** @author vinayak*/public class ReadAccount {public String[] Read() throws ClassNotFoundException, SQLException{Class.forName("org.sqlite.JDBC");Connection conn = DriverManager.getConnection("jdbc:sqlite:ac.db");Statement stat = conn.createStatement();ResultSet rs = stat.executeQuery("select * from accounts;");String[] ac = new String[5] ;while (rs.next()) {ac[0]= rs.getString("uname");ac[1]=rs.getString("pass");ac[2]=rs.getString("tok");ac[3]=rs.getString("toks");}rs.close();conn.close();return ac;}}
This code will check the account details so that the application can try to authenticate with twitter service. If the authentication fails, then the login window (asking for PIN) will be shown to user and the user will have to authenticate again (if the information provided in the earlier case was wrong).
You can find the second part of the tutorial here.





Join Techblog
Facebook Group
Read
Digg entries
Add techblog to
Google reader