1

您好我正在尝试使用以下代码更新 Twitter 状态,但我不断收到以下异常,TwitterException{exceptionCode=[bfb606ed-4ef9708b bfb606ed-4ef97061], statusCode=-1, message=null, code=-1, retryAfter= -1,rateLimitStatus=null,版本=3.0.3}

我已经输入了正确的消费者“密钥”和消费者“秘密”,感谢您的帮助!

    public void twitterUpdates(String update) throws TwitterException, IOException  {
    // The factory instance is re-useable and thread safe.
    Twitter twitter = TwitterFactory.getSingleton();
    twitter.setOAuthConsumer("key", "secret");
    RequestToken requestToken = twitter.getOAuthRequestToken();
    AccessToken accessToken = loadAccessToken(twitter.verifyCredentials().getId());
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    while (accessToken == null) {
        System.out
                .println("Open the following URL and grant access to your account:");
        System.out.println(requestToken.getAuthorizationURL());
        System.out
                .print("Enter the PIN(if available) or just hit enter.[PIN]:");
        String pin = br.readLine();
        try {
            if (pin.length() > 0) {
                accessToken = twitter
                        .getOAuthAccessToken(requestToken, pin);
            } else {
                accessToken = twitter.getOAuthAccessToken();
            }
        } catch (TwitterException te) {
            if (401 == te.getStatusCode()) {
                System.out.println("Unable to get the access token.");
            } else {
                te.printStackTrace();
            }
        }
    }
    // persist to the accessToken for future reference.
    storeAccessToken(twitter.verifyCredentials().getId(), accessToken);
    Status status = twitter.updateStatus(update);
    System.out.println("Successfully updated the status to ["
            + status.getText() + "].");


     System.exit(0);
}
4

3 回答 3

1

我得到了同样的异常,它来自调用RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL); 结果是与代理相关的问题。通过在 ConfigurationBuilder 中设置代理主机和端口来修复它:

ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true);
cb.setOAuthConsumerKey(key);
cb.setOAuthConsumerSecret(secret);
cb.setHttpProxyHost("proxy");
cb.setHttpProxyPort(8080);
Twitter twitter = new TwitterFactory(cb.build()).getInstance();

请注意,我还将 callbackURL 传递给 getOAuthRequestToken 方法:

RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL);
于 2016-04-07T10:53:53.113 回答
0

根据我对这个问题的个人经验,是防火墙导致了这个问题,而编码没有问题——我没有解决防火墙问题。希望这可以帮助一些人!

于 2014-01-06T09:38:45.013 回答
0

你需要增加超时。

$cb->setConnectionTimeout(20000); $cb->setTimeout(50000);

于 2015-03-18T16:07:38.003 回答