Quantcast
Channel: Tweetinvi a friendly Twitter C# library
Viewing all articles
Browse latest Browse all 4126

New Post: Operation is not valid due to the current state of the object

$
0
0
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.

Viewing all articles
Browse latest Browse all 4126

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>