带有 twitter4j.properties 文件的 Twitter4j

Twitter4j with a twitter4j.properties file

我正在尝试使用 Twitter4j post 到 Twitter,并根据 http://twitter4j.org.

上的文档将我的 OAuth 凭据放入 twitter4j.properties 文件中

但是我无法使用此文件的凭据正确 post 到 Twitter,因为我只能找到使用 ConfigurationBuilder 的示例并且没有关于何时使用哪种方法的明确示例凭据都存储在 twitter4j.properties 文件中。

我都试过了:

Twitter twitter = TwitterFactory.getSingleton();
twitter.updateStatus("My status");

Twitter twitter = new TwitterFactory().getInstance();
twitter.updateStatus("My status");

两者都给我一个 "Authentication credentials are missing" 错误。

我查看了 Twitter4j 附带的示例,它们涉及通过 URL 回调获取访问令牌,我真的不需要它,因为我已经从 https://apps.twitter.com/

我知道我可以只使用 new ConfigurationBuilder() 方法,但这迫使我将密钥复制并粘贴到我的每个 类 中,这很耗时而且看起来很笨重。从 twitter4j.properties 文件设置凭据的正确方法是什么?

经过一番搜索,我现在明白 twitter4j.properties 文件必须放在我的 WEB-INF 文件夹中,因为我是 运行 a Google App 引擎小服务程序。

http://twitter4j.org/en/configuration.html 上的措辞含糊不清,因为它只是说文件 twitter4j.properties 应该放在 "either the current directory or root of the classpath directory" 中。在我的例子中,我最初将它放在与我的其他源文件相同的目录中,这意味着 Twitter4J 表面上找不到它,因为它只在类路径目录的根目录中搜索,我假设在我的例子中它是WEB-INF.

你可以把它放在src/main/resources/twitter4j.properties然后使用它:

    //scala
    val tweet4u = TwitterFactory.getSingleton()
    tweet4u.updateStatus("Tes1t")