简体   繁体   中英

Login to twitter and tweet from Java (web-app)

I want to write a simple java (or java web app) program that will allow a user to log in and post a twit. I don't even need a user interface. I can simply hard code the twit, userId, and password. I just want to know the process. I have been looking for a while now, and I have had no success so far. The following code which was finally supposed to work does not work.

The code is a simple application as opposed to a web-app. Does anyone have some code that will work with the present Twitter API? I have been trying to use twitter4j.

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;

public class TwitterUtils {

public static void main(String[] args) {
    try {

        final String consumerKey = "**********";
        final String consumerSecret = "**********";
        Twitter twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(consumerKey, consumerSecret);
        RequestToken requestToken = twitter.getOAuthRequestToken();

        String token = requestToken.getToken();
        String tokenSecret = requestToken.getTokenSecret();
        System.out.println("My token :: " + token);
        System.out.println("My token Secret :: " + tokenSecret);

        //AccessToken a = new AccessToken(token, tokenSecret);
        //twitter.setOAuthAccessToken(a);
        twitter.updateStatus("If you're reading this on Twitter, it worked!");

    } catch (TwitterException e) {
        e.printStackTrace();
    }
}//main
}//TwitterUtils

With the AccessToken lines commented I get the error

Exception in thread "main" java.lang.IllegalStateException: Authentication credentials are missing. See http://twitter4j.org/configuration.html for the detail.     at twitter4j.TwitterBaseImpl.ensureAuthorizationEnabled(TwitterBaseImpl.java:205)
at twitter4j.TwitterImpl.updateStatus(TwitterImpl.java:453)
at playaround.TwitterUtils.main(TwitterUtils.java:55)

Java Result: 1

When I uncomment the lines, the error reads

Exception in thread "main" java.lang.IllegalArgumentException: Invalid access token format. at twitter4j.auth.AccessToken.<init>(AccessToken.java:50)
at playaround.TwitterUtils.main(TwitterUtils.java:53)

Does anyone have a complete solution I may use? Thanks.

Through this link ( http://module.minic.ro/how-to-make-a-twitter-application-tutorial/ ) you can generate consumer key,consumer secret, access token, and access token secret keys

Use this code when you get keys for Twitter object:

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("*********************")
.setOAuthConsumerSecret("******************************************")
.setOAuthAccessToken("**************************************************")
.setOAuthAccessTokenSecret("******************************************");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();

http://twitter4j.org/en/configuration.html

Besides consumerKey and consumerSecret (your application's key and secret) you need accessToken from the user that is using your application. You get this accessToken from Twitter using OAuth protocol.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM