use of twitter4j.TwitterException in project twitter-2-weibo by rjyo.
the class AuthServlet method doGet.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpServletRouter r = new HttpServletRouter(request);
r.setPattern("/:type");
response.setContentType("text/plain");
PrintWriter writer = response.getWriter();
String serverPath = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
HttpSession session = request.getSession();
if (r.is(":type", "weibo")) {
try {
Oauth oauth = new Oauth();
String redirectUrl = oauth.authorize("code");
response.setStatus(302);
response.setHeader("Location", redirectUrl);
log.info("Redirecting Weibo...");
} catch (WeiboException e) {
log.error(e);
}
} else if (r.is(":type", "twitter")) {
log.info("hello world~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
try {
TwitterFactory factory = new TwitterFactory();
Twitter t = factory.getInstance();
twitter4j.auth.RequestToken requestToken = t.getOAuthRequestToken(serverPath + "/callback/twitter");
response.setStatus(302);
log.info(requestToken.getAuthenticationURL());
response.setHeader("Location", requestToken.getAuthenticationURL());
session.setAttribute(Keys.SESSION_REQUEST_TOKEN, requestToken);
log.info("Redirecting Twitter...");
} catch (TwitterException e) {
log.error(e);
}
writer.close();
} else {
response.setStatus(200);
writer.println("Wrong parameter, not working!");
writer.close();
}
}
use of twitter4j.TwitterException in project openhab1-addons by openhab.
the class Twitter method sendTweet.
/**
* Sends a Tweet via Twitter
*
* @param tweetTxt the Tweet to send
*
* @return <code>true</code>, if sending the tweet has been successful and
* <code>false</code> in all other cases.
*/
@ActionDoc(text = "Sends a Tweet via Twitter", returns = "<code>true</code>, if sending the tweet has been successful and <code>false</code> in all other cases.")
public static boolean sendTweet(@ParamDoc(name = "tweetTxt", text = "the Tweet to send") String tweetTxt) {
if (!TwitterActionService.isProperlyConfigured) {
logger.debug("Twitter client is not yet configured > execution aborted!");
return false;
}
if (!isEnabled) {
logger.debug("Twitter client is disabled > execution aborted!");
return false;
}
try {
// abbreviate the Tweet to meet the 140 character limit ...
tweetTxt = StringUtils.abbreviate(tweetTxt, CHARACTER_LIMIT);
// send the Tweet
Status status = client.updateStatus(tweetTxt);
logger.debug("Successfully sent Tweet '{}'", status.getText());
return true;
} catch (TwitterException e) {
logger.error("Failed to send Tweet '" + tweetTxt + "' because of: " + e.getLocalizedMessage());
return false;
}
}
use of twitter4j.TwitterException in project openhab1-addons by openhab.
the class Twitter method sendDirectMessage.
/**
* Sends a direct message via Twitter
*
* @param recipientId the receiver of this direct message
* @param messageTxt the direct message to send
*
* @return <code>true</code>, if sending the direct message has been successful and
* <code>false</code> in all other cases.
*/
@ActionDoc(text = "Sends a direct message via Twitter", returns = "<code>true</code>, if sending the direct message has been successful and <code>false</code> in all other cases.")
public static boolean sendDirectMessage(@ParamDoc(name = "recipientId", text = "the receiver of this direct message") String recipientId, @ParamDoc(name = "messageTxt", text = "the direct message to send") String messageTxt) {
if (!isEnabled) {
logger.debug("Twitter client is disabled > execution aborted!");
return false;
}
try {
// abbreviate the Tweet to meet the 140 character limit ...
messageTxt = StringUtils.abbreviate(messageTxt, CHARACTER_LIMIT);
// send the direct message
DirectMessage message = client.sendDirectMessage(recipientId, messageTxt);
logger.debug("Successfully sent direct message '{}' to @", message.getText(), message.getRecipientScreenName());
return true;
} catch (TwitterException e) {
logger.error("Failed to send Tweet '" + messageTxt + "' because of: " + e.getLocalizedMessage());
return false;
}
}
use of twitter4j.TwitterException in project twitter4j by yusuke.
the class MediaUploadTest method testNonexistingFileUpload.
public void testNonexistingFileUpload() throws Exception {
ImageUploadFactory factory = new ImageUploadFactory(getConfiguration("d414e7c05f440c867990fbb08286bdfd"));
ImageUpload upload = factory.getInstance(MediaProvider.IMG_LY);
try {
upload.upload(new File("foobar"));
} catch (TwitterException te) {
if (!(te.getCause() instanceof FileNotFoundException)) {
fail("expecting FileNotFoundException");
}
}
}
use of twitter4j.TwitterException in project android-priority-jobqueue by yigit.
the class FetchTweetsJob method shouldReRunOnThrowable.
@Override
protected RetryConstraint shouldReRunOnThrowable(Throwable throwable, int runCount, int maxRunCount) {
if (throwable instanceof TwitterException) {
//if it is a 4xx error, stop
TwitterException twitterException = (TwitterException) throwable;
int errorCode = twitterException.getErrorCode();
return errorCode < 400 || errorCode > 499 ? RetryConstraint.RETRY : RetryConstraint.CANCEL;
}
return RetryConstraint.RETRY;
}
Aggregations