Java tutorial
//$URL: http://192.168.1.6/svn/Coding/privat/Piraten/retweeter/src/main/java/de/dev/eth0/retweeter/TwitterAuthenticator.java $ //$Id: TwitterAuthenticator.java 25 2012-05-08 11:27:33Z deveth0 $ package de.dev.eth0.retweeter; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import twitter4j.Twitter; import twitter4j.TwitterException; import twitter4j.auth.AccessToken; import twitter4j.auth.RequestToken; /* * ---------------------------------------------------------------------------- * "THE BEER-WARE LICENSE": * <amuthmann@dev-eth0.de> wrote this file. As long as you retain this notice you * can do whatever you want with this stuff. If we meet some day, and you think * this stuff is worth it, you can buy me a beer in return * ---------------------------------------------------------------------------- */ /** * Helper to authenticate a user's account at twitter for usage of this app * @author deveth0 */ public class TwitterAuthenticator extends AbstractTwitterClient { private final static Logger logger = LoggerFactory.getLogger(TwitterAuthenticator.class); public TwitterAuthenticator(Config _config) { super(_config); } public void authenticate() throws TwitterException, IOException { Twitter twitter = getTwitter(); twitter.setOAuthAccessToken(null); AccessToken accessToken = null; RequestToken requestToken = twitter.getOAuthRequestToken(); 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) { logger.error("Couldnt get access token", te); } } System.out.println("Your accessToken: " + accessToken.getToken()); System.out.println("Your accessTokenSecret: " + accessToken.getTokenSecret()); System.out.println("add this to your config: "); System.out.println("\"accessToken\":\"" + accessToken.getToken() + "\","); System.out.println("\"accessSecret\":\"" + accessToken.getTokenSecret() + "\","); } }