Example usage for twitter4j.auth AccessToken AccessToken

List of usage examples for twitter4j.auth AccessToken AccessToken

Introduction

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

Prototype

public AccessToken(String token, String tokenSecret, long userId) 

Source Link

Usage

From source file:RandomPicBot.java

License:Open Source License

public static void main(String[] args) {
    /*/* w w w.j  a va  2  s . c  o  m*/
     * initialize the yaml setup
     * reads from keys.yml in the classpath, or same directory as the program
     */
    Yaml yaml = new Yaml();
    InputStream fileInput = null;
    try {
        fileInput = new FileInputStream(new File("keys.yml"));
    } catch (FileNotFoundException e1) {
        System.out.println("keys.yml not found!");
        e1.printStackTrace();
        System.exit(0);
    }
    @SuppressWarnings("unchecked") //unchecked cast, who cares.
    Map<String, String> map = (Map<String, String>) yaml.load(fileInput);

    /*
     * extracts values from the map
     */

    String CONSUMER_KEY = map.get("CONSUMER_KEY");
    String CONSUMER_SECRET = map.get("CONSUMER_SECRET");
    String TOKEN = map.get("TOKEN");
    String TOKEN_SECRET = map.get("TOKEN_SECRET");
    long USER_ID = Long.parseLong(map.get("USER_ID"));

    /*
     * initialize Twitter using the keys we got from the yaml file
     */

    Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
    twitter.setOAuthAccessToken(new AccessToken(TOKEN, TOKEN_SECRET, USER_ID));

    /*
     * set up our picture folder
     * gets the folder from the command line argument
     * may change this to yaml too in the future
     */
    File directory = new File(args[0]);
    File[] pictures = directory.listFiles(new FileFilter() {
        /*
         * check to make sure the file is under 3mb(with some wiggle room) and is an acceptable image(non-Javadoc)
         * @see java.io.FileFilter#accept(java.io.File)
         */
        public boolean accept(File pathname) {
            if (pathname.isFile() && pathname.length() < 3000000 && isImage(pathname)) {
                return true;
            } else
                return false;
        }
    });

    System.out.println(pictures.length + " usable files found.");

    Random rand = new Random();

    /*
     * convert our minute value into milliseconds since thats what Thread.sleep() uses
     */

    int WAIT_TIME = Integer.parseInt(args[1]) * 60 * 1000;

    String statusText = "";
    if (args[2] != null) {
        for (int i = 2; i < args.length; i++) {
            statusText += args[i] + " ";
        }
        statusText.trim();
    }

    if (statusText.length() > 117) {
        System.out.println("Your message is too long!");
        System.out.println("Only messages up to 117 characters are supported!");
        System.exit(5);
    }

    StatusUpdate status = new StatusUpdate(statusText);

    /*
     * main loop
     * generate a random number to choose our image with
     * then upload the image to twitter
     * then wait for the defined time(gotten from the command line in minutes)
     */

    while (true) {
        int index = rand.nextInt(pictures.length);
        status.setMedia(pictures[index]);

        try {
            System.out.print("uploading picture: " + pictures[index].getName());
            if (!statusText.equals(""))
                System.out.println(" with text: " + statusText);
            else
                System.out.println();
            twitter.updateStatus(status);
        } catch (TwitterException e) {
            System.out.println("Problem uploading the file!");
            e.printStackTrace();
        }
        try {
            Thread.sleep(WAIT_TIME);
        } catch (InterruptedException e) {
            System.out.println("Program was interrupted!");
            e.printStackTrace();
        }
    }
}

From source file:jp.xxxxxxxx.l3fish.twnyaan.model.AccessTokenBean.java

License:Open Source License

public AccessToken toAccessToken() {
    return new AccessToken(accessToken, accessTokenSecret, userId);
}

From source file:net.nokok.twitduke.core.io.AccessTokenPropertyReader.java

License:Open Source License

/**
 * ????????/*from  w  w w .  ja  v  a 2s.c o  m*/
 * ??????null???
 *
 * @param path ?
 *
 * @return 
 */
public AccessToken readAccessTokenUnsafe(String path) {
    File file = new File(path);
    Properties properties = new Properties();
    try {
        try (FileInputStream inputStream = new FileInputStream(file)) {
            properties.load(inputStream);
        }
        String token = properties.getProperty(PropertyKey.TOKEN);
        String secret = properties.getProperty(PropertyKey.TOKEN_SECRET);
        long id = Long.parseLong(properties.getProperty(PropertyKey.USER_ID));
        String screenName = properties.getProperty(PropertyKey.SCREEN_NAME);
        AccessToken accessToken = new AccessToken(token, secret, id);
        setScreenNameWithReflection(accessToken, screenName);
        return accessToken;
    } catch (IOException | NumberFormatException e) {
        return null;
    }
}

From source file:net.nokok.twitduke.core.type.AccessTokenProperty.java

License:Open Source License

/**
 * ??????/*  w  ww . j av a2  s .  c  o  m*/
 *
 * @param properties
 */
public AccessTokenProperty(Properties properties) {
    String token = Optional.of(properties.getProperty(TOKEN_KEY)).orElseThrow(IllegalArgumentException::new);
    String secret = Optional.of(properties.getProperty(TOKEN_SECRET_KEY))
            .orElseThrow(IllegalArgumentException::new);
    Optional.ofNullable(properties.getProperty(SCREEN_NAME_KEY)).orElseThrow(IllegalArgumentException::new);
    String id = Optional.ofNullable(properties.getProperty(USER_ID_KEY))
            .orElseThrow(IllegalArgumentException::new);
    this.properties = properties;
    accessToken = new AccessToken(token, secret, Long.parseLong(id));
}

From source file:nz.net.speakman.android.dreamintweets.preferences.DreamPreferences.java

License:Apache License

public AccessToken getAccessToken() {
    SharedPreferences prefs = getPrefs();
    String token = prefs.getString(KEY_ACCESS_TOKEN, "");
    String tokenSecret = prefs.getString(KEY_ACCESS_TOKEN_SECRET, "");
    long userId = prefs.getLong(KEY_ACCESS_TOKEN_USER_ID, 0);
    AccessToken at = new AccessToken(token, tokenSecret, userId);
    return at;//from   w  ww  . ja va  2s. c  o m
}