Second Call To Twitter API Fails

Sep 7, 2014 at 5:28 PM
Edited Sep 7, 2014 at 5:29 PM
Hey Joe,

I am using the Version 2.1 LinqToTwitterLibrary. The first call to User timeline works fine. I receive the tweets. But when I make the second call it just does not return anything, not even error, it just shows busy. Please shade some light on it.

FYI: I have been using the library for last two years, but never had such a problem.
Coordinator
Sep 8, 2014 at 1:06 AM
Hi,

I can't reproduce the problem yet. Here's what I'm using (from the downloadable source code demos):
        private static void UserStatusQueryDemo(TwitterContext twitterCtx)
        {
            // last tweet processed on previous query set
            //ulong sinceID = 210024053698867204;
            ulong sinceID = 1;

            ulong maxID;
            const int Count = 10;
            var statusList = new List<Status>();

            var userStatusResponse =
                (from tweet in twitterCtx.Status
                 where tweet.Type == StatusType.User &&
                       tweet.ScreenName == "JoeMayo" &&
                       tweet.SinceID == sinceID &&
                       tweet.Count == Count
                 select tweet)
                .ToList();

            statusList.AddRange(userStatusResponse);

            // first tweet processed on current query
            maxID = userStatusResponse.Min(
                status => ulong.Parse(status.StatusID)) - 1;

            do
            {
                // now add sinceID and maxID
                userStatusResponse =
                    (from tweet in twitterCtx.Status
                     where tweet.Type == StatusType.User &&
                           tweet.ScreenName == "JoeMayo" &&
                           tweet.Count == Count &&
                           tweet.SinceID == sinceID &&
                           tweet.MaxID == maxID
                     select tweet)
                    .ToList();

                if (userStatusResponse.Count > 0)
                {
                    // first tweet processed on current query
                    maxID = userStatusResponse.Min(
                        status => ulong.Parse(status.StatusID)) - 1;

                    statusList.AddRange(userStatusResponse); 
                }
            }
            while (userStatusResponse.Count != 0 && statusList.Count < 30);

            for (int i = 0; i < statusList.Count; i++)
            {
                Status status = statusList[i];

                Console.WriteLine("{0, 4}. [{1}] User: {2}\nStatus: {3}",
                    i + 1, status.StatusID, status.User.Name, status.Text);
            }
        }
Do you have a sample that reproduces the problem?

@JoeMayo
Sep 8, 2014 at 6:30 AM
Here is my code:

var auth = new WebAuthorizer
            {
                Credentials = new InMemoryCredentials
                {
                    ConsumerKey = "somekey",
                    ConsumerSecret = "somesecret",
                    OAuthToken = "someoauthtoken",
                    AccessToken = "someaccesstoken"
                }
            };
 
            var pubTweetCtx = new TwitterContext(auth);
            DataTable dtScreenNameTweets = new DataTable("ScreenName");
            dtScreenNameTweets.Columns.Add("CreatedAt", typeof(DateTime));
            dtScreenNameTweets.Columns.Add("UserName", typeof(string));
            dtScreenNameTweets.Columns.Add("Text", typeof(string));
            dtScreenNameTweets.Columns.Add("Tweet_ID", typeof(ulong));
            dtScreenNameTweets.Columns.Add("Screen_Name", typeof(string));
            dtScreenNameTweets.Columns.Add("Location", typeof(string));
            dtScreenNameTweets.Columns.Add("Followers", typeof(string));
            dtScreenNameTweets.Columns.Add("Following", typeof(string));
            dtScreenNameTweets.Columns.Add("Lat", typeof(double));
            dtScreenNameTweets.Columns.Add("Lon", typeof(double));
 
            var queryResponse = (from tweet in pubTweetCtx.Status
                                 where tweet.Type == StatusType.User &&
                                       tweet.ScreenName == txtScreenName.Text &&
                                       tweet.Count == 200
                                 select tweet).ToList();
 
// store the response in datatable foreach (var q in queryResponse) { DataRow dr = dtScreenNameTweets.NewRow(); dr["CreatedAt"] = q.CreatedAt; dr["UserName"] = q.User.Name; dr["Text"] = q.Text.Replace("'", " "); dr["Tweet_ID"] = q.StatusID; if (q.Retweeted == true) { string Retweet = q.RetweetedStatus.Text; } dr["Lat"] = q.Coordinates.Longitude; dr["Lon"] = q.Coordinates.Latitude; dr["Location"] = q.User.Location; dr["Followers"] = q.User.FollowersCount; dr["Following"] = q.User.FriendsCount; dtScreenNameTweets.Rows.Add(dr); } // convert datatable to xml string xmlString = string.Empty; using (TextWriter writer = new StringWriter()) { dtScreenNameTweets.WriteXml(writer); xmlString = writer.ToString(); } txtResults.Text = string.Empty; txtResults.Text = xmlString;



Sep 8, 2014 at 8:42 AM
When I use the latest library verion 3 it works fine....but with version 2 it fails after first call. And I cannot change the library version with project at work from 2 to 3 coz we are using framework 4.0 for the project.


Coordinator
Sep 8, 2014 at 3:44 PM
A couple things:
  1. You're using InMemoryCredentials on a Web application. When the Web application recycles, you loose in-memory state. Try SessionStateCredentials or build your own ICredentials type to ensure your credential state is available. Also, set your web.config sessionState to either StateServer or SqlServer or you'll see the same problem as with InMemoryCredentials. This is a a typical problem where the application works well for a long time and suddenly starts having problems.
  2. Wrap your queries in try/catch. Sometimes Twitter returns non-200 response codes, which surface as exceptions. Log the exceptions, including any inner exceptions and data returned by Twitter for debugging. LINQ to Twitter exceptions are normally TwitterQueryException types, so make sure you log the HttpError and ErrorCode properties as well as the ex.ToString() to get the stack trace.
@JoeMayo
Sep 9, 2014 at 6:28 AM
I also have been getting the same error as mentioned by webminer since august 29. I am using 2.1 version of the nuget package
Sep 9, 2014 at 6:57 AM
In my actual application I am using session state credentials. Still facing the same problem.

Sep 9, 2014 at 7:35 AM
Edited Sep 9, 2014 at 7:49 AM
Even I am facing this problem from 29 August. I am not even getting an error response. It just shows the busy timer for as long as I don't force stop the process.
Sep 9, 2014 at 8:38 AM
Edited Sep 9, 2014 at 8:40 AM
Here is the code from actual application:
 IOAuthCredentials Creds = new SessionStateCredentials();
                Creds.ConsumerKey = "somkey";
                Creds.ConsumerSecret = "somesecret";
                Creds.AccessToken = "someaccesstoken";
                Creds.OAuthToken = "sometoken";
                //Auth Object With Credentials
                auth = new WebAuthorizer
                {
                    Credentials = Creds 
                };                

                //Instantiate Twitter Context
                var twitterCtx = new TwitterContext(auth);                
               
                //Fetch Tweets
               
                        var queryResponse = from tweet in twitterCtx.Status
                                            where tweet.Type == StatusType.User && tweet.ScreenName == Screen_Name && tweet.Count == 200 
                                            select tweet;

                        foreach (var q in queryResponse)
                        {
                            DataRow dr = DT_Tweets.NewRow();
                            dr["CREATED_AT"] = q.CreatedAt;
                            dr["UserName"] = q.User.Name;
                            dr["Screen_Name"] = q.User.Identifier.ScreenName;
                            dr["TWITTER_USER_ID"] = q.User.Identifier.UserID;
                            dr["Description"] = q.User.Description;
                            dr["Picture_URL"] = q.User.ProfileImageUrl;
                            dr["USER_URL"] = q.User.Url;
                            dr["USER_LOCATION"] = q.User.Location;
                            dr["USER_TIMEZONE"] = q.User.TimeZone;
                            dr["Location"] = q.User.Location;
                            dr["Followers"] = q.User.FollowersCount;
                            dr["Following"] = q.User.FriendsCount;
                            dr["Tweets_Count"] = q.User.StatusesCount;
                            dr["USER_FAVORITES_COUNT"] = q.User.FavoritesCount;
                            dr["USER_LISTED_COUNT"] = q.User.ListedCount;
                            dr["USER_VERIFIED"] = q.User.Verified;
                            dr["USER_PROTECTED"]= q.User.Protected;
                            dr["USER_CREATED_AT"]= q.User.CreatedAt;
                            if (q.RetweetCount > 0 && string.IsNullOrEmpty(q.RetweetedStatus.Text) == false)
                            {
                                dr["TWEET_TEXT"] = q.RetweetedStatus.Text;
                                dr["OriginalTweetBy"] = q.RetweetedStatus.User.Identifier.ScreenName;
                                dr["OriginalTweetStatusID"] = q.RetweetedStatus.StatusID;
                            }
                            else
                            {
                                dr["TWEET_TEXT"] = q.Text;
                                dr["OriginalTweetBy"] = string.Empty;
                                dr["OriginalTweetStatusID"] = string.Empty;
                            }
                            dr["Tweet_ID"] = q.StatusID;
                            dr["Lat"] = q.Coordinates.Latitude;
                            dr["Lon"] = q.Coordinates.Longitude;
                            dr["Retweet_Count"] = q.RetweetCount;
                            dr["Retweeted"] = q.Retweeted;
                            dr["Favourite_Count"] = q.FavoriteCount;
                            dr["Tweet_Source"] = q.Source;
                            dr["User_Mentions_Count"] = q.Entities.UserMentionEntities.Count;
                            dr["Hashtag_Count"] = q.Entities.HashTagEntities.Count;
                            dr["Media_Count"] = q.Entities.MediaEntities.Count;
                            dr["Url_Count"] = q.Entities.UrlEntities.Count;
                            DT_Tweets.Rows.Add(dr);
                        }
Sep 9, 2014 at 8:56 AM
Recent observation has been that, the second call sometimes takes as long as 15-20 mins if it returns any data, so its not the issue Session State Credentials. Why does the second call take so long?
Sep 9, 2014 at 9:57 AM
I tested with 3.0.4 version in framework 4.5. With the same code, it returns data. I need this fix in 2.1.1 as well. Can anyone help ?
Sep 9, 2014 at 10:10 AM
Edited Sep 9, 2014 at 10:17 AM
Likewise, it works fine wth 3.x version, but not with 2.x....Need a fix with 2.x, coz I cannot replace the 2.x library in my current project.
Coordinator
Sep 9, 2014 at 2:49 PM
Sep 9, 2014 at 5:42 PM
Tried, but does not make any difference.


Coordinator
Sep 9, 2014 at 7:35 PM
I just added a UserTimeline.aspx demo to the LinqToTwitterWebFormsDemo project. Each time I click the Submit Query button it works for me. Maybe you can download that and see if it works.
  1. If my demo works for you, we can find the delta between what your problem is and this demo.
  2. If the demo shows the same problem, my next advice would be to open Fiddler to see what message Twitter API is sending back to you.
@JoeMayo
Sep 18, 2014 at 9:06 AM
Hey Joe,

We have deployed the LinqToTwitter 3.x library in framework 4.5. But we are stuck with async web mthod. Any samples on async web methods using the 3.x library would be a great help. Thank You.
Coordinator
Sep 18, 2014 at 2:18 PM
There's a Linq2TwitterDemos_WebForms project in the source code.

@JoeMayo
Jan 2, 2015 at 7:06 PM
It seens to be a little buggy if is only one result!