Posted by: Suneet Kumar G. Chaudhary of Applied Programming blog
Dear All,
The following content is intended to demonstrate TWITTER INTEGRATION in a simplified way:
Step 1
Go to https://apps.twitter.com/
Step 2
Create New App.
Step 3
After creating new app, you shall receive following:
1. Consumer Key (API Key)
2. Consumer Secret (API Secret)
3. Access Token
4. Access Token Secret
Step 4
Download Twitter4j API kit from the below link:
http://twitter4j.org/archive/twitter4j-4.0.4.zip
Attach it to your library folder. See below.
Step 5
Compile the below core java code.
package twitterintegrator;
import java.util.logging.Level;
import java.util.logging.Logger;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
/**
*
* @author Suneet
*/
public class TwitterIntegrator {
/**
* @param args the command line arguments
*/
//CREATE TWITTER OBJECT USING API TOKENS
synchronized static Twitter getTwitter() {
Twitter twitter = TwitterFactory.getSingleton();
//Keep the "Consumer Secret" a secret.
//This key should never be human-readable in your application.
twitter.setOAuthConsumer("Consumer Key (API Key)", "Consumer Secret (API Secret)");
//This access token can be used to make API requests on your own account's behalf.
//Do not share your access token secret with anyone.
AccessToken at = new AccessToken("Access Token", "Access Token Secret");
twitter.setOAuthAccessToken(at);
//Authorized Twitter object created.
return twitter;
}
public static void main(String[] args) {
try {
// TODO code application logic here
//Create Twitter object.
Twitter twitter = getTwitter();
//Set status message by calling 'updateStatus()' method.
twitter.updateStatus("Hello World! - Posted from my app.");
//You are encouraged to explore more functions of the Twitter object.
System.out.println("Message posted.");
} catch (TwitterException ex) {
Logger.getLogger(TwitterIntegrator.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Step 6
Check Status on https://twitter.com/