Example usage for twitter4j.conf ConfigurationBuilder setOAuthConsumerKey

List of usage examples for twitter4j.conf ConfigurationBuilder setOAuthConsumerKey

Introduction

In this page you can find the example usage for twitter4j.conf ConfigurationBuilder setOAuthConsumerKey.

Prototype

public ConfigurationBuilder setOAuthConsumerKey(String oAuthConsumerKey) 

Source Link

Usage

From source file:foo.bar.twitter.sample01.Sample01Activity.java

License:Apache License

/**
 * connect twitter/*w ww.j  a  v  a 2  s  . c  o  m*/
 */
private void connectTwitter() {
    ConfigurationBuilder confbuilder = new ConfigurationBuilder();
    confbuilder.setOAuthConsumerKey(ConstantValue.CONSUMER_KEY);
    confbuilder.setOAuthConsumerSecret(ConstantValue.CONSUMER_SECRET);
    Configuration conf = confbuilder.build();

    twitter = new TwitterFactory(conf).getInstance();
    twitter.setOAuthAccessToken(null);

    try {
        requestToken = twitter.getOAuthRequestToken(ConstantValue.CALLBACK_URL);
        Intent intent = new Intent(this, TwitterLoginActivity.class);
        intent.putExtra(ConstantValue.IEXTRA_AUTH_URL, requestToken.getAuthorizationURL());
        this.startActivityForResult(intent, 0);
    } catch (TwitterException e) {
        Toast.makeText(this, "Twitter Exception!!\n" + e.toString(), Toast.LENGTH_LONG).show();
    }
}

From source file:generatetwittertokens.GenerateTwitterTokens.java

License:Open Source License

public static void main(String[] args) {
    String username = "";

    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
    configurationBuilder.setOAuthConsumerKey(consumerKey);
    configurationBuilder.setOAuthConsumerSecret(consumerSecret);

    try {//from  w w  w . j a v a 2s.  c  om
        Twitter twitter = new TwitterFactory(configurationBuilder.build()).getInstance();
        RequestToken requestToken = twitter.getOAuthRequestToken();
        AccessToken accessToken = null;

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        while (accessToken == null) {
            System.out.println("PhantomBot Twitter API Connection Tool\r\n\r\n"
                    + "This tool will request access to read and write to your Twitter account.\r\n"
                    + "You will be presented with a URL to open in your local browser to approve\r\n"
                    + "access, however, this application will attempt to launch a browser for\r\n"
                    + "you automatically.\r\n\r\n"
                    + "You will be presented with a PIN that you must provide back to this\r\n"
                    + "application. After that is completed, Twitter will generate OAuth keys\r\n"
                    + "which will be stored in the PhantomBot directory as twitter.txt.\r\n\r\n"
                    + "Do keep this file safe! The keys are the same as a password to your Twitter\r\n"
                    + "account!\r\n\r\n" + "You may regenerate the OAuth keys at any time if needed.\r\n");

            System.out.println("Open the following URL in your browser if a browser does not automatically\r\n"
                    + "launch within a few seconds:");
            System.out.println("    " + requestToken.getAuthorizationURL() + "\r\n");

            /*
             * Attempt to launch a local browser.  Ignore exceptions, except if the URL is bad.
             */
            try {
                Desktop.getDesktop().browse(new URI(requestToken.getAuthorizationURL()));
            } catch (UnsupportedOperationException ignore) {
            } catch (IOException ignore) {
            } catch (URISyntaxException e) {
                throw new AssertionError(e);
            }

            /*
             * Request the username from the user.
             */
            username = "";
            while (username.length() < 1) {
                System.out.print("Provide your Twitter username: ");
                try {
                    username = bufferedReader.readLine();
                } catch (IOException ex) {
                    username = "";
                    System.out.println("Failed to read input. Please try again.");
                }
            }

            /*
             * Request the PIN from the user.
             */
            String pin = "";
            while (pin.length() < 1) {
                System.out.print("Enter the PIN provided by Twitter: ");
                try {
                    pin = bufferedReader.readLine();
                    accessToken = twitter.getOAuthAccessToken(requestToken, pin);
                } catch (TwitterException ex) {
                    if (ex.getStatusCode() == 401) {
                        pin = "";
                        System.out.println("Twitter failed to provide access tokens.  Please try again.");
                    } else {
                        System.out.println("Twitter returned an error:\r\n" + ex.getMessage());
                        System.exit(1);
                    }
                } catch (IOException ex) {
                    pin = "";
                    System.out.println("Failed to read input. Please try again.");
                }
            }

        }

        System.out.println("Twitter has provided PhantomBot with OAuth Access Tokens.");

        String twitterData = "";
        try {
            twitterData = "# Twitter Configuration File\r\n"
                    + "# Generated by PhantomBot GenerateTwitterTokens\r\n"
                    + "# If new tokens are required, run the application again.\r\n" + "#\r\n"
                    + "# PROTECT THIS FILE AS IF IT HAD YOUR TWITTER PASSWORD IN IT!\r\n" + "twitter_username="
                    + username + "\r\n" + "twitter_access_token=" + accessToken.getToken() + "\r\n"
                    + "twitter_secret_token=" + accessToken.getTokenSecret() + "\r\n";
            Files.write(Paths.get("./twitter.txt"), twitterData.getBytes(StandardCharsets.UTF_8),
                    StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
            System.out.println("Data has been successfully stored in twitter.txt.");
        } catch (IOException ex) {
            System.out.println("Unable to create twitter.txt.\r\nPlease create with the following content:\r\n"
                    + twitterData);
            System.exit(1);
        }
    } catch (TwitterException ex) {
        System.out.println("Twitter returned an error:\r\n" + ex.getMessage());
        System.exit(1);
    }
}

From source file:gohai.simpletweet.SimpleTweet.java

License:Apache License

protected void createInstance() {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    if (oAuthConsumerKey != null) {
        cb.setOAuthConsumerKey(oAuthConsumerKey);
    }//w w  w  . j a va  2 s. com
    if (oAuthConsumerSecret != null) {
        cb.setOAuthConsumerSecret(oAuthConsumerSecret);
    }
    if (oAuthAccessToken != null) {
        cb.setOAuthAccessToken(oAuthAccessToken);
    }
    if (oAuthAccessTokenSecret != null) {
        cb.setOAuthAccessTokenSecret(oAuthAccessTokenSecret);
    }
    twitter = new TwitterFactory(cb.build()).getInstance();
}

From source file:it.greenvulcano.gvesb.social.twitter.TwitterSocialAdapterAccount.java

License:Open Source License

/**
 * This method returns the interface class towards Twitter, already instantiated with the
 * account's tokens/*from w ww.j  av  a  2 s  .  c  o  m*/
 * 
 * @return {@link Twitter}
 */
public Twitter getProxyObject() {
    if (twitter != null) {
        return twitter;
    } else {
        // setting OAuth tokens
        ConfigurationBuilder confBuilder = new ConfigurationBuilder();
        if (proxy != null) {
            confBuilder.setHttpProxyHost(proxy.getHttpProxyHost());
            confBuilder.setHttpProxyPort(proxy.getHttpProxyPort());
            confBuilder.setHttpProxyUser(proxy.getHttpProxyUser());
            confBuilder.setHttpProxyPassword(proxy.getHttpProxyPassword());
        }
        confBuilder.setOAuthConsumerKey(consumerKey);
        confBuilder.setOAuthConsumerSecret(consumerSecret);
        confBuilder.setOAuthAccessToken(accessToken);
        confBuilder.setOAuthAccessTokenSecret(accessTokenSecret);
        Configuration config = confBuilder.build();
        // instantiating Twitter object
        this.twitter = new TwitterFactory(config).getInstance();
    }
    logger.info("got TwitterFactory instance.");
    return twitter;
}

From source file:it.polimi.meteocal.ejb.HandleAuthTwitterImpl.java

License:Open Source License

/**
 * Method that return the Twitter object that allows the access to the
 * Twitter API//from   w w w .j a  va2 s . c om
 *
 * @param user the user in MeteoCal
 * @return null if there was a problem with the creation of the Twitter
 * object
 */
public static Twitter getTwitterObject(User user) {
    Twitter twitter;

    if (user.getTwitterToken() == null) {
        // Twitter not connected
        return null;
    }

    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setOAuthConsumerKey(CLIENT_ID);
    builder.setOAuthConsumerSecret(CLIENT_SECRET);
    Configuration configuration = builder.build();
    TwitterFactory factory = new TwitterFactory(configuration);
    twitter = factory.getInstance();

    AccessToken at = new AccessToken(user.getTwitterToken(), user.getTwitterTokenSecret());
    LOGGER.log(Level.INFO, at);
    try {
        twitter.setOAuthAccessToken(at);
    } catch (Exception e) {
        LOGGER.log(Level.ERROR, e);
        return null;
    }
    return twitter;
}

From source file:it.polimi.meteocal.ejb.HandleAuthTwitterImpl.java

License:Open Source License

/**
 * Default constructor./*from w w  w.  j av a  2s .c  o m*/
 */
public HandleAuthTwitterImpl() {
    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setOAuthConsumerKey(CLIENT_ID);
    builder.setOAuthConsumerSecret(CLIENT_SECRET);
    Configuration configuration = builder.build();
    TwitterFactory factory = new TwitterFactory(configuration);
    twitter = factory.getInstance();
    cont = 0;
}

From source file:it.polimi.meteocal.ejb.HandleAuthTwitterImpl.java

License:Open Source License

@Override
public String getUrlLoginTwitter() {
    String urlLogin = "error.xhtml";

    try {//  w w  w  .ja v  a2 s . c  om
        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.setOAuthConsumerKey(CLIENT_ID);
        builder.setOAuthConsumerSecret(CLIENT_SECRET);
        Configuration configuration = builder.build();
        TwitterFactory factory = new TwitterFactory(configuration);
        twitter = factory.getInstance();
        //if (requestToken == null) {
        requestToken = twitter.getOAuthRequestToken(URL_BASE + "/MeteoCal-web/loginTwitter.xhtml");
        //}
        urlLogin = requestToken.getAuthenticationURL();
    } catch (TwitterException e) {
        LOGGER.log(Level.ERROR, e);
    }
    cont++;
    LOGGER.log(Level.INFO, "Conteggio: " + cont);
    LOGGER.log(Level.INFO, "URL LOGIN " + urlLogin);
    return urlLogin;
}

From source file:Jums.AllAPI.java

public static Configuration TwitterConnect() {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true);/*  w  w w. j  a va2  s.co  m*/
    cb.setOAuthConsumerKey(CONSUMERKEY);
    cb.setOAuthConsumerSecret(CONSUMERSECRET);
    cb.setOAuthAccessToken(TOKEN);
    cb.setOAuthAccessTokenSecret(TOKENSECRET);
    return cb.build();
}

From source file:Jums.SearchTweet.java

public void main(PrintWriter out) {
    try {/*from  www.  ja v a2s  . c  om*/
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true);
        cb.setOAuthConsumerKey(CONSUMERKEY);
        cb.setOAuthConsumerSecret(CONSUMERSECRET);
        cb.setOAuthAccessToken(TOKEN);
        cb.setOAuthAccessTokenSecret(TOKENSECRET);
        Twitter tw = new TwitterFactory(cb.build()).getInstance();
        User user = tw.verifyCredentials();
        List<Status> statuses = tw.getHomeTimeline();

        for (Status s : statuses) {

        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:net.wasdev.gameon.auth.twitter.TwitterAuth.java

License:Apache License

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    ConfigurationBuilder c = new ConfigurationBuilder();
    c.setOAuthConsumerKey(key).setOAuthConsumerSecret(secret);

    Twitter twitter = new TwitterFactory(c.build()).getInstance();
    request.getSession().setAttribute("twitter", twitter);

    try {/* w  ww .j  a v a  2  s.  c  o  m*/
        // twitter will tell the users browser to go to this address once
        // they are done authing.
        StringBuffer callbackURL = request.getRequestURL();
        int index = callbackURL.lastIndexOf("/");
        callbackURL.replace(index, callbackURL.length(), "").append("/TwitterCallback");

        // to initiate an auth request, twitter needs us to have a request
        // token.
        RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL.toString());

        // stash the request token in the session.
        request.getSession().setAttribute("requestToken", requestToken);

        // send the user to twitter to be authenticated.
        response.sendRedirect(requestToken.getAuthenticationURL());

    } catch (TwitterException e) {
        throw new ServletException(e);
    }

}