Introduction: Connecting Twitter to an Eclipse Project With the Twitter4J API

This instructable explains how to connect your Twitter account with your Eclipse project, and how to import the Twitter4J application program interface (API) in order to use its functions to automate reading and writing to Twitter.

This instructable does not demonstrate how to use the Twitter4J API, and does not provide example code.

Materials needed:

  • A Twitter account
  • A Computer
  • Internet access
  • The latest version of Eclipse installed
  • An Eclipse project created
  • The latest stable version of Twitter4J. Download here: http://twitter4j.org

Time:

  • ~1 hour (can vary by level of expertise)

Step 1: Connecting Your Twitter Account to the Twitter Application Account

Details: In order to post to your account you will need to connect it to the Twitter application account you will make.

  • Type and go to apps.twitter.com in your browser.
  • Sign in with your twitter credentials. This will connect your existing Twitter account to the Twitter application account.

  • Click the “Create New App” button on the page displayed when logged in.

  • Fill in the “Name”, “Description”, and “Website”(http://examplesite.com) sections with whatever you desire. The “Callback URL” can be left blank. See figure 1

  • Read and agree to the developer agreement, and click the “Create your Twitter application” button once all areas have been sufficiently filled. You will be redirected to your “Application Management” page.

Step 2: Setting Up Your Twitter Application Page

Details: These steps prep your application for reading and writing from your project

  • In the “Application Management” page, go to the “Permissions” tab. See figure 2.
  • Change the type of access to “Read and Write”. This will allow you to read and write to and from your timeline. See figure 3

a. “Read only” will only allow you to read your timeline (you will not be clicking this)

b. “Read and Write” will allow you to read and write your timeline.

c. “Read, Write, and Access direct messages” will allow you to read and write your timeline and your direct messages(you will not be clicking this).

  • Next click the “Keys and Access Tokens” tab. See figure 4

  • Then click the “Create my access token” button. This will create your access token and access token secret which you will need for later. See figure 5

Step 3: Creating Your Properties File

Details: This file will hold all your secrets that identify and authenticate you as yourself.

  • Right click your project and create a new file. See figure 6.
  • Name this file “twitter4j.properties” file. This file should be at the top level of your project, and not be inside any folders you have created. If the file does not show in the Package Explorer then right click your project and click the refresh button. See figure 7.

  • Within the file, copy and paste the following lines(Lines should be single spaced once copied into the file.Correct as needed):

debug=true

oauth.consumerKey=*****

oauth.consumerSecret=****

oauth.accessToken=*****

oauth.accessTokenSecret=*****

  • Now replace the “****” with your own key/secret/token/tokenSecret found back at the “Application Management” page under the “Key and Access Tokens” tab. See figure 8.

Step 4: Moving the Twitter4J File Into Your Project

Details: These steps will help move the Twitter4J folder into your project

  • Download the latest stable version of the Twitter4J API from http://twitter4j.org if you have not already. See figure 9.
  • Extract the file to your desktop (Anywhere is fine. You are going to move it anyways). See figure 10.
  • Move the newly extracted Twitter4J file into your project. This file should not be within any other file (similar to how your twitter4J.properties file is).

a. Note: It is not necessary to move the file into the project, but doing so will keep it in a convenient place to revisit in the future.

  • Go into your extracted Twitter4J file you have just moved (~/../twitter4j-4.0.4).
  • Next go into the “lib” folder ( ~/../twitter4j-4.0.4/lib).
  • Within the “lib” folder locate the “twitter4j-core-4.0.4.jar,” and make note of where it is. See figure 11.

a. You are not doing anything with this .jar file yet.

Step 5: Importing Twitter4J Jar Into Your Build Path

Details: Importing the necessary .jar will allow you to use the Twitter4J API and its functions

  • Within your Eclipse project, go to the properties window for you project. Here are 3 different ways to get there. In all three cases, make sure to “focus” on the project by clicking on it in the “Package Explorer” window with the left mouse button beforehand.

a. File(top left)->Properties. See figure 12

b. Alt + Enter

c. Right mouse click on the project -> Properties. See figure 13.

  • From the “Properties” window click the “Java Build Path” tab on the left See figure 14.
  • After clicking the tab click the “Libraries” tab from “Java Build Path” window See figure 14.
  • Next click the “Add External JARs…” button from the buttons given on the right hand side See figure 14.
  • Locate the “Twitter4J-core-4.0.4.jar” that you previously located, and add it. Do not add any other .jar. They are not needed for this tutorial.

a. If you put the Twitter4J folder in your project then the .jar should be in ~/.../”your project”/twitter4j-4.0.4/lib folder

Congratulations, now you can use the Twitter4J API to create an application that automatically reads and writes to and from Twitter for you.