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:org.osframework.maven.plugins.twitter.AbstractTwitterMojo.java

License:Apache License

protected void loadAccessToken(final Twitter twitter) throws TwitterException {
    // Check for stored access token
    File tokenStore = new File(getWorkDirectory(), "auth");
    if (tokenStore.canRead()) {
        Properties p = new Properties();
        InputStream in = null;/*  ww w .  j a va  2 s.  co  m*/
        try {
            in = new FileInputStream(tokenStore);
            p.load(in);
        } catch (IOException ignore) {
        } finally {
            IOUtil.close(in);
        }
        authToken = new AccessToken(p.getProperty(OAUTH_ACCESS_TOKEN),
                p.getProperty(OAUTH_ACCESS_TOKEN_SECRET));
    }
    // Get access token via user authorization
    else {
        RequestToken requestToken = twitter.getOAuthRequestToken();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while (null == authToken) {
            getLog().info("Open the following URL and grant access to your account:");
            getLog().info(requestToken.getAuthorizationURL());
            System.out.print("Enter the PIN (if available) or just hit enter. [PIN]: ");
            try {
                String pin = br.readLine();
                authToken = (0 < pin.length()) ? twitter.getOAuthAccessToken(requestToken, pin)
                        : twitter.getOAuthAccessToken();
            } catch (IOException ioe) {
                getLog().error("Could not read authorization PIN from input");
                throw new TwitterException(ioe);
            } catch (TwitterException te) {
                if (401 == te.getStatusCode()) {
                    getLog().error("Could not acquire access token");
                }
                throw te;
            }
        }
    }
}

From source file:org.osframework.maven.plugins.twitter.AbstractTwitterMojo.java

License:Apache License

private Twitter getAuthenticatedTwitter() throws TwitterException {
    if (StringUtils.isBlank(consumerKey) || StringUtils.isBlank(consumerSecret)) {
        throw new TwitterException("Missing required credentials");
    }//from w  ww.ja v a 2  s  . co  m
    Twitter twitter = TwitterFactory.getSingleton();
    twitter.setOAuthConsumer(consumerKey, consumerSecret);
    loadAccessToken(twitter);
    twitter.setOAuthAccessToken(authToken);
    return twitter;
}

From source file:org.rhq.enterprise.server.plugins.alertMicroblog.MicroblogServerPluginComponent.java

License:Open Source License

public void initialize(ServerPluginContext context) throws Exception {
    this.context = context;

    String consumerKey = this.context.getPluginConfiguration().getSimpleValue("consumerKey",
            MicroblogSender.CONS_KEY);/*from  w  w w  .  ja  va  2 s  . c  o m*/
    String consumerSecret = this.context.getPluginConfiguration().getSimpleValue("consumerSecret",
            MicroblogSender.CONS_SECRET);

    if (consumerKey == null || consumerSecret == null)
        throw new TwitterException(
                "consumerKey or consumerSecret missing. Please configure the Microblog plugin before.");

    // The factory instance is re-useable and thread safe.
    this.twitter = new TwitterFactory().getInstance();
    this.twitter.setOAuthConsumer(consumerKey, consumerSecret);

    log.debug("Twitter using consumerKey [" + consumerKey + "] and consumerSecret: [" + consumerSecret + "]");
}

From source file:org.sush.twitterstream.MyTwitterStream.java

License:Apache License

public void startStatusStream() throws TwitterException, InterruptedException {
    if (predicates == null) {
        throw new TwitterException("predicates not set");
    }//  w w  w. j av a 2  s .c o m
    FilterQuery filter = new FilterQuery();
    filter.track(predicates.toArray(new String[1]));
    twitterStream.filter(filter);
    while (!isStreamOn && twitterException != null) {
        Thread.sleep(10000);
    }
    if (twitterException != null) {
        throw twitterException;
    }
}

From source file:org.wso2.fasttrack.project.roadlk.rest.DatasetGenerator.java

License:Open Source License

/**
 * @param consumerKey/*w w w .  j a v a2s . c  om*/
 *            Twitter Consumer Key (API Key)
 * @param consumerSecret
 *            Twitter Consumer Secret (API Secret)
 * @param accessToken
 *            Twitter Access Token
 * @param accessTokenSecret
 *            Twitter Access Token Secret
 * @param pageLimit
 *            Maximum pages to be retrieved
 * @throws IOException
 * @throws TwitterException
 */
@SuppressWarnings("resource")
public void generateDataset(String consumerKey, String consumerSecret, String accessToken,
        String accessTokenSecret, int pageLimit) throws IOException, TwitterException {
    // Twitter object of Twitter4J library
    Twitter twitter = TwitterFactory.getSingleton();

    // Twitter API authentication
    twitter.setOAuthConsumer(consumerKey, consumerSecret);
    twitter.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret));

    PrintWriter printWriter = null;

    try {
        printWriter = new PrintWriter(new BufferedWriter(new FileWriter("dataset.txt", true)));
    } catch (IOException e) {
        throw new IOException(e);
    }

    LOGGER.debug("Twitter feed extraction started.");

    for (int i = 1; i < pageLimit; i = i + 1) {

        Paging paging = new Paging(i, 100);
        ResponseList<Status> statuses = null;

        try {
            statuses = twitter.getUserTimeline("road_lk", paging);
        } catch (TwitterException e) {
            //LOGGER.error("TwitterException occurred." + e.getMessage());
            throw new TwitterException(e);
        }

        for (Status status : statuses) {
            printWriter.println(status.getCreatedAt() + ": " + status.getText());
        }
    }
    printWriter.close();
    LOGGER.debug("Twitter feed extraction completed.");
}

From source file:TwitterAnalytics.TwitterAPI.java

private Query makeQuery(List<String> keywords) throws TwitterException {
    // asumsi: pake OR
    if (keywords.isEmpty()) {
        throw new TwitterException("No keyword entered!");
    } else {//from w w  w .jav  a  2s .  c o  m
        StringBuilder sb = new StringBuilder();

        int i = 0;
        sb.append("(").append(keywords.get(i)).append(")");
        i++;

        while (i < keywords.size()) {
            sb.append("OR (").append(keywords.get(i)).append(")");
            i++;
        }

        return new Query(sb.toString());
    }
}

From source file:uk.ac.susx.tag.method51.twitter.apikeystore.TwitterPinAuthentication.java

License:Apache License

public ApiKey getAuth(String pin) throws TwitterException {

    if (requestToken == null) {

        throw new TwitterException("Auth request not issued, run requestAuthUrl() first!");
    }/*from ww w .j  av a2 s  . c  o m*/

    AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, pin);

    String at = accessToken.getToken();
    String ats = accessToken.getTokenSecret();

    ApiKey authedKey = new ApiKey("replaceme", consumerKey, consumerSecret, at, ats);

    return authedKey;
}