Twitter4j 回复推文不起作用
Twitter4j reply to tweet not working
我正在尝试使用 twitter4j 回复一条推文(我 post 10 秒前的推文),但它只是 post 将其发送到时间线上,而不是作为回复。
post = mainTwitter.updateStatus(...); //1st twitter account
StatusUpdate reply = new StatusUpdate(...);
try {
reply.setInReplyToStatusId(post.getId());
contextTwitter.updateStatus(reply); //2nd twitter account
} catch (TwitterException e) {
System.err.println("Couldn't post context");
e.printStackTrace();
}
id不是-1,是正确的long值。没有抛出异常。
您可以使用以下代码使您的代码段正常工作
public void init() {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(TWITTER_CONSUMER_KEY)
.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET)
.setOAuthAccessToken(TWITTER_ACCESS_TOKEN)
.setOAuthAccessTokenSecret(TWITTER_ACCESS_SECRET);
TwitterFactory tf = new TwitterFactory(cb.build());
twitter = tf.getInstance();
}
/***
*
* @param tweetId : tweet Id
* @param messgae : Reply message
* @return
* @throws TwitterException
*/
public String reply(String tweetId, String messgae ) throws TwitterException {
Status status = twitter.showStatus(Long.parseLong(tweetId));
Status reply = twitter.updateStatus(new StatusUpdate(" @" + status.getUser().getScreenName() + " "+ messgae).inReplyToStatusId(status.getId()));
return Long.toString(reply.getId());
}
用法:
reply("930497966443454464", " Hello Eva")
我正在尝试使用 twitter4j 回复一条推文(我 post 10 秒前的推文),但它只是 post 将其发送到时间线上,而不是作为回复。
post = mainTwitter.updateStatus(...); //1st twitter account
StatusUpdate reply = new StatusUpdate(...);
try {
reply.setInReplyToStatusId(post.getId());
contextTwitter.updateStatus(reply); //2nd twitter account
} catch (TwitterException e) {
System.err.println("Couldn't post context");
e.printStackTrace();
}
id不是-1,是正确的long值。没有抛出异常。
您可以使用以下代码使您的代码段正常工作
public void init() {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(TWITTER_CONSUMER_KEY)
.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET)
.setOAuthAccessToken(TWITTER_ACCESS_TOKEN)
.setOAuthAccessTokenSecret(TWITTER_ACCESS_SECRET);
TwitterFactory tf = new TwitterFactory(cb.build());
twitter = tf.getInstance();
}
/***
*
* @param tweetId : tweet Id
* @param messgae : Reply message
* @return
* @throws TwitterException
*/
public String reply(String tweetId, String messgae ) throws TwitterException {
Status status = twitter.showStatus(Long.parseLong(tweetId));
Status reply = twitter.updateStatus(new StatusUpdate(" @" + status.getUser().getScreenName() + " "+ messgae).inReplyToStatusId(status.getId()));
return Long.toString(reply.getId());
}
用法:
reply("930497966443454464", " Hello Eva")