Twitter and Java

From CompSciWiki
Revision as of 12:21, 10 August 2010 by Mdomarat (Talk | contribs)

Jump to: navigation, search

Extra Lab: jTwitter

In this lab, we'll learn how to use a simple API wrapper for twitter to write Java programs to play with twitter.

Step 0: Register for Twitter

If you don't have a twitter account, you can sign up easily here. You can always delete this account later.

Step 1: Download jTwitter

jTwitter is the jar file we will use to add twitter capability. You can download the file here. In this lab, we'll be using an old version of jTwitter to write our programs. A newer version of jTwitter is available on the jtwitter homepage, but it requires more complex authorization than what we've got time for here. If you were building a serious application using jTwitter, you would want to use the most recent version.

Step 2: Make jTwitter accessible to your code

In eclipse, make the jar file accessible to your code:

  1. create a new Java project (File -> New -> Java Project).
  2. select the project, then click on Project-> Properties).
  3. click on "Java Build Path" in the left-hand pane, then click on the "Libraries" tab.
  4. click on "Add External JARs..." to add the jar file to the project.
  5. select the jtwitter.jar file you downloaded.

After this, the jtwitter.jar and all its contents should be accessible to your code.

If you are not using eclipse, then you need to extract the jar file in the directory you want to work in. In Windows, do the following:

  1. go to the directory you want to write you program in, and move the jTwitter.jar file to that directory.
  2. open a command prompt and go to the same directory.
  3. at the command prompt extract the jar file with the command jar xf jtwitter.jar.
  4. if Windows complains about jar not being a recognized program, then you need to type the entire path of the jar command. Typically this is something like C:\Program Files\Java\jdk<some_number>\bin\jar.exe.

After this, the directory winterwell should have been created. In other operating systems (mac OS, linux), the same instructions work, and your OS should properly know where the jar command is.

Step 3: Start coding

In order to access the jTwitter methods, add the following import line with the rest of your imports:

import winterwell.jtwitter.*;

We can now start by writing a simple program to use jTwitter:

import winterwell.jtwitter.*;

public class tryjTwitter {
  public static void main (String[] args) {
    Twitter myTwitter = new Twitter("yourUserName","yourPassword");
    myTwitter.setStatus("hello twitter world.");
  }
}

Step 4: Don't wreck it for everyone

Step 5: Other routines in jTwitter

Step 6: Password safety in Java