Search in sources :

Example 11 with AccessToken

use of twitter4j.auth.AccessToken in project twitter4j by yusuke.

the class UpdateStatus method main.

/**
     * Usage: java twitter4j.examples.tweets.UpdateStatus [text]
     *
     * @param args message
     */
public static void main(String[] args) {
    if (args.length < 1) {
        System.out.println("Usage: java twitter4j.examples.tweets.UpdateStatus [text]");
        System.exit(-1);
    }
    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(args[0]);
        System.out.println("Successfully updated the status to [" + status.getText() + "].");
        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);
    }
}
Also used : Status(twitter4j.Status) InputStreamReader(java.io.InputStreamReader) RequestToken(twitter4j.auth.RequestToken) AccessToken(twitter4j.auth.AccessToken) BufferedReader(java.io.BufferedReader) Twitter(twitter4j.Twitter) TwitterFactory(twitter4j.TwitterFactory) IOException(java.io.IOException) TwitterException(twitter4j.TwitterException)

Example 12 with AccessToken

use of twitter4j.auth.AccessToken in project opennms by OpenNMS.

the class MicroblogClient method main.

public static void main(final String[] args) throws Exception {
    System.out.println("=== Configure Microblog Authentication ===");
    System.out.println("");
    final String configPath = System.getProperty("opennms.home") + File.separator + "etc" + File.separator + "microblog-configuration.xml";
    final File configFile = new File(configPath);
    if (!configFile.exists())
        usage();
    String profile = null;
    if (args.length > 0) {
        profile = args[0];
    }
    try {
        final MicroblogClient client = new MicroblogClient(new FileSystemResource(configFile));
        int step = 1;
        final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        if (!client.hasOAuth(profile)) {
            System.out.println("This utility is for connecting OpenNMS notifications with Twitter, or any other");
            System.out.println("microblog site which uses OAuth.  These examples will use Twitter URLs, but you");
            System.out.println("should be able to do the equivalent with any Twitter-compatible site like identi.ca.");
            System.out.println("");
            System.out.println("If you wish to use a username and password instead, just enter them into your");
            System.out.println("microblog-configuration.xml file.");
            System.out.println("");
            System.out.println("Step " + step++ + ".  Go to https://twitter.com/oauth_clients/new and create a Twitter");
            System.out.println("\"application\" for your OpenNMS install.  If you have already created an application,");
            System.out.println("you can get the info you need for the next steps at https://dev.twitter.com/apps/");
            System.out.println("instead.  Make sure you go to 'Keys and Access Tokens' and configure the 'Access Level'");
            System.out.println("to allow 'Read, Write and Access direct messages'.");
            System.out.println("");
            System.out.print("Step " + step++ + ".  Enter your consumer key: ");
            final String consumerKey = br.readLine();
            System.out.println("");
            System.out.print("Step " + step++ + ".  Enter your consumer secret: ");
            final String consumerSecret = br.readLine();
            System.out.println("");
            client.getProfile(profile).setOauthConsumerKey(consumerKey);
            client.getProfile(profile).setOauthConsumerSecret(consumerSecret);
            if (!client.hasOAuth(profile)) {
                System.err.println("Something went wrong, either your consumer key or secret were empty.  Bailing.");
                System.exit(1);
            }
        }
        final MicroblogAuthorization auth = client.requestAuthorization(profile);
        System.out.println("Step " + step++ + ".  Go to " + auth.getUrl());
        System.out.println("in your browser and authorize OpenNMS.");
        System.out.println("");
        System.out.print("Step " + step++ + ".  Type your PIN from the web page, or hit ENTER if there is no PIN: ");
        final String pin = br.readLine();
        AccessToken token = null;
        if (pin == null || pin.length() == 0 || !pin.matches("^[0-9]*$")) {
            System.err.println("No pin, or pin input was not numeric.  Trying pinless auth.");
            token = auth.retrieveToken();
        } else {
            token = auth.retrieveToken(pin);
        }
        System.out.println("");
        System.out.println("Step " + step + ".  There is no step " + step++ + ".");
        System.out.println("");
        System.out.print("Saving tokens to " + configPath + "... ");
        client.saveAccessToken(profile, token);
        System.out.println("done");
        System.out.println("");
    } catch (final Exception e) {
        System.err.println("Failed to get access token.");
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        throw new RuntimeException(e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) AccessToken(twitter4j.auth.AccessToken) BufferedReader(java.io.BufferedReader) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) MicroblogAuthorizationException(org.opennms.netmgt.notifd.MicroblogAuthorization.MicroblogAuthorizationException) IOException(java.io.IOException)

Example 13 with AccessToken

use of twitter4j.auth.AccessToken in project intellij-community by JetBrains.

the class StudyTwitterUtils method setAuthInfoInTwitter.

/**
   * Set access token and token secret in Twitter instance
   */
private static void setAuthInfoInTwitter(Twitter twitter, @NotNull String accessToken, @NotNull String tokenSecret) {
    AccessToken token = new AccessToken(accessToken, tokenSecret);
    twitter.setOAuthAccessToken(token);
}
Also used : AccessToken(twitter4j.auth.AccessToken)

Example 14 with AccessToken

use of twitter4j.auth.AccessToken in project intellij-community by JetBrains.

the class StudyTwitterUtils method authorizeAndUpdateStatus.

/**
   * Show twitter dialog, asking user to tweet about his achievements. Post tweet with provided by panel
   * media and text. 
   * As a result of succeeded tweet twitter website is opened in default browser.
   */
public static void authorizeAndUpdateStatus(@NotNull final Project project, @NotNull final Twitter twitter, @NotNull final StudyTwitterUtils.TwitterDialogPanel panel) throws TwitterException {
    RequestToken requestToken = twitter.getOAuthRequestToken();
    BrowserUtil.browse(requestToken.getAuthorizationURL());
    ApplicationManager.getApplication().invokeLater(() -> {
        String pin = createAndShowPinDialog();
        if (pin != null) {
            try {
                AccessToken token = twitter.getOAuthAccessToken(requestToken, pin);
                StudyTwitterPluginConfigurator configurator = StudyUtils.getTwitterConfigurator(project);
                if (configurator != null) {
                    configurator.storeTwitterTokens(project, token.getToken(), token.getTokenSecret());
                    updateStatus(panel, twitter);
                } else {
                    LOG.warn("No twitter configurator is provided for the plugin");
                }
            } catch (TwitterException e) {
                if (e.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
                    LOG.warn("Unable to get the access token.");
                    LOG.warn(e.getMessage());
                }
            } catch (IOException e) {
                LOG.warn(e.getMessage());
            }
        }
    });
}
Also used : RequestToken(twitter4j.auth.RequestToken) AccessToken(twitter4j.auth.AccessToken) IOException(java.io.IOException) StudyTwitterPluginConfigurator(com.jetbrains.edu.learning.StudyTwitterPluginConfigurator) TwitterException(twitter4j.TwitterException)

Example 15 with AccessToken

use of twitter4j.auth.AccessToken in project quorrabot by GloriousEggroll.

the class TwitterAPI method loadAccessToken.

public void loadAccessToken(String token, String tokenSecret) {
    this.access_token = new AccessToken(token, tokenSecret);
    twitter.setOAuthAccessToken(access_token);
}
Also used : AccessToken(twitter4j.auth.AccessToken)

Aggregations

AccessToken (twitter4j.auth.AccessToken)22 IOException (java.io.IOException)8 TwitterException (twitter4j.TwitterException)7 Twitter (twitter4j.Twitter)5 TwitterFactory (twitter4j.TwitterFactory)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 InetSocketAddress (java.net.InetSocketAddress)4 Socket (java.net.Socket)4 GeneralSecurityException (java.security.GeneralSecurityException)4 Calendar (java.util.Calendar)4 SSLContext (javax.net.ssl.SSLContext)4 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)4 DefaultHttpClientConnection (org.apache.http.impl.DefaultHttpClientConnection)4 BasicHttpEntityEnclosingRequest (org.apache.http.message.BasicHttpEntityEnclosingRequest)4 BasicHttpParams (org.apache.http.params.BasicHttpParams)4 HttpParams (org.apache.http.params.HttpParams)4 RequestToken (twitter4j.auth.RequestToken)4 T2WUser (h2weibo.model.T2WUser)3 File (java.io.File)3 KeyManagementException (java.security.KeyManagementException)3