Karl Humphries
Karl Humphries

Reputation: 328

I can't get my account to authorise itself using linqtotwitter

I know this has been asked many times before, but I have used info from the linqtotwitter docs and examples along with other posts on here to get this far. I can get my new app to send a tweet, but only by letting it take me to twitters authorise page and me having to click the authorise button to continue.

The app itself is authorised fine, so I don't need my password each time, it's just the use of the app that it wants permission for.

I know the reason for this is because nowhere in my code is there my access token or access token secret. I have added them in but every time I get a 401 unauthorised (invalid or expired token).

My code is as follows, perhaps you can see where I'm going wrong?

    private IOAuthCredentials credentials = new SessionStateCredentials();

    private MvcAuthorizer auth;
    private TwitterContext twitterCtx;

    public ActionResult Index()
    {

        credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
        credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
        credentials.AccessToken = "MYaccessTOKENhere"; // Remove this line and line below and all works fine with manual authorisation every time its called //
        credentials.OAuthToken = "MYaccessTOKENsecretHERE";

        auth = new MvcAuthorizer
        {
            Credentials = credentials
        };

        auth.CompleteAuthorization(Request.Url);

        if (!auth.IsAuthorized)
        {
            Uri specialUri = new Uri(Request.Url.ToString());
            return auth.BeginAuthorization(specialUri);
        }

        twitterCtx = new TwitterContext(auth);
        twitterCtx.UpdateStatus("Test Tweet Here"); // This is the line it fails on //

        return View();
    }

Upvotes: 0

Views: 539

Answers (1)

Joe Mayo
Joe Mayo

Reputation: 7513

Here's the FAQ that has help on resolving 401 errors: http://linqtotwitter.codeplex.com/wikipage?title=LINQ%20to%20Twitter%20FAQ&referringTitle=Documentation

A couple items that might be helpful too:

  1. You can't tweet the same text twice - so either delete the previous tweet with the same text or change the text. All my test append DateTime.Now.ToString() to avoid this error.
  2. The SessionStateCredentials holds credentials in SessionState. The persistence mode default for Session state is InProc, meaning that your session variables will be null if the process recycles, which will happen unpredictably. You might want to make sure that you are using either StateServer or SQL Server modes.

Upvotes: 2

Related Questions