嗨,我正在尝试使用以下代码更新Twitter状态,但我不断收到以下异常,
TwitterException {exceptionCode = [bfb606ed-4ef9708b bfb606ed-4ef97061],statusCode = -1,message = null,code = -1,retryAfter = -1,rateLimitStatus = null,version = 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);
}
最佳答案
我遇到了同样的异常,它来自调用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();
请注意,我还将回调URL传递给getOAuthRequestToken方法:
RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL);