Search in sources :

Example 56 with TwitterException

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();
    }
}
Also used : Oauth(weibo4j.Oauth) WeiboException(weibo4j.model.WeiboException) HttpSession(javax.servlet.http.HttpSession) HttpServletRouter(h2weibo.HttpServletRouter) Twitter(twitter4j.Twitter) TwitterFactory(twitter4j.TwitterFactory) TwitterException(twitter4j.TwitterException) PrintWriter(java.io.PrintWriter)

Example 57 with TwitterException

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;
    }
}
Also used : Status(twitter4j.Status) TwitterException(twitter4j.TwitterException) ActionDoc(org.openhab.core.scriptengine.action.ActionDoc)

Example 58 with TwitterException

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;
    }
}
Also used : DirectMessage(twitter4j.DirectMessage) TwitterException(twitter4j.TwitterException) ActionDoc(org.openhab.core.scriptengine.action.ActionDoc)

Example 59 with TwitterException

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");
        }
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) TwitterException(twitter4j.TwitterException)

Example 60 with TwitterException

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;
}
Also used : TwitterException(twitter4j.TwitterException) RetryConstraint(com.birbit.android.jobqueue.RetryConstraint)

Aggregations

TwitterException (twitter4j.TwitterException)96 Twitter (twitter4j.Twitter)69 TwitterFactory (twitter4j.TwitterFactory)54 Status (twitter4j.Status)21 User (twitter4j.User)12 Intent (android.content.Intent)11 ArrayList (java.util.ArrayList)9 File (java.io.File)6 IDs (twitter4j.IDs)6 Context (android.content.Context)5 Date (java.util.Date)4 DirectMessage (twitter4j.DirectMessage)4 Paging (twitter4j.Paging)4 AccessToken (twitter4j.auth.AccessToken)4 Activity (android.app.Activity)3 SharedPreferences (android.content.SharedPreferences)3 ActivityOptionsCompat (android.support.v4.app.ActivityOptionsCompat)3 View (android.view.View)3 ImageView (android.widget.ImageView)3 TextView (android.widget.TextView)3