Pages

Sunday, July 4, 2010

Twitter API

Twitter4J is a Java library for the Twitter API.
With Twitter4J, We can easily integrate our Java application with the Twitter service.

Step-up Twitter4j API

Get the Consumer key \ API key & Consumer Secret \Secret Key.

To start your work with Twitter4j API, You need to get the
Consumer key \ API key & Consumer Secret \Secret Key.

You need to get register your applications in this page http://twitter.com/apps
using Twitter account.

Download Twitter4j jar
Download - twitter4j-core-2.1.3-SNAPSHOT.jar
Inlcuded the jar in the class path and try to access the class in the jar

We can start coding the application now.
Create the Twitter object & get the request token by passing the application Consumer key & secret.

Code

Twitter twitter = new Twitter();
twitter.setOAuthConsumer("consumer key",”consumer secret” );
RequestToken requestToken = twitter.getOAuthRequestToken();


Get the Access Token & Secret

By using the request token you need to get the access token & token secret.
Code

String token = requestToken.getToken();
String tokenSecret = requestToken.getTokenSecret();


Authorized URL for accessing the Application
Store the requestToken & Authorized URl in a session.

String authUrl = requestToken.getAuthorizationURL();
session.setAttribute("authUrl", authUrl);
session.setAttribute("requestToken", requestToken);


Example Authorized Application URL

https://twitter.com/oauth/authorize?auth_token=LJyavdddrrrreddddy3sfpm8bTHg3H3Bddddm4

Once you click the authorized link it will ask for you twitter username & password.Once user details verfied it redirect to your application.


Functions in Twitter4j

Tweet Message

This function is to tweet a text message in his/her twitter.

Status status = twitter.updateStatus(“Tweet Message .”);


Delete the Tweeted message

We can delete the tweeted message by passing the TweetId

destroyTweet(twitter,tweetId);

User details
This function is used to get the user details.We need to pass the user’s id .

User user = twitter.showUser(twitter.getId());

From the user object we can get the details of the user profile,description …

user.getProfileImageURL();
user.getScreenName();
user.getName();
user.getDescription();
user.getURL();
user.getLocation();
user.getProfileBackgroundColor();
user.getProfileBackgroundImageUrl();


Search Tweet
We can search for tweets by using this method

String searchTweet ="TieCon MidWest"
Query query = new Query("searchTweet");
QueryResult result = twitter.search(query);
System.out.println("hits:" + result.getTotal());
for (Tweet tweet : result.getTweets()) {
System.out.println(tweet.getFromUser() + ":" + tweet.getText());
}

To get the friends tweets & details

/* All the Tweets in the first page will be displayed*/


List statuses = twitter.getFriendsTimeline();
for (Status status : statuses) {
System.out.println(status.getUser().getName() + ":" +
status.getText());
}


/** Four tweets in the first page will be displayed*/

Paging paging = new Paging(1, 4);
List statuses = twitter.getFriendsTimeline(paging);


Sending / Receiving Direct Messages

Twitter sender= new TwitterFactory().getInstance(senderID,senderPassword);
sender.sendDirectMessage(recipientId,message);
Twitter receiver = new TwitterFactory().getInstance(recipientId,recipientPassword);
List messages = receiver.getDirectMessages();
for (DirectMessage message : messages) {
System.out.println("Sender:" + message.getSenderScreenName());
System.out.println("Text:" + message.getText());
}



Method's avaialabe in the Twitter API

http://apiwiki.twitter.com/Twitter-API-Documentation

3 comments:

  1. Yes We can.But we have to use third party API like YFrog,Twitpic..etc.,You can check this link for some of third party providers.
    http://bit.ly/euD08e

    ReplyDelete
  2. I was searching for this .....thanks to post this nice article



    GetDividendHistory

    ReplyDelete