I am pulling tweets in allowed rate limit using user_timeline api successfully but after running for a while it starts throwing too many request error


#1

I tried to run the api to pull 200 users tweets in every 15 min windows rate limit (allowed rate limit is 300 request / 15 min). It worked fine for first time and then from next it starts throwing too many request error but pulls tweets for 1-2 users every time.

I don’t understand why this error comes, even when I send request in allowed limit and if it throws error then why it fetches tweets for some users randomly.

I tried to increase my recurring duration to 1 hr for fetching tweets but still I am getting the same issue.

Please suggest possible solution.

string GetTweet(string q, int lTopicID, string TwitterSinceId, string TwitterHandle, string TwitterMaxId)
{
// oauth application keys
try
{
var oauth_token = ConfigurationManager.AppSettings[“TwitterAccessToken”];
var oauth_token_secret = ConfigurationManager.AppSettings[“TwitterconAccessTokenSecret”];
var oauth_consumer_key = ConfigurationManager.AppSettings[“TwitterconsumerKey”];
var oauth_consumer_secret = ConfigurationManager.AppSettings[“TwitterconsumerSecret”];

            // oauth implementation details
            var oauth_version = "1.0";
            var oauth_signature_method = "HMAC-SHA1";

            // unique request details
            var oauth_nonce = Convert.ToBase64String(
                new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
            var timeSpan = DateTime.UtcNow
                - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();

            // message api details
            //var status = "Updating status via REST API if this works";
            var resource_url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
            var screen_name = TwitterHandle;
            // create oauth signature
            var baseFormat = "count={8}&oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
                            "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&screen_name={7}&since_id={9}&user_id={6}";


            string TweetCount = Uri.EscapeDataString(System.Configuration.ConfigurationManager.AppSettings["TweetCount"].ToString());


            var baseString = string.Format(baseFormat,
                                        oauth_consumer_key,
                                        oauth_nonce,
                                        oauth_signature_method,
                                        oauth_timestamp,
                                        oauth_token,
                                        oauth_version,
                                        Uri.EscapeDataString(screen_name),
                                         Uri.EscapeDataString(screen_name),
                                         Uri.EscapeDataString(TweetCount),
                                         Uri.EscapeDataString(TwitterMaxId)
                                        );

            baseString = string.Concat("GET&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString));

            var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
                                    "&", Uri.EscapeDataString(oauth_token_secret));

            string oauth_signature;
            using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
            {
                oauth_signature = Convert.ToBase64String(
                    hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
            }

            // create the request header
            var headerFormat = "OAuth oauth_nonce=\"{0}\", oauth_signature_method=\"{1}\", " +
                               "oauth_timestamp=\"{2}\", oauth_consumer_key=\"{3}\", " +
                               "oauth_token=\"{4}\", oauth_signature=\"{5}\", " +
                               "oauth_version=\"{6}\"";

            var authHeader = string.Format(headerFormat,
                                    Uri.EscapeDataString(oauth_nonce),
                                    Uri.EscapeDataString(oauth_signature_method),
                                    Uri.EscapeDataString(oauth_timestamp),
                                    Uri.EscapeDataString(oauth_consumer_key),
                                    Uri.EscapeDataString(oauth_token),
                                    Uri.EscapeDataString(oauth_signature),
                                    Uri.EscapeDataString(oauth_version)
                            );


            // make the request

            ServicePointManager.Expect100Continue = false;
            var postBody = string.Format("count={2}&user_id={0}&screen_name={1}&since_id={3}", Uri.EscapeDataString(screen_name), Uri.EscapeDataString(screen_name), Uri.EscapeDataString(System.Configuration.ConfigurationManager.AppSettings["TweetCount"].ToString()), Uri.EscapeDataString(TwitterMaxId));//"user_id=@sachin_rt&screen_name=sachin_rt";//            
            resource_url += "?" + postBody;//"user_id=sachin_rt&screen_name=sachin_rt";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
            request.Headers.Add("Authorization", authHeader);
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";


            WebResponse response = request.GetResponse();
            string strResponse = string.Empty;
            strResponse = new StreamReader(response.GetResponseStream()).ReadToEnd();

            List<TweetBO> lstTweet = new List<TweetBO>();


            JArray jsonDat = JArray.Parse(strResponse);
            for (int x = 0; x < jsonDat.Count(); x++)
            {
                 
                {
                    TweetBO tweet = new TweetBO();
                    tweet.TweetText = Convert.ToString(jsonDat[x]["text"].ToString());  //Convert.ToString(AntiXss.GetSafeHtmlFragment(obj["text"])); 
                    tweet.TweetText = tweet.TweetText.Replace("'", "&#39;").Replace("<", "&#60;").Replace(">", "&#62");
                    tweet.TweetID = Convert.ToString(jsonDat[x]["id"]).Trim();
                    tweet.TweetUserName = TwitterHandle; //Convert.ToString(jsonDat[x]["User"]["screen_name"]);

                    lstTweet.Add(tweet);
                     
                }
            }

            PostComment(lstTweet, lTopicID);
            
            return "success";
        }
        catch (Exception twit_error)
        {
            return "fail";
            
        }

    }