Java examples for Social Media:Twitter
Get Twitter access token
import twitter4j.Twitter; import twitter4j.TwitterException; import twitter4j.TwitterFactory; import twitter4j.auth.AccessToken; import twitter4j.auth.RequestToken; import java.awt.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URI; import java.net.URISyntaxException; public class Test5 { private static String consumerKey = "key"; private static String consumerSecret = "key"; public static void main(String[] args) { Twitter twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(consumerKey, consumerSecret); try {//from w w w . j a va 2s. c om RequestToken requestToken = twitter.getOAuthRequestToken(); System.out.println("Request token: " + requestToken.getToken()); System.out.println("Request token secret: " + requestToken.getTokenSecret()); System.out.println("getAuthorizationURL " + requestToken.getAuthorizationURL()); Desktop.getDesktop().browse( new URI(requestToken.getAuthorizationURL())); BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); System.out .print("Enter the PIN(if available) and hit enter after you granted access.[PIN]:"); String pin = br.readLine(); AccessToken accessToken = twitter.getOAuthAccessToken( requestToken, pin); System.out.println("Access token: " + accessToken.getToken()); System.out.println("Access token secret: " + accessToken.getTokenSecret()); } catch (TwitterException | URISyntaxException | IOException e) { e.printStackTrace(); } } }