0

第一次发海报,但我真的被卡住了。我正在做一个小项目,我正在尝试使用 netbeans 项目发送一条推文。我正在使用 twitter4j,似乎最近事情已经改变到您必须使用他们的 OAuth 功能的地方。我在 twitter 上创建了一个应用程序并尝试了一些代码,但我一直收到此错误:线程“main”中的异常连接超时相关讨论可以在 Internet 上:

http://www.google.co.jp/search?q=b2b52c28 or
http://www.google.co.jp/search?q=1b442895 
TwitterException{exceptionCode=[b2b52c28-1b442895 b2b52c28-1b44286b], statusCode=-1, retryAfter=-1, rateLimitStatus=null, featureSpecificRateLimitStatus=null, version=2.2.5}
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:1929)
at twitter4j.TwitterImpl.updateStatus(TwitterImpl.java:433)
at login.Login.start(Login.java:36)
at login.Login.main(Login.java:63)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:75)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:388)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:483)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:213)
at sun.net.www.http.HttpClient.New(HttpClient.java:300)
at sun.net.www.http.HttpClient.New(HttpClient.java:316)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:992)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:928)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:846)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1087)
at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:158)
... 6 more
Java Result: 1

我不完全确定我做错了什么。下面是我尝试过的代码。

package login;

import java.io.IOException;

import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;

public class Login {

    private final static String CONSUMER_KEY = "******";
    private final static String CONSUMER_KEY_SECRET =
     "******";

    public void start() throws TwitterException, IOException {

 Twitter twitter = new TwitterFactory().getInstance();
 twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_KEY_SECRET);

 // here's the difference
 String accessToken = getSavedAccessToken();
 String accessTokenSecret = getSavedAccessTokenSecret();
 AccessToken oathAccessToken = new AccessToken(accessToken,
  accessTokenSecret);

 twitter.setOAuthAccessToken(oathAccessToken);
 // end of difference

 twitter.updateStatus("Hi, im updating status again from Namex Tweet for Demo");

 System.out.println("\nMy Timeline:");

 // I'm reading your timeline
 ResponseList list = twitter.getHomeTimeline();
/* for (Status each : list) {

     System.out.println("Sent by: @" + each.getUser().getScreenName()
      + " - " + each.getUser().getName() + "\n" + each.getText()
      + "\n");
 }*/

    }

    private String getSavedAccessTokenSecret() {
 // consider this is method to get your previously saved Access Token
 // Secret
 return "oC8tImRFL6i8TuRkTEaIcWsF8oY4SL5iTGNkG9O0Q";
    }

    private String getSavedAccessToken() {
 // consider this is method to get your previously saved Access Token
 return "102333999-M4W1Jtp8y8QY8RH7OxGWbM5Len5xOeeTUuG7QfcY";
    }

    public static void main(String[] args) throws Exception {
            new Login().start();
    }

}
4

2 回答 2

4

我可以建议一条替代路线吗..

我最近一直在搞乱 twitter4j,但我的处理方式略有不同——我找到了一种很好且简单的方法来使用 ConfigurationBuilder 对象对客户端进行身份验证,并将其传递给获取所需 Twitter 对象实例的工厂。

package main;

import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.conf.ConfigurationBuilder;

public class Base {

    protected Twitter twitter;
    //protected TwitterStream twitterStream;
    private ConfigurationBuilder configBuilder;

    public Base(){

        configBuilder = new ConfigurationBuilder();
        configBuilder.setDebugEnabled(true);        
        configBuilder.setOAuthConsumerKey("[consumer key here]");
        configBuilder.setOAuthConsumerSecret("[consumer secret key here]");
        configBuilder.setOAuthAccessToken("[OAuthAccessToken here]");
        configBuilder.setOAuthAccessTokenSecret("[secret OAuthAccessToken here]");

        //use the ConfigBuilder.build() method and pass the result to the TwitterFactory
        TwitterFactory tf = new TwitterFactory(configBuilder.build());
        //you can now get authenticated instance of Twitter object.
        twitter = tf.getInstance();
    }
}

然后,您可以使用实现所需功能的子类来扩展此类,或者在代码的其他位置创建 ConfigurationBuilder/TwitterFactory/Twitter 对象。

下面我实现了一个创建状态的类,并且可以返回包含附加信息的状态对象,例如 createdAt() 和 ID 等。

package main;

import twitter4j.Status;
import twitter4j.TwitterException;

public class StatusUpdater extends Base{   

    public StatusUpdater(){}

    public Status updateStatus(String statusToUpdate) throws TwitterException{
        Status status = twitter.updateStatus(statusToUpdate);
        System.out.println("statusToUpdate: " + status + ".");
        return status;
    }
}

然后您可以使用以下语句来创建状态。这可以通过 mbean/ejb/servlet 等来完成。

     try {
            StatusUpdater statusUpdater = new StatusUpdater();
            String statusTextToSet = "test status";
            Status updatedStatus = statusUpdater.updateStatus(statusTextToSet);
            System.out.println("Created at: " + updatedStatus.getCreatedAt());
        } catch (TwitterException tex) {
            System.out.println(tex.getErrorMessage());
        }

有关配置过程的更多信息在这里

于 2012-05-16T12:59:36.900 回答
0

你的代码看起来不错。您是否确认您确实为您的应用使用了正确的消费者密钥和秘密?尝试twitter.verifyCredentials()查看您是否收到相同的错误或更具体的错误。

如果您通过代理更正到网络,则需要包括您的代理设置以允许连接成功,可以在此处找到详细信息http://twitter4j.org/en/configuration.html#HTTP%20proxy% 20服务器

顺便说一句,您可能希望从您的帖子中删除您的访问令牌和密码,如果它们仍然有效,那么有人可以使用他们登录您的帐户。

于 2012-04-09T05:11:59.117 回答