开发者

Best java twitter library? [closed]

开发者 https://www.devze.com 2023-02-16 11:43 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

Questions asking us to recommend or f开发者_运维百科ind a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.

Closed 8 years ago.

Improve this question

The twitter API site lists 4 java twitter libraries.

  • Twitter4j
  • java-twitter
  • jtwitter
  • twittered

Do you know others? What are your experiences in support, ease of use, stability, community, etc.


I just took a look at them.

JTwitter definitely looks like the weakest of the three. It doesn't appear to have been updated lately, doesn't cover much of the Twitter API, and doesn't appear to have any releases besides the initial one. On the upside, it's LPGL licensed, comes packaged with what little extra code it needs, and looks small and simple to understand.

The other two, java-twitter and Twtter4J look much closer in quality. Both cover the core API, and both have all the accouterments of normal open-source projects: publicly available source code repository, on-line docs, active mailing lists, and recent development activity.

However, Twitter4J looks to be the leader. From the docs, its API coverage appears to be more complete. The mailing list is definitely more active. The docs are much better. And most importantly to me, the releases have been more frequent. java-twitter has one release up, the "0.9-SNAPSHOT" release about 4 months ago. Twitter4J has had several releases in that time period, including 2.0.0 and incremental releases up through 2.0.8, fixing issues and adding support for new Twitter APIs.

I'm going to start with Twitter4J; if you don't hear back, assume it was just great for me.


I think Twitter4j is good one it is most upto date API


Ahem; JTwitter is actively maintained, regularly updated (version 1.6.2 released today), and covers most of the Twitter API.

It's missing only a method for setting your profile image and a few other profile settings. Other than that, it's pretty complete. Status updates, timelines, friendships, lists, searches, streaming, etc.

It's also fast and robust. Twitter can be flaky in places and JTwitter has work-arounds and informative exceptions to make your life easier.

As the main JTwitter developer, I am rather biased! But the feedback from developers using JTwitter is also very positive.


I use Twitter4J and have yet to have a problem with it. I actually like it a lot.

The OAuth example they give on their website is the biggest nuisance -- it's not helpful. Here's my OAuthServlet code if you're interested (or anyone else). I know this question is rather old, so I'm putting it in here more for search results.

package com.example.oauth;

import java.io.PrintWriter;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import twitter4j.*;
import twitter4j.http.*;


@SuppressWarnings("serial")
public class OAuthServlet extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        HttpSession sess = req.getSession(true);
        RequestToken requestToken = (RequestToken) sess.getAttribute("requestToken");



        if (sess.getAttribute("twitter_user_id") != null) {
            resp.setContentType("text/plain");
            PrintWriter out = resp.getWriter();
            if (req.getRequestURI().indexOf("logout") > 3) {
                sess.removeAttribute("twitter_user_id");
                out.println("You're now logged out.");
            } else {
                out.println("You're already logged in!");
            }
        } else if (req.getRequestURI().indexOf("callback") > 3 && req.getParameter("oauth_token").length() > 0 && requestToken != null) {

            handleCallback(req, resp, sess);
            if (sess.getAttribute("oauth_previous") != null) {
                resp.sendRedirect((String) sess.getAttribute("oauth_previous"));
                sess.removeAttribute("oauth_previous");
            }

        } else {

            sendToTwitter(resp, sess);
            sess.setAttribute("oauth_previous", req.getHeader("Referer"));
        }

    }

    private void sendToTwitter(HttpServletResponse resp, HttpSession sess) throws IOException {

        RequestToken requestToken = (RequestToken) sess.getAttribute("requestToken");

        try {
            Twitter twitter = new TwitterCnx().registerOAuth().get();

            requestToken = twitter.getOAuthRequestToken();
            sess.setAttribute("requestToken", requestToken);

            resp.sendRedirect(requestToken.getAuthorizationURL());


        } catch (TwitterException e) {
            PrintWriter out = resp.getWriter();
            out.println(e.getStackTrace());
        }
    }

    private void handleCallback(HttpServletRequest req, HttpServletResponse resp, HttpSession sess) throws IOException {

        RequestToken requestToken = (RequestToken) sess.getAttribute("requestToken");
        sess.removeAttribute("requestToken");
        String secret = req.getParameter("oauth_token");

        resp.setContentType("text/plain");
        PrintWriter out = resp.getWriter();

        try {
            Twitter t = new TwitterCnx().registerOAuth().get();

            AccessToken accessToken = t.getOAuthAccessToken(requestToken.getToken(), secret);
            long id = (long) t.verifyCredentials().getId();

            storeAccessToken(id, accessToken,sess);
            sess.setAttribute("twitter_user_id", id);

            out.println("You're now logged in!");

        } catch (TwitterException e) {
            out.println("Something went wrong. Sorry about that. Please try again.");
        }

    }



    private void storeAccessToken(Long id, AccessToken at, HttpSession sess) {
        //you probably want to persist this somewhere other than in sessions.
        sess.setAttribute("secret", at.getTokenSecret() );
        sess.setAttribute("token", at.getToken());
        //also, don't forget to persist the user id alongside the token.
    }
}

I use the class TwitterCnx as a wrapper for twitter4j.Twitter. TwitterCnx defines my OAuth consumer stuff and returns a Twitter object. It's a final class with static methods so I don't produce more than one Twitter object per app instance.


For anyone still looking at this question, it's worth mentioning that the only API that Twitter currently lists on its website for Java is Twitter4J. So, if you needed any more convincing that this was the one to use, well, here you go!


I have selected Twitter4j because a lot of people are working on it. And its easy to find out some contents on Internet about it.


There's also TweetStream4J which is a Java binding for the Twitter Streaming API. It's pretty simple, and unlike the last time I used it, the author has updated it to include a pom.xml file so you can build it. It's pretty simple and quick when I last used it (from Scala).

0

精彩评论

暂无评论...
验证码 换一张
取 消