API throws 401 after an hour or so usage


#1

Everything works when new oauth_token and oauth_verifier gets generated. I am using Twitterizer2 DLL I am storing user specific oauth_token and oauth_verifier using below code at page load.

 if (Request["oauth_token"] == null)
        {
               OAuthTokenResponse reqToken = OAuthUtility.GetRequestToken(
                        oauth_consumer_key,
                        oauth_consumer_secret,
                        Request.Url.AbsoluteUri);
                if (Request["tsecure"] == "true")
                {
                       Response.Redirect(string.Format("https://twitter.com/oauth/authorize?oauth_token={0}", reqToken.Token));
                }
                else
                {
                       Response.Redirect(string.Format("http://twitter.com/oauth/authorize?oauth_token={0}", reqToken.Token));
                   }
        }

        string oauthToken = Request["oauth_token"].ToString();
        string oauthVerified = Request["oauth_verifier"].ToString();

Everything works fine on below code for posting a message. I am storing oauthToken and oathVerifer values per user.

var tokens = OAuthUtility.GetAccessToken(
oauth_consumer_key,
oauth_consumer_secret,
requestToken,
pin);

        OAuthTokens accesstoken = new OAuthTokens()
        {
            AccessToken = tokens.Token,
            AccessTokenSecret = tokens.TokenSecret,
            ConsumerKey = oauth_consumer_key,
            ConsumerSecret = oauth_consumer_secret
        };


               TwitterResponse<TwitterStatus> response = TwitterStatus.Update(accesstoken, strTweet, new StatusUpdateOptions() { UseSSL = true, APIBaseAddress = "http://api.twitter.com/1.1/" });

Everything works when new oauth_token and oauth_verifier gets generated. I am using Twitterizer2 DLL I am storing user specific oauth_token and oauth_verifier using below code at page load

    if (Request["oauth_token"] == null)
    {
           OAuthTokenResponse reqToken = OAuthUtility.GetRequestToken(
                    oauth_consumer_key,
                    oauth_consumer_secret,
                    Request.Url.AbsoluteUri);
            if (Request["tsecure"] == "true")
            {
                   Response.Redirect(string.Format("https://twitter.com/oauth/authorize?oauth_token={0}", reqToken.Token));
            }
            else
            {
                   Response.Redirect(string.Format("http://twitter.com/oauth/authorize?oauth_token={0}", reqToken.Token));
               }
    }

    string oauthToken = Request["oauth_token"].ToString();
    string oauthVerified = Request["oauth_verifier"].ToString();

Everything works fine on below code for posting a message. I am storing oauthToken and oathVerifer values per user once they approved my App to post tweets on their behalf.

        var tokens = OAuthUtility.GetAccessToken(
            oauth_consumer_key,
            oauth_consumer_secret,
            requestToken,
            pin);

        OAuthTokens accesstoken = new OAuthTokens()
        {
            AccessToken = tokens.Token,
            AccessTokenSecret = tokens.TokenSecret,
            ConsumerKey = oauth_consumer_key,
            ConsumerSecret = oauth_consumer_secret
        };


               TwitterResponse<TwitterStatus> response = TwitterStatus.Update(accesstoken, strTweet, new StatusUpdateOptions() { UseSSL = true, APIBaseAddress = "http://api.twitter.com/1.1/" });

After about an hour same code at below line throws error

var tokens = OAuthUtility.GetAccessToken(....

Error:

An exception of type 'Twitterizer.TwitterizerException' occurred in >Twitterizer2.dll but was not handled in user code

{"The remote server returned an error: (401) Unauthorized."}

All setting are correct for Twitter App. Its marked as web application with call back URL http://127.0.0.1

Are tokens getting expired/reset somehow ? Regenerating new tokens works fine for that user. Any pointers to solve this issue.


#2

You should generate accesstoken once and store that per user. The request_token and oauth_verifier are one time use.


#3

Ok, let me try that. Thanks.