2013-10-01 53 views
1

嗨,我嘗試使用下面的代碼來更新Twitter的狀態,但我不斷收到以下異常, TwitterException {exceptionCode = [bfb606ed-4ef9708b bfb606ed-4ef97061],的StatusCode = - 1,message = null,code = -1,retryAfter = -1,rateLimitStatus = null,version = 3.0.3}連接超時越來越OAuthRequestToken當Twitter的

我已經把正確的消費者「鑰匙」和消費者「祕密」,任何幫助表示讚賞!

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); 
} 

回答

0

在我與這個問題的個人經驗,這是這是造成這個問題,並與編碼沒有問題防火牆 - 我並沒有在解決防火牆問題的工作。 希望這可以幫助一些人!

0

您需要增加超時時間。

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

1

我得到了這個例外,它來自電話RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL); 原來是一個代理相關的問題。

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); 
通過設置ConfigurationBuilder代理主機和端口固定它