有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我的程序使用多少对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,我正在尝试存储我朋友的时间表。 问题是,在我的代码挂起的时候(twitter禁止我),我不相信我已经达到了利率限制。为了正确地放置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();
         }

因此,对于上面的程序,当我收到第二个用户的tweet时,twitterapi拒绝访问。 在这一点上,我有1个twitter。GetFriendsID(光标),1个twitter。查找用户(id)和2个twitter。对twitter API的getUserTimeline调用

我算错电话号码了吗

为什么Twitter API已经达到我的应用程序的速率限制

我已经读过https://dev.twitter.com/docs/rate-limiting/1.1/limits,但也许我有什么误解

感谢您的任何帮助


共 (1) 个答案

  1. # 1 楼答案

    在Twitter 1.1中,你想看看rate_limit_status

    你可以打电话给

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

    您可以列出并限制对资源GET调用中任何resoruces的响应