将Twitter状态更新发布到Java专用帐户的最简单方法

Hen*_*ann 1 java twitter oauth

我正在寻找一种将状态更新发布到专用Twitter帐户的简单方法.

目前我们使用https基本身份验证使用POST请求https://api.twitter.com/1/statuses/update.xml.但Twitter将禁用此API:http://apiwiki.twitter.com/Authentication

这个oauth的东西真的非常复杂,根本没有很好的解释.

我尝试使用路标,但在玩了几个小时后,我得到的只是一个401.

OAuthConsumer consumer = new DefaultOAuthConsumer(consumerKey, consumerSecret, SignatureMethod.HMAC_SHA1);
consumer.setTokenWithSecret(accessToken, tokenSecret);

URL url = new URL("https://api.twitter.com/1/statuses/update.xml");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// Setup the header for the request
connection.setRequestMethod("POST");
connection.setRequestProperty("User-Agent", "Status Updater");

consumer.sign(connection);

// write the message
connection.setDoOutput(true);
OutputStream os = connection.getOutputStream();
os.write(("status=" + message.substring(0, Math.min(message.length(), 139))).getBytes("UTF-8"));
os.close();

// send the request
connection.getInputStream().close();
Run Code Online (Sandbox Code Playgroud)

Plu*_*tor 5

我有很多更好的运气Twitter4J比路标.核心jar只有300k,它也是Google App Engine和Android(这实际上是我用过的地方)兼容的.它维护得很好; 它几乎支持xAuth.他们的网站上有一些很好的代码示例.

如果你想看到真正的代码,这里是我为此实现的应用程序的源代码.