2014-01-29 13 views
0

我需要一些幫助與Twitter API錯誤多少請求Twitter的API我的程序佔用

429:Returned in API v1.1 when a request cannot be served due to the application's rate limit having been exhausted for the resource. 

我使用twitter4j,我試圖來存儲我的朋友的時間表。 問題是,在我的代碼掛起(嘰嘰喳喳禁止我)不認爲我已經達到了速率限制。爲了正確使用wait()或sleep()方法,瞭解這一點很重要。

我的代碼如下:當我收到第二用戶的微博

ConfigurationBuilder cb = new ConfigurationBuilder(); 
     cb.setDebugEnabled(true) 
      .setOAuthConsumerKey("appKey") 
      .setOAuthConsumerSecret("appSecret") 
      .setOAuthAccessToken("userKey") 
      .setOAuthAccessTokenSecret("userSecret") 
      .setUseSSL(true); 


     TwitterFactory tf = new TwitterFactory(cb.build());    
     Twitter twitter; 
     twitter = tf.getInstance();   

     Paging paging; 
     DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd"); 
     Date date = new Date(); 

     Long tweetsinceId=0L; 
      try{       
       long cursor = -1;    
       IDs ids = twitter.getFriendsIDs(cursor); 
       long[] id = ids.getIDs(); 
       ResponseList<User> users = twitter.lookupUsers(id); 
       int numoftweets=1; 
       int count=0; 

       /**************START: Store the friends of the user in the list usernames - The search will be performed only for the accounts included in the list**************************************************************************************/ 
       for (User user : users) { 

        numoftweets = user.getStatusesCount(); 
        userId = user.getId(); 


        System.out.println("INSERT INTO userinfo " + 
         "VALUES ("+userId+",'"+user.getName()+"','"+ user.getScreenName()+"','"+dateformat.format(user.getCreatedAt())+"',"+ user.getFavouritesCount()+","+ user.getFollowersCount() 
            +","+user.getFriendsCount()+",'"+ user.getDescription()+"','"+user.getLocation()+"',null,"+ numoftweets+",'"+ user.getURL() 
            +"','"+ user.getMiniProfileImageURL()+"',null, null, '"+user.getLang()+"',"+ user.getAccessLevel()+");"); 

        count++; 

        int numberofpages = Math.round(numoftweets/100); 
        for (int j=1;j<=numberofpages;j++){ 
         if (tweetsinceId==0L){ 
          paging = new Paging(j, 100); 
         } 
         else{ 
          paging = new Paging(j, 100).maxId(tweetsinceId); 
          } 
          if(twitter.getUserTimeline(userId,paging)!=null){ 
           List<Status> statusess = twitter.getUserTimeline(userId,paging);         
           for (Status status3 : statusess) 
           { 
             //if (count==600){ 
             // Thread.sleep(1000); 
             // } 

             String tweet = status3.getText(); 
             Boolean isfavourite = status3.isFavorited();                     
             Boolean isretweet = status3.isRetweet(); 
             Boolean isretweetedbyme=status3.isRetweetedByMe(); 


             System.out.println("INSERT INTO tweetinfo " + 
             "VALUES ("+userId+","+status3.getId()+",'"+tweet+"','"+ dateformat.format(status3.getCreatedAt()) 
               +"',null,"+ status3.getCurrentUserRetweetId()+",null,null,null,null" 
               +",null,"+status3.getRetweetCount()+",null,null,"+ isfavourite+","+isretweet+","+isretweetedbyme+",null,'"+ status3.getSource()+"');"); 

             count++; 
             System.out.println(count); 

             tweetsinceId = status3.getId(); 
           }//end for 
          } //end if       
         }//end for 
       }    
      } 
      catch (Exception e) { 
       e.getStackTrace(); 
      } 

     //}catch(SQLException se){ 
     }catch(Exception e){ 
      //Handle errors for Class.forName 
      e.printStackTrace(); 
     } 

因此,對於上述程序Twitter的API拒絕訪問。 在這一點上,我有1個twitter.getFriendsIDs(遊標),1個twitter.lookupUsers(id)和2個twitter.getUserTimeline調用twitter API

我的計數錯誤的調用數量?

爲什麼Twitter API已經達到我的應用程序的速率限制?

我已經閱讀https://dev.twitter.com/docs/rate-limiting/1.1/limits但也許我有一些誤解。

任何形式的幫助表示讚賞。

回答

0

在Twitter 1.1你想看看rate_limit_status

你可以提供給

GET https://api.twitter.com/1.1/application/rate_limit_status.json?resources=help,users,search,statuses

電話您可以列出並限制了資源的任何resoruces響應GET呼叫