0

我正在运行以下代码以在 twitter 上发布推文,使用twitter4j-core-2.2.6.jar.

package twitter4j.examples.tweets;
//import java.awt.List;

import java.util.List;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.OAuthAuthorization;
import twitter4j.conf.ConfigurationBuilder;

public class TwitterTest{
    private static final String ACCESS_TOKEN = ""; // proper access_token
    private static final String ACCESS_TOKEN_SECRET = ""; // proper secret_token
    private static final String CONSUMER_KEY = ""; // proper consumer_key
    private static final String CONSUMER_SECRET = ""; // proper consumer_secret

    public static void main(String[] args)
    {
        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.setOAuthAccessToken(ACCESS_TOKEN);
        builder.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
        builder.setOAuthConsumerKey(CONSUMER_KEY);
        builder.setOAuthConsumerSecret(CONSUMER_SECRET);
        OAuthAuthorization auth = new OAuthAuthorization(builder.build());

       Twitter twitter = new TwitterFactory().getInstance(auth);
       /* List<Status> statuses = null;
        try {
            statuses = twitter.getHomeTimeline();
        } catch (TwitterException e1) {
            System.out.println("Error");
            e1.printStackTrace();
        }
        System.out.println("Showing home timeline.");
        for (Status status : statuses) {
            System.out.println(status.getUser().getName() + ":" +
                               status.getText());
        }*/
        try 
        {
            twitter.updateStatus("Hello World!");
            System.out.println("yessssss\n");
        } catch (TwitterException e) 
        {
            System.err.println("Error occurred while updating the status!="+e.getErrorMessage());
            e.printStackTrace();
            return;
        }
        System.out.println("Successfully updated the status.");
    }
}

当我将状态更新为非空值时,它给了我以下错误。

TwitterException{exceptionCode=[126cde36-60c4a990 126cde36-60c4a966], statusCode=-1, retryAfter=-1, rateLimitStatus=null, featureSpecificRateLimitStatus=null, version=2.2.6}
        at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:200)
        at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65)
        at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:102)
        at twitter4j.TwitterImpl.post(TwitterImpl.java:1762)
        at twitter4j.TwitterImpl.updateStatus(TwitterImpl.java:399)
        at twitter4j.examples.tweets.TwitterTest.main(TwitterTest.java:42)
    Caused by: java.net.ConnectException: Connection refused
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:327)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:193)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)
        at java.net.Socket.connect(Socket.java:546)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:173)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:409)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:530)
        at sun.net.www.http.HttpClient.<init>(HttpClient.java:240)
        at sun.net.www.http.HttpClient.New(HttpClient.java:321)
        at sun.net.www.http.HttpClient.New(HttpClient.java:338)

我怎样才能解决这个问题?

连接被拒绝的相关讨论可以在 Internet 上找到:

4

1 回答 1

0

尝试改变

OAuthAuthorization auth = new OAuthAuthorization(builder.build());
Twitter twitter = new TwitterFactory().getInstance(auth);

TwitterFactory twitterFactory = new TwitterFactory(builder.build());
Twitter twitter = twitterFactory.getInstance();

那应该工作

于 2013-04-09T08:08:12.417 回答