Example usage for twitter4j TwitterException TwitterException

List of usage examples for twitter4j TwitterException TwitterException

Introduction

In this page you can find the example usage for twitter4j TwitterException TwitterException.

Prototype

public TwitterException(Exception cause) 

Source Link

Usage

From source file:ch.schrimpf.core.AccessHandler.java

License:Open Source License

public Twitter connect() throws TwitterException {
    Properties prop = new Properties();
    try {// w  w w  .j  ava 2  s.  c o  m
        prop.load(new FileInputStream("api.key"));

    } catch (IOException e) {
        // Properties could not be load
        throw new TwitterException("could not get api key properties");
    }
    twitter.setOAuthConsumer(prop.getProperty("apiKey"), prop.getProperty("apiSecret"));
    twitter.setOAuthAccessToken(loadToken());
    return twitter;
}

From source file:com.allenzheng.twittersyn.utility.impl.TwitterAPIImpl.java

License:Apache License

    public String getAuthorisationUrl() throws TwitterException{
      Properties prop = loadProperties();
      /*from  www . j a  v a 2  s  .co m*/
         Twitter twitter = new TwitterFactory().getInstance();
         twitter.setOAuthConsumer(prop.getProperty("twitter.consumerkey"), 
               prop.getProperty("twitter.consumersecret"));
         try {
            RequestToken requestToken;
            if (callbackUrl == null) {
               requestToken = twitter.getOAuthRequestToken();
            } else {
               requestToken = twitter
                     .getOAuthRequestToken(callbackUrl);
            }
            String authorisationUrl = requestToken
                  .getAuthorizationURL();
//            session.setAttribute(ATTR_TWITTER, twitter);
//            session.setAttribute(ATTR_REQUEST_TOKEN, requestToken);

            logger.debug("Redirecting user to " + authorisationUrl);
//            response.sendRedirect(authorisationUrl);
            return authorisationUrl;
         } catch (TwitterException e) {
            logger.error("Sign in with Twitter failed - "
                  + e.getMessage());
            e.printStackTrace();
            throw new TwitterException(e);
         }
      
      
   }

From source file:com.dwdesign.tweetings.util.Utils.java

License:Open Source License

public static HttpResponse getRedirectedHttpResponse(final HttpClientWrapper client, final String url)
        throws TwitterException {
    if (url == null)
        return null;
    final ArrayList<String> urls = new ArrayList<String>();
    urls.add(url);//from   w  ww.  jav a 2  s.  c om
    HttpResponse resp;
    try {
        resp = client.get(url, url);
    } catch (final TwitterException te) {
        if (isRedirected(te.getStatusCode())) {
            resp = te.getHttpResponse();
        } else
            throw te;
    }
    while (resp != null && isRedirected(resp.getStatusCode())) {
        final String request_url = resp.getResponseHeader("Location");
        if (request_url == null)
            return null;
        if (urls.contains(request_url))
            throw new TwitterException("Too many redirects");
        urls.add(request_url);
        try {
            resp = client.get(request_url, request_url);
        } catch (final TwitterException te) {
            if (isRedirected(te.getStatusCode())) {
                resp = te.getHttpResponse();
            } else
                throw te;
        }
    }
    return resp;
}

From source file:com.freshdigitable.udonroad.TimelineInstTestBase.java

License:Apache License

protected Status findByStatusId(long statusId) throws Exception {
    for (Status s : responseList) {
        if (s.getId() == statusId) {
            return s;
        }// w  w  w. j a  v  a2 s .c  o m
    }
    throw new TwitterException("status is not found. ID: " + statusId);
}

From source file:de.vanita5.twittnuker.util.net.TwidereHttpClientImpl.java

License:Apache License

@Override
public twitter4j.http.HttpResponse request(final twitter4j.http.HttpRequest req) throws TwitterException {
    final HostAddressResolver resolver = FactoryUtils.getHostAddressResolver(conf);
    final String urlString = req.getURL();
    final URI urlOrig = ParseUtils.parseURI(urlString);
    final String host = urlOrig.getHost(), authority = urlOrig.getAuthority();
    try {/*from w  w  w  .  j av a2 s.  c om*/
        HttpRequestBaseHC4 commonsRequest;
        final String resolvedHost = resolver != null ? resolver.resolve(host) : null;
        final String resolvedUrl = !isEmpty(resolvedHost)
                ? urlString.replace("://" + host, "://" + resolvedHost)
                : urlString;
        final RequestMethod method = req.getMethod();
        if (method == RequestMethod.GET) {
            commonsRequest = new HttpGetHC4(resolvedUrl);
        } else if (method == RequestMethod.POST) {
            final HttpPostHC4 post = new HttpPostHC4(resolvedUrl);
            post.setEntity(getAsEntity(req.getParameters()));
            post.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
            commonsRequest = post;
        } else if (method == RequestMethod.DELETE) {
            commonsRequest = new HttpDeleteHC4(resolvedUrl);
        } else if (method == RequestMethod.HEAD) {
            commonsRequest = new HttpHeadHC4(resolvedUrl);
        } else if (method == RequestMethod.PUT) {
            final HttpPutHC4 put = new HttpPutHC4(resolvedUrl);
            put.setEntity(getAsEntity(req.getParameters()));
            commonsRequest = put;
        } else
            throw new TwitterException("Unsupported request method " + method);
        final HttpParams httpParams = commonsRequest.getParams();
        HttpClientParams.setRedirecting(httpParams, false);
        final Map<String, String> headers = req.getRequestHeaders();
        for (final String headerName : headers.keySet()) {
            commonsRequest.addHeader(headerName, headers.get(headerName));
        }
        final Authorization authorization = req.getAuthorization();
        final String authorizationHeader = authorization != null ? authorization.getAuthorizationHeader(req)
                : null;
        if (authorizationHeader != null) {
            commonsRequest.addHeader(HttpHeaders.AUTHORIZATION, authorizationHeader);
        }
        if (resolvedHost != null && !resolvedHost.isEmpty() && !resolvedHost.equals(host)) {
            commonsRequest.addHeader(HttpHeaders.HOST, authority);
        }

        final ApacheHttpClientHttpResponseImpl res;
        try {
            final HttpContext httpContext = new BasicHttpContextHC4();
            httpContext.setAttribute(HostResolvedSSLConnectionSocketFactory.HTTP_CONTEXT_KEY_ORIGINAL_HOST,
                    host);
            res = new ApacheHttpClientHttpResponseImpl(client.execute(commonsRequest, httpContext), conf);
        } catch (final IllegalStateException e) {
            throw new TwitterException("Please check your API settings.", e);
        } catch (final NullPointerException e) {
            // Bug http://code.google.com/p/android/issues/detail?id=5255
            throw new TwitterException("Please check your APN settings, make sure not to use WAP APNs.", e);
        } catch (final OutOfMemoryError e) {
            // I don't know why OOM thown, but it should be catched.
            System.gc();
            throw new TwitterException("Unknown error", e);
        }
        final int statusCode = res.getStatusCode();
        if (statusCode < OK || statusCode > ACCEPTED)
            throw new TwitterException(res.asString(), req, res);
        return res;
    } catch (final IOException e) {
        // TODO
        if (resolver instanceof TwidereHostAddressResolver) {
            final TwidereHostAddressResolver twidereResolver = (TwidereHostAddressResolver) resolver;
            twidereResolver.removeCachedHost(host);
        }
        throw new TwitterException(e);
    }
}

From source file:de.vanita5.twittnuker.util.Utils.java

License:Open Source License

public static HttpResponse getRedirectedHttpResponse(final HttpClientWrapper client, final String url,
        final String signUrl, final Authorization auth) throws TwitterException {
    if (url == null)
        return null;
    final ArrayList<String> urls = new ArrayList<String>();
    urls.add(url);/*w  ww  . ja  v a 2s . c  o m*/
    HttpResponse resp;
    try {
        resp = client.get(url, signUrl, auth);
    } catch (final TwitterException te) {
        if (isRedirected(te.getStatusCode())) {
            resp = te.getHttpResponse();
        } else
            throw te;
    }
    while (resp != null && isRedirected(resp.getStatusCode())) {
        final String request_url = resp.getResponseHeader("Location");
        if (request_url == null)
            return null;
        if (urls.contains(request_url))
            throw new TwitterException("Too many redirects");
        urls.add(request_url);
        try {
            resp = client.get(request_url, request_url);
        } catch (final TwitterException te) {
            if (isRedirected(te.getStatusCode())) {
                resp = te.getHttpResponse();
            } else
                throw te;
        }
    }
    return resp;
}

From source file:hashimotonet.UpdateStatus.java

License:Apache License

/**
 * Twitter?/*w ww.ja va  2 s . c o m*/
 * @throws TwitterException 
 */
private void connectTwitter() throws TwitterException {
    // Twitetr4j??
    Configuration conf = ConfigurationContext.getInstance();
    // OauthF?IuWFNg??
    OAuthAuthorization Oauth = new OAuthAuthorization(conf);
    // OauthF?IuWFNgconsumerKeyconsumerSecret?
    Oauth.setOAuthConsumer(Globals.KEY, Globals.SECRET);

    //       // NGXgg?[N??
    RequestToken sRequestToken = null;
    try {
        sRequestToken = Oauth.getOAuthRequestToken();
    } catch (TwitterException e) {
        throw new TwitterException(e.toString());
    }
    String url = sRequestToken.getAuthorizationURL();
    System.out.println("Url = " + url);
}

From source file:mx.bigdata.t4j.TwitterStreamImpl.java

License:Apache License

private StatusStream getCountStream(String relativeUrl, int count) throws TwitterException {
    ensureAuthorizationEnabled();// w  w w  .j  a va 2s . c  om
    try {
        return new StatusStreamImpl(http.post(conf.getStreamBaseURL() + relativeUrl,
                new HttpParameter[] { new HttpParameter("count", String.valueOf(count)) }, auth));
    } catch (IOException e) {
        throw new TwitterException(e);
    }
}

From source file:mx.bigdata.t4j.TwitterStreamImpl.java

License:Apache License

/**
 * {@inheritDoc}/*from   w ww. j av a2s.  co  m*/
 */
public StatusStream getRetweetStream() throws TwitterException {
    ensureAuthorizationEnabled();
    try {
        return new StatusStreamImpl(
                http.post(conf.getStreamBaseURL() + "statuses/retweet.json", new HttpParameter[] {}, auth));
    } catch (IOException e) {
        throw new TwitterException(e);
    }
}

From source file:mx.bigdata.t4j.TwitterStreamImpl.java

License:Apache License

/**
 * {@inheritDoc}/*from   w ww. j av  a2  s  .c  om*/
 */
public StatusStream getSampleStream() throws TwitterException {
    ensureAuthorizationEnabled();
    try {
        return new StatusStreamImpl(http.post(conf.getStreamBaseURL() + "statuses/sample.json",
                new HttpParameter[] { new HttpParameter("delimited", "length") }, auth));
    } catch (IOException e) {
        throw new TwitterException(e);
    }
}