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*/
Liststatuses = 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);
Liststatuses = 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);
Listmessages = 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
Can we post a video or image
ReplyDeleteYes 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.
ReplyDeletehttp://bit.ly/euD08e
I was searching for this .....thanks to post this nice article
ReplyDeleteGetDividendHistory