Operation is not valid due to the current state of the object

Aug 28, 2014 at 5:32 PM
I made a Log In + Tweet sample on http://grwpo.azurewebsites.net/simplelogin.aspx, but for some unknown reason, though it rarely happens, it throws the following error:

Operation is not valid due to the current state of the object

Server Error in '/' Application.

Operation is not valid due to the current state of the object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Tweetinvi.Logic.Exceptions.TwitterException: Operation is not valid due to the current state of the object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[TwitterException: Operation is not valid due to the current state of the object.]
Tweetinvi.WebLogic.WebRequestExecutor.ExecuteWebRequest(HttpWebRequest httpWebRequest) +487
Tweetinvi.WebLogic.TwitterRequester.ExecuteQueryWithTemporaryCredentials(String url, HttpMethod httpMethod, ITemporaryCredentials temporaryCredentials, IEnumerable`1 parameters) +101
Tweetinvi.Credentials.CredentialsCreator.GetCredentialsFromVerifierCode(String verifierCode, ITemporaryCredentials temporaryCredentials) +224
Tweetinvi.CredentialsCreator.GetCredentialsFromVerifierCode(String verifierCode, ITemporaryCredentials temporaryCredentials) +54
TweetInviSample.SimpleLogin.btnCheck_Click(Object sender, EventArgs e) in c:\Users\Yushell\Documents\Visual Studio 2013\Projects\TweetInviSample\TweetInviSample\SimpleLogin.aspx.cs:41
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9628114
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

The ASPX file has the following:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SimpleLogin.aspx.cs" Inherits="TweetInviSample.SimpleLogin" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <meta name="description" content="The description of my page" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <style>
        #step2 {
            display: none;
        }
        #step3 {
            display: none;
        }
        #step4 {
            display: none;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div id="step1" runat="server">
            <a id="participa" href="#" runat="server">Participa</a>
        </div>

        <div id="step2" runat="server">
            <p>
                <asp:TextBox ID="txtCaptcha" runat="server"></asp:TextBox>
            </p>
            <p>
                <asp:Button ID="btnCheck" runat="server" Text="Verifica tu código" OnClick="btnCheck_Click" />
            </p>
        </div>

        <div id="checkStatus" style="display: none;" runat="server">0</div>

        <div id="step3" runat="server">
            <p>
                <asp:TextBox ID="txtTweet" Text="Este es un texto predeterminado #vwsound" TextMode="MultiLine" runat="server"></asp:TextBox>
            </p>
            <p>
                <asp:Button ID="btnTweet" runat="server" Text="Tweetea" OnClick="btnTweet_Click" />
            </p>
        </div>

        <div id="step4">
            Gracias
        </div>
    </form>
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.js"></script>
    <script>
        $("#participa").on("click", function () {
            $("#step1").hide();
            $("#step2").show();
        });
        if ($("#checkStatus").text() == 1) {
            $("#step1").hide();
            $("#step2").hide();
            $("#step3").show();
        }
        if ($("#checkStatus").text() == 2) {
            $("#step1").hide();
            $("#step2").hide();
            $("#step3").hide();
            $("#step4").show();
        }
        $("#btnTweet").on("click", function () {
            $("#step3").hide();
            $("#step4").show();
        });
    </script>
</body>
</html>
The ASPX.CS file has the following
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Tweetinvi;
using Tweetinvi.Core.Interfaces.oAuth;

namespace TweetInviSample
{
    public partial class SimpleLogin : System.Web.UI.Page
    {
        public static Tweetinvi.Core.Interfaces.Credentials.ITemporaryCredentials applicationCredentials;
        public static string ConsumerKey = string.Empty;
        public static string ConsumerSecret = string.Empty;
        public static string AccessToken = string.Empty;
        public static string AccessTokenSecret = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                applicationCredentials = CredentialsCreator.GenerateApplicationCredentials("eFxlRGcJ9hy2mlIcQ9xSjDEqy", "6wJhQnSNBWkhLwMe3CD9iI0auhzxbM336JKnWqBfPtPFp6Do6k");
                var url = CredentialsCreator.GetAuthorizationURL(applicationCredentials);
                participa.Attributes.Add("onclick", "popupWindow()");
                participa.Target = "_blank";
                participa.HRef = url;
            }
        }

        protected void btnTwitter_Click(object sender, EventArgs e)
        {

        }

        protected void btnCheck_Click(object sender, EventArgs e)
        {
            var captcha = txtCaptcha.Text;

            var newCredentials = CredentialsCreator.GetCredentialsFromVerifierCode(captcha, applicationCredentials);
            ConsumerKey = newCredentials.ConsumerKey;
            ConsumerSecret = newCredentials.ConsumerSecret;
            AccessToken = newCredentials.AccessToken;
            AccessTokenSecret = newCredentials.AccessTokenSecret;
            checkStatus.InnerText = "1";
        }

        protected void btnTweet_Click(object sender, EventArgs e)
        {
            var credentials = TwitterCredentials.CreateCredentials(AccessToken, AccessTokenSecret, ConsumerKey, ConsumerSecret);
            TwitterCredentials.ExecuteOperationWithCredentials(credentials, () =>
            {
                var user = Tweetinvi.User.GetLoggedUser(credentials);
                string twitterHandle = user.ScreenName;

                var tweet = Tweet.CreateTweet(txtTweet.Text);
                Tweet.PublishTweet(tweet);
                string userId = tweet.Creator.IdStr;
                string userHandle = tweet.Creator.ScreenName;
                string tweetId = tweet.Id.ToString();
                string n = string.Empty;
            });
            checkStatus.InnerText = "2";
        }
    }
}
The steps are:
  1. Click on Participa (Participate)
  2. Authorize and get code
  3. Insert code in site, click Verifica tu codigo (Verify your code)
  4. Tweet
As I said ealier. This error rarely happens and I've been unable to catch the cause. I appreciate any help.
Aug 28, 2014 at 6:49 PM
Hi there,

Would you please try to publish your application on a machine that you "own". I know that"Operation is not valid due to the current state of the object" can be received if the machine system time is incorrect.

If the error occurs try to get the Time of the local machine and get the Time from the internet and compare these to check whether there is a difference. Usually a difference of seconds can make the operation crash because twitter are preventing them to occur.

Linvi
Aug 28, 2014 at 7:22 PM
Is there no way to avoid this problem? I normally publish to Azure Websites, and I can't control the time on them. Though I imagine they should have the right time. And I rarely get this error, but when I get it, I don't know why.
Aug 29, 2014 at 6:03 PM
Hi,

Could you please try to get the latest version of Tweetinvi (Source Code), compile it and use the generated DLL.
Then log the errors from the ExceptionHandler and reply to this thread. I will have more resources to help you this way.

Linvi
Oct 17, 2014 at 11:43 AM
Did this ever get resolved? I'm getting a similar issue
Oct 21, 2014 at 12:37 AM
Hi there,

No, it has never been resolved because I did not get the information I needed to investigate the problem any further.
I you could please send me the information that are stored in your ExceptionHandler I will be happy to help you.

Linvi
Oct 21, 2014 at 7:15 AM
Hi

I solved it myself thanks, it was a configuration problem with my application in twitter - I had left the callback field empty.