Why twitter token gets expired?

112 Views Asked by At

I have a twitter bot that uses LinqtoTwitter it works perfectly fine but when i start it after almost 20 reply(comment) on other users tweets the token gets expired without any warning. I changed the intervals from every 5 min to every 10 minutes still the tokens get disappeared and i need to generate a new token!!

This app sends product links to users based on their tweets it is web based and runs background thread so once it is running it is working in the background even if closing the web page How can I fix this issue?! And now the app got suspended :( not the user tho! Here is the auth code:

    private static string consumer_Key = ConfigurationManager.AppSettings["consumerkey"];
    private static string consumer_Key_Secret = ConfigurationManager.AppSettings["consumersecret"];
    private static string access_Token = ConfigurationManager.AppSettings["accessToken"];
    private static string access_Token_Secret = ConfigurationManager.AppSettings["accessTokenSecret"];

    static SingleUserAuthorizer auth = new SingleUserAuthorizer
    {
        CredentialStore = new SingleUserInMemoryCredentialStore
        {
            ConsumerKey = consumer_Key,
            ConsumerSecret = consumer_Key_Secret,
            AccessToken = access_Token,
            AccessTokenSecret = access_Token_Secret,
        }
    };
1

There are 1 best solutions below

2
Joe Mayo On

A common issue with Web apps is that the Web server (IIS) recycles occasionally and any credentials held in memory are lost. One of the things you can do is to use a SessionStateCredentialStore, like this:

static SingleUserAuthorizer auth = new SingleUserAuthorizer
{
    CredentialStore = new SessionStateCredentialStore
    {
        ConsumerKey = consumer_Key,
        ConsumerSecret = consumer_Key_Secret,
        OAuthToken = access_Token,
        OAuthTokenSecret = access_Token_Secret,
    }
};

To overcome the recycling problem, make sure your session state is set to StateService or SqlServer, rather than InProc.

Another option is to verify that you still have credentials loaded before performing an operation. The CredentialStore has a HasAllCredentials method you can use like this.

if (!auth.CredentialStore.HasAllCredentials())
{
    auth = new SingleUserAuthorizer
    {
        CredentialStore = new SingleUserInMemoryCredentialStore
        {
            ConsumerKey = consumer_Key,
            ConsumerSecret = consumer_Key_Secret,
            AccessToken = access_Token,
            AccessTokenSecret = access_Token_Secret,
        }
    };
}

If you're running into capacity problems where Twitter is sending you rate limit errors, consider the ApplicationOnlyAuthorizer, which gives you a larger rate limit. If you go with ApplicationOnlyAuthorizer, my comments on credentials above still apply.

You can use try/catch blocks to detect when you've exceeded rate limit or use the TwitterContext.RateLimitXxx properties to monitor each query to avoid exceeding the limits.

If Twitter is revoking your credentials, you'll need to resolve that with them. That's an issue between the Twitter API and your application, not LINQ to Twitter.