Difference between revisions of "Twitter and Java"

From CompSciWiki
Jump to: navigation, search
m
Line 1: Line 1:
=Extra Lab: jTwitter=
+
===Under Construction===
 
+
In this lab, we'll learn how to use a simple API wrapper for twitter to write Java programs to play with [http://www.twitter.com twitter].
+
 
+
""This page is under revision'''
+
 
+
==Step 0: Register for Twitter==
+
 
+
If you don't have a twitter account, you can sign up easily [https://twitter.com/signup 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 [http://www.cs.umanitoba.ca/~mdomarat/1010/jtwitter.jar 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 [http://www.winterwell.com/software/jtwitter.php 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:
+
# create a new Java project (File -> New -> Java Project).
+
# select the project, then click on Project-> Properties).
+
# click on "Java Build Path" in the left-hand pane, then click on the "Libraries" tab.
+
# click on "Add External JARs..." to add the jar file to the project.
+
# 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:
+
 
+
# go to the directory you want to write you program in, and move the jTwitter.jar file to that directory.
+
# open a command prompt and go to the same directory.
+
# at the command prompt extract the jar file with the command <tt>jar xf jtwitter.jar</tt>.
+
# if Windows complains about jar not being a recognized program, then you need to type the entire path of the <tt>jar</tt> command. Typically this is something like <tt>C:\Program Files\Java\jdk<some_number>\bin\jar.exe</tt>.
+
 
+
After this, the directory <tt>winterwell</tt> should have been created. In other operating systems (mac OS, linux), the same instructions work, and your OS should properly know where the <tt>jar</tt> command is.
+
 
+
==Step 3: Start coding==
+
 
+
In order to access the jTwitter methods, add the following import line with the rest of your imports:
+
 
+
<pre>
+
import winterwell.jtwitter.*;
+
</pre>
+
 
+
We can now start by writing a simple program to use jTwitter:
+
 
+
<pre>
+
import winterwell.jtwitter.*;
+
 
+
public class tryjTwitter {
+
  public static void main (String[] args) {
+
    Twitter myTwitter = new Twitter("yourUserName","yourPassword");
+
    myTwitter.setStatus("hello twitter world.");
+
  }
+
}
+
</pre>
+
 
+
In this example, the string <tt>"yourUserName"</tt> is your actual user name that you use to log in to twitter, and <tt>"yourPassword"</tt> is your (plain-text!) password. So your code will actually contain your password! If you don't want your friend to see your password, don't let them see your code. They will abuse it if they find it. Trust me.  We will solve this problem in Step 6.
+
 
+
==Step 4: Don't wreck it for everyone==
+
 
+
The previous program (hopefully) works: it posts the string "hello twitter world" to your twitter account as a tweet.  So please, please, don't abuse this. Do not write code like
+
 
+
<pre>
+
for (int i = 0; i < 1000; i++) {
+
  myTwitter.setStatus("this is automated tweet number " + i);
+
}
+
</pre>
+
 
+
That might well get you 1000 tweets on your account.  But it might also get your account suspended and possibly wreck things for everyone. So please don't automate large volumes of tweets.
+
 
+
 
+
==Step 5: Other routines in jTwitter==
+
 
+
Setting your status is not the only thing you can accomplish with jTwitter. Other methods of the class <tt>Twitter</tt> that you may find useful are:
+
 
+
* <tt>public Twitter.Status getStatus (String userName)</tt>: Returns the current status of the twitter with user name <tt>userName</tt>. The method returns an object of type <tt>Twitter.Status</tt>, but this can be printed directly like it were a <tt>String</tt>:
+
<pre>
+
System.out.println(myTwitter.getStatus("shitmydadsays"));
+
</pre>
+
You don't need to be following the user to get the status.  If no String parameter is passed, the status returned is your own.
+
* <tt>public Twitter.User follow(String userName)</tt>: follow the twitter account with user name <tt>userName</tt>.  Returns an object of type <tt>Twitter.User</tt> which can be used to internally represent the user, but can also be
+
discarded.
+
* <tt>public Twitter.User breakFriendship(String userName)</tt>: stop following <tt>userName</tt>
+
* <tt>public java.util.List<Twitter.User> getFriendsTimeline ()</tt>: return the last 20 tweets from friends you follow. They are returned as a list. Example (uses Java 5 for loops):
+
<pre>
+
ArrayList<Twitter.Status> tweets = (ArrayList<Twitter.Status>) myT.getFriendsTimeline();
+
for (Twitter.Status x : tweets) {
+
  System.out.println(x.getUser() + " says " + x);
+
}
+
</pre>
+
 
+
==Step 6: Password safety in Java==
+
 
+
Up to now, you've had to have your password displayed as plain text in your source code which means
+
# anyone looking at your source code can steal your password and
+
# no one else can use your code unless they hardcode in your password.
+
 
+
To solve this, we can use the <tt>JPasswordField</tt> class provided by Java to read in a password. You need to import <tt>javax.swing.JPasswordField</tt>. With it, you can write some dialog boxes using JOptionPane to read in usernames and passwords:
+
<pre>
+
String userID = JOptionPane.showInputDialog(null,"Enter userid");
+
JPasswordField pwd = new JPasswordField(10);
+
JOptionPane.showConfirmDialog(null, pwd,"Enter Password for "+userID,JOptionPane.OK_OPTION);
+
String userPassword = new String(pwd.getPassword());
+
Twitter myTwitter = new Twitter(userID,userPassword);
+
</pre>
+
 
+
This is a small hack to quickly access the <tt>JPasswordField</tt> class.  In a serious application, you would likely design a single window to read in both the username and password using <tt>JFrame</tt> as in the [[User:Mdomarat/Extralabs/GUI_Part_I|GUI lab]].
+

Revision as of 10:33, 30 September 2010

Under Construction