Skip to content

How to Get Twitter Token And Token Secret

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;
public class UpdateStatus {
   * @param args
  public static void main(String args[]) throws Exception {
	  // The factory instance is re-useable and thread safe.
      Twitter twitter = new TwitterFactory().getInstance();

//insert the appropriate consumer key and consumer secret here

      twitter.setOAuthConsumer(token, secret);
      RequestToken requestToken = twitter.getOAuthRequestToken();
      AccessToken accessToken = null;
      BufferedReader br = new BufferedReader(new InputStreamReader(;
      while (null == accessToken) {
        System.out.println("Open the following URL and grant access to your account:");
        System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
        String pin = br.readLine();
           if(pin.length() > 0){
             accessToken = twitter.getOAuthAccessToken(requestToken, pin);
             accessToken = twitter.getOAuthAccessToken();
        } catch (TwitterException te) {
          if(401 == te.getStatusCode()){
            System.out.println("Unable to get the access token.");
      //persist to the accessToken for future reference.
      System.out.println("token : " + accessToken.getToken());
      System.out.println("tokenSecret : " + accessToken.getTokenSecret());
      //storeAccessToken(twitter.verifyCredentials().getId() , accessToken);
      Status status = twitter.updateStatus("This is my Twitter Status");
      System.out.println("Successfully updated the status to [" + status.getText() + "].");

From → Java Examples

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: