如何在twitter4j中传递用户名和密码?

时间:2012-03-05 06:59:39

标签: java twitter4j

执行以下代码时出现以下异常。另外我想知道如何使用twitter4j传递用户名和密码?

例外:

Exception in thread "main" java.lang.NoSuchMethodError: twitter4j.Twitter.<init>(Ltwitter4j/conf/Configuration;Ltwitter4j/http/Authorization;)V
        at twitter4j.TwitterFactory.getInstance(TwitterFactory.java:151)
        at twitter4j.TwitterFactory.getInstance(TwitterFactory.java:81)
        at twitter.UpdateStatus.main(UpdateStatus.java:38)

我的代码:

package twitter;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;

import java.io.IOException;
import twitter4j.http.AccessToken;

public final class UpdateStatus {

    public static void main(String[] args) throws IOException {

        String testPost = "hello from otc";
        String consumerKey = "xxxxxxxxxxxxxx";
        String consumerSecret = "yyyyyyyyyyyyyyyy";
        String accessToken = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
        String accessSecret = "fffffffffffffffffffffffffff";

        // ConfigurationBuilder cb = new ConfigurationBuilder();
        //
        // cb.setDebugEnabled(true)
        // .setOAuthConsumerKey(consumerKey)
        // .setOAuthConsumerSecret(consumerSecret)
        // .setOAuthAccessToken(accessToken)
        // .setOAuthAccessTokenSecret(accessSecret);

        try {
            TwitterFactory factory = new TwitterFactory();
            Twitter twitter = factory.getInstance();
            AccessToken accestoken = new AccessToken(accessToken, accessSecret);

            // twitter.setOAuthConsumer(consumerKey, consumerSecret);
            twitter.setOAuthAccessToken(accestoken);
            Status status = twitter.updateStatus(testPost);
            System.out.println("it worked!");
            if (status.getId() == 0) {
                System.out.println("Error occured while posting tweets to twitter");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

正如你的代码中所见, 使用twitter4j无需传递用户名和密码。

消费者密钥/秘密和令牌密钥/秘密足以识别用户。