最好的 Java 推特库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/851767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:10:50  来源:igfitidea点击:

Best java twitter library?

javatwitter

提问by flybywire

The twitter API site lists 3 java twitter libraries.

twitter API 站点列出了 3 个 java twitter 库。

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

你认识其他人吗?您在支持、易用性、稳定性、社区等方面有什么经验?

采纳答案by Rahul Garg

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

我认为 Twitter4j 很好,它是最新的 API

回答by William Pietri

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.

JTwitter 看起来绝对是三者中最弱的。它最近似乎没有更新,没有涵盖 Twitter API 的大部分内容,并且除了最初的版本之外似乎没有任何版本。从好的方面来说,它是 LPGL 许可的,打包了它需要的少量额外代码,并且看起来小巧且易于理解。

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.

另外两个,java-twitter 和 Twtter4J 在质量上看起来更接近。两者都涵盖了核心 API,并且都具有普通开源项目的所有装备:公开可用的源代码存储库、在线文档、活动邮件列表和最近的开发活动。

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.

然而,Twitter4J 看起来是领导者。从文档来看,它的 API 覆盖范围似乎更完整。邮件列表肯定更活跃。文档要好得多。对我来说最重要的是,发布更加频繁。java-twitter 发布了一个版本,即大约 4 个月前的“0.9-SNAPSHOT”版本。Twitter4J 在此期间发布了多个版本,包括 2.0.0 和 2.0.8 之前的增量版本,修复了问题并添加了对新 Twitter API 的支持。

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

我将从 Twitter4J 开始;如果您没有收到回复,请假设这对我来说很棒。

回答by Tahir Akram

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.

我选择了 Twitter4j,因为很多人都在研究它。并且很容易在互联网上找到一些关于它的内容。

回答by Ted Pennings

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

我使用 Twitter4J 并且还没有遇到问题。我实际上非常喜欢它。

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.

他们在其网站上提供的 OAuth 示例是最大的麻烦——它没有帮助。如果您有兴趣(或其他任何人),这是我的 OAuthServlet 代码。我知道这个问题已经很老了,所以我把它放在这里更多是为了搜索结果。

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.

我使用类 TwitterCnx 作为 twitter4j.Twitter 的包装器。TwitterCnx 定义了我的 OAuth 消费者内容并返回一个 Twitter 对象。这是一个带有静态方法的最终类,所以我不会为每个应用程序实例生成一个以上的 Twitter 对象。

回答by Daniel Winterstein

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

咳咳; JTwitter得到积极维护,定期更新(今天发布的 1.6.2 版),并涵盖了 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.

它也快速而强大。Twitter 在某些地方可能不稳定,而 JTwitter 有变通方法和信息丰富的例外,让您的生活更轻松。

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

作为主要的 JTwitter 开发者,我相当有偏见!但使用 JTwitter 的开发人员的反馈也非常积极。

回答by Tom Morris

There's also TweetStream4Jwhich 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).

还有TweetStream4J,它是 Twitter Streaming API 的 Java 绑定。它非常简单,与我上次使用它不同,作者更新了它以包含一个 pom.xml 文件,以便您可以构建它。当我上次使用它(来自 Scala)时,它非常简单和快速。

回答by AbsentMoniker

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

对于仍在关注此问题的任何人,值得一提的是 Twitter目前在其网站列出的唯一用于 Java 的 API 是 Twitter4J。因此,如果您需要更令人信服地证明这是可以使用的,那么,就这样吧!