Example usage for twitter4j Twitter getOAuthRequestToken

List of usage examples for twitter4j Twitter getOAuthRequestToken

Introduction

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

Prototype

RequestToken getOAuthRequestToken() throws TwitterException;

Source Link

Document

Retrieves a request token

Usage

From source file:jp.ky.twittersample.UpdateStatus.java

License:Apache License

/**
 * Usage: java twitter4j.examples.tweets.UpdateStatus [text]
 *
 * @param args message/*from  w  w w  .ja  v a2 s. c  om*/
 */
public static void main(String[] args) {
    //        if (args.length < 1) {
    //            System.out.println("Usage: java twitter4j.examples.tweets.UpdateStatus [text]");
    //            System.exit(-1);
    //        }
    String message = "Test5";
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        try {
            // get request token.
            // this will throw IllegalStateException if access token is already available
            RequestToken requestToken = twitter.getOAuthRequestToken();
            System.out.println("Got request token.");
            System.out.println("Request token: " + requestToken.getToken());
            System.out.println("Request token secret: " + requestToken.getTokenSecret());
            AccessToken accessToken = null;

            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            while (null == accessToken) {
                System.out.println("Open the following URL and grant access to your account:");
                System.out.println(requestToken.getAuthorizationURL());
                System.out.print("Enter the PIN(if available) and hit enter after you granted access.[PIN]:");
                String pin = br.readLine();
                try {
                    if (pin.length() > 0) {
                        accessToken = twitter.getOAuthAccessToken(requestToken, pin);
                    } else {
                        accessToken = twitter.getOAuthAccessToken(requestToken);
                    }
                } catch (TwitterException te) {
                    if (401 == te.getStatusCode()) {
                        System.out.println("Unable to get the access token.");
                    } else {
                        te.printStackTrace();
                    }
                }
            }
            System.out.println("Got access token.");
            System.out.println("Access token: " + accessToken.getToken());
            System.out.println("Access token secret: " + accessToken.getTokenSecret());
        } catch (IllegalStateException ie) {
            // access token is already available, or consumer key/secret is not set.
            if (!twitter.getAuthorization().isEnabled()) {
                System.out.println("OAuth consumer key/secret is not set.");
                System.exit(-1);
            }
        }

        //Status status = twitter.updateStatus(message);
        //System.out.println("Successfully updated the status to [" + status.getText() + "].");
        AccountSettings as = twitter.getAccountSettings();
        System.out.println("accountsettings:" + as.getScreenName());

        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get timeline: " + te.getMessage());
        System.exit(-1);
    } catch (IOException ioe) {
        ioe.printStackTrace();
        System.out.println("Failed to read the system input.");
        System.exit(-1);
    }
}

From source file:jp.xxxxxxxx.l3fish.twnyaan.service.AuthenticationService.java

License:Open Source License

/**
 * RequestToken????URL???????PIN???/* ww  w. j  av  a 2 s  .  com*/
 * RequestToken??????{@code null}?
 *
 * @return ???RequestToken??????????{@code null}
 */
public RequestToken authorize() {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(TwitterAPIKey.getConsumerKey())
            .setOAuthConsumerSecret(TwitterAPIKey.getConsumerSecret());
    Twitter twitter = new TwitterFactory(cb.build()).getInstance();

    RequestToken requestToken = null;
    try {
        requestToken = twitter.getOAuthRequestToken();
        URI authorizationURI = new URI(requestToken.getAuthorizationURL());
        Desktop desktop = Desktop.getDesktop();
        desktop.browse(authorizationURI);
    } catch (URISyntaxException | IOException e) {
        System.err.println(ErrorCode.CANNOT_OPEN_AUTHORIZATION_URI);
    } catch (TwitterException e) {
        System.err.println(ErrorCode.TWITTER_SERVICE_UNAVAILABLE);
    }

    return requestToken;
}

From source file:net.chrissearle.flickrvote.service.cli.TwitterOAuthCLI.java

License:Apache License

public void authorize(String key, String secret) throws TwitterException, IOException {
    Twitter twitter = new TwitterFactory().getInstance();

    twitter.setOAuthConsumer(key, secret);

    RequestToken requestToken = twitter.getOAuthRequestToken();

    AccessToken accessToken = null;/*from   w w w .j  a va2 s.  c  o  m*/

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    while (null == accessToken) {
        System.out.println("Open the following URL and grant access to your account:");
        System.out.println(requestToken.getAuthorizationURL());
        System.out.print("Enter the PIN(if available) or just hit enter.[PIN]:");

        String pin = br.readLine();
        try {
            if (pin.length() > 0) {
                accessToken = twitter.getOAuthAccessToken(requestToken, pin);
            } else {
                accessToken = twitter.getOAuthAccessToken();
            }

            System.out.println("Token: " + accessToken.getToken());
            System.out.println("TokenSecret: " + accessToken.getTokenSecret());
        } catch (TwitterException te) {
            if (401 == te.getStatusCode()) {
                System.out.println("Unable to get the access token.");
            } else {
                te.printStackTrace();
            }
        }
    }
}

From source file:net.tiger.star.nsen.action.IndexAction.java

License:Apache License

@Execute(validator = false, redirect = true)
public String login() {
    Twitter twitter = new TwitterFactory().getInstance();

    // RequestToken?Twitter??
    try {//from   w  ww.java2  s .c o m
        userDataDto.requestToken = twitter.getOAuthRequestToken();
    } catch (TwitterException e) {
        return null;
    }
    return userDataDto.requestToken.getAuthorizationURL();
}

From source file:onl.area51.a51li.twitter.TwitterAuth.java

License:Apache License

public static void main(String args[]) throws Exception {
    final String consumerKey = args[0];
    final String consumerSecret = args[1];

    // The factory instance is re-useable and thread safe.
    Twitter twitter = TwitterFactory.getSingleton();
    twitter.setOAuthConsumer(consumerKey, consumerSecret);
    RequestToken requestToken = twitter.getOAuthRequestToken();
    AccessToken accessToken = null;//from  w ww  .j  a  va  2  s .c o m
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    while (null == accessToken) {
        System.out.println("Open the following URL and grant access to your account:");
        System.out.println(requestToken.getAuthorizationURL());
        System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
        String pin = br.readLine();
        try {
            if (pin.length() > 0) {
                accessToken = twitter.getOAuthAccessToken(requestToken, pin);
            } else {
                accessToken = twitter.getOAuthAccessToken();
            }
        } catch (TwitterException te) {
            if (401 == te.getStatusCode()) {
                System.out.println("Unable to get the access token.");
            } else {
                te.printStackTrace();
            }
        }
    }
    //persist to the accessToken for future reference.
    storeAccessToken(twitter.verifyCredentials().getId(), accessToken);
    Status status = twitter.updateStatus(args[0]);
    System.out.println("Successfully updated the status to [" + status.getText() + "].");
    System.exit(0);
}

From source file:org.encuestame.social.api.templates.TwitterAPITemplate.java

License:Apache License

/**
 * Get Twitter Ping.//from  w ww  . j av a  2s .  c om
 * @param consumerKey consumer key
 * @param consumerSecret consumer secret
 * @return {@link RequestToken}
 * @throws TwitterException exception
 */
public RequestToken getTwitterPing() throws TwitterException {
    if (consumerKey == null) {
        throw new IllegalArgumentException("Consumer key is missing");
    }
    if (consumerSecret == null) {
        throw new IllegalArgumentException("Consumer secret is missing");
    }
    final Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(consumerKey, consumerSecret);
    return twitter.getOAuthRequestToken();
}

From source file:org.exoplatform.extensions.twitter.services.TwitterService.java

License:Open Source License

public RequestToken getAuthorizationUrl() {

    RequestToken requestToken = null;/* w ww  .j a  v a 2  s . c o m*/
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(consumerKey, secretKey);
        requestToken = twitter.getOAuthRequestToken();

    } catch (TwitterException ex) {
        Logger.getLogger(TwitterService.class.getName()).log(Level.SEVERE, null, ex);
    }

    return requestToken;
}

From source file:org.luwrain.app.twitter.Auth.java

License:Open Source License

public static void main(String[] args) {
    if (args.length < 2) {
        System.err.println("You must provide consumer key and consumer secret");
        return;/*from   w w  w .j a v a2 s .  com*/
    }
    Twitter twitter = null;
    try {
        ConfigurationLuwrain conf = new ConfigurationLuwrain(args[0], args[1], null, null);
        twitter = new TwitterFactory(conf).getInstance();
        final RequestToken requestToken = twitter.getOAuthRequestToken();
        System.out.println("Got request token.");
        System.out.println("Request token: " + requestToken.getToken());
        System.out.println("Request token secret: " + requestToken.getTokenSecret());
        AccessToken accessToken = null;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while (accessToken == null) {
            System.out.println("Open the following URL and grant access to your account:");
            System.out.println(requestToken.getAuthorizationURL());
            System.out.print("Enter the PIN(if available) and hit enter after you granted access.[PIN]:");
            String pin = br.readLine();
            try {
                if (pin.length() > 0) {
                    accessToken = twitter.getOAuthAccessToken(requestToken, pin);
                } else {
                    accessToken = twitter.getOAuthAccessToken(requestToken);
                }
            } catch (TwitterException te) {
                if (te.getStatusCode() == 401) {
                    System.out.println("Unable to get the access token.");
                } else {
                    te.printStackTrace();
                }
            }
        } //while();
        System.out.println("Got access token.");
        System.out.println("Access token: " + accessToken.getToken());
        System.out.println("Access token secret: " + accessToken.getTokenSecret());
    } catch (IllegalStateException e) {
        if (!twitter.getAuthorization().isEnabled())
            System.out.println("OAuth consumer key/secret is not set.");
        else
            e.printStackTrace();
    } catch (TwitterException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.n52.twitter.TwitterAccessTokenRetriever.java

License:Open Source License

public static void main(String args[]) throws Exception {
    String[] props = readProperties();
    Twitter twitter = TwitterFactory.getSingleton();
    twitter.setOAuthConsumer(props[0], props[1]);
    RequestToken requestToken = twitter.getOAuthRequestToken();
    AccessToken accessToken = null;// w ww.  j  a va2s . c  om
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    while (null == accessToken) {
        LOGGER.info("Open the following URL and grant access to your account:");
        LOGGER.info(requestToken.getAuthorizationURL());
        LOGGER.info("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
        String pin = br.readLine();
        try {
            if (pin.length() > 0) {
                accessToken = twitter.getOAuthAccessToken(requestToken, pin);
            } else {
                accessToken = twitter.getOAuthAccessToken();
            }
        } catch (TwitterException te) {
            if (401 == te.getStatusCode()) {
                LOGGER.error("Unable to get the access token.");
            } else {
                LOGGER.error(te.getErrorMessage(), te);
            }
        }
    }
    storeAccessToken(accessToken.getToken(), accessToken.getTokenSecret());
}

From source file:org.opennms.netmgt.notifd.MicroblogAuthorization.java

License:Open Source License

public MicroblogAuthorization(final Twitter twitter) throws MicroblogAuthorizationException {
    m_twitter = twitter;//  w  ww  . j a va 2  s.  c  om
    try {
        m_requestToken = twitter.getOAuthRequestToken();
    } catch (final TwitterException e) {
        throw new MicroblogAuthorizationException("Unable to get OAuth request token", e);
    }
}