401 error only for debug mode

Apr 5, 2011 at 2:04 AM

Hi!

I'm testing link to twitter, and every time i try to use the authentification i got an exception on the auth.CompleteAuthorization(Request.Url) line. So i launch my app in debug mode, authorize my application for my twitter account, and then i got every time a 401 exception. Strange thing, this only appear when the project is on debug mode. if i just launch my project and immediatly stop the debug, it works fine...

So i'm looking for a solution to this problem... i'm using linqtoTwitter in a mvc project. this is my HomeController.cs :

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LinqToTwitter;
using System.Configuration;
using TwitterProjetct.Models;

namespace TwitterProjetct.Controllers
{
    public class HomeController : Controller
    {

        twitterProjectEntities entity = new twitterProjectEntities();

        IOAuthCredentials credentials = new SessionStateCredentials();

        private MvcAuthorizer auth;
        private TwitterContext twitterCtx;

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult LogOut()
        {
            /*if (twitterCtx != null)
            {
                twitterCtx.Dispose();
                twitterCtx = null;
            }*/
            return View("Index");
        }


        public ActionResult LogMeIn()
        {
            if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
            {
                credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
                credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
            }

            auth = new MvcAuthorizer
            {
                Credentials = credentials
            };

            auth.CompleteAuthorization(Request.Url); <-------------- Here is the 401 exception

            if (!auth.IsAuthorized)
            {
                return auth.BeginAuthorization();
            }

            twitterCtx = new TwitterContext(auth);

            var friendTweets =
                (from tweet in twitterCtx.Status
                 where tweet.Type == StatusType.Friends
                 select new TweetViewModel
                 {
                     ImageUrl = tweet.User.ProfileImageUrl,
                     ScreenName = tweet.User.Identifier.ScreenName,
                     Tweet = tweet.Text
                 })
                .ToList();
            foreach(TweetViewModel tweet in friendTweets) {
                TwitterTweets temp_tweet = new TwitterTweets();
                temp_tweet.Text = tweet.Tweet;
                entity.AddToTwitterTweets(temp_tweet);
            }
            entity.SaveChanges();
            return View(friendTweets);
        }

        public ActionResult Details()
        {
            return View(entity.TwitterTweets.ToList());
        }
    }
}

i've looked in a lot of discussions but didn't find anything to fix this...

 

Thanks in advance for your help.

 

PS: i'm really sorry for my bad english, it is not my native language... thanks for your help again

Coordinator
Apr 5, 2011 at 2:23 AM

Hi,

I've worked around it by putting a delay before the CompleteAuthorization, like this:

    Thread.Sleep(15000);
    auth.CompleteAuthorization(Request.Url); 

Joe

 

Apr 6, 2011 at 5:09 PM
Edited Apr 6, 2011 at 7:50 PM

I changed 

 

 public abstract class OAuthAuthorizer
    {
        public OAuthAuthorizer()
        {
            OAuthRequestTokenUrl = "https://api.twitter.com/oauth/request_token";
            OAuthAuthorizeUrl = "https://api.twitter.com/oauth/authorize";
            OAuthAccessTokenUrl = "https://api.twitter.com/oauth/access_token";
to 
 public abstract class OAuthAuthorizer
    {
        public OAuthAuthorizer()
        {
            OAuthRequestTokenUrl = "http://twitter.com/oauth/request_token";
            OAuthAuthorizeUrl = "http://twitter.com/oauth/authorize";
            OAuthAccessTokenUrl = "http://twitter.com/oauth/access_token";

and it start work normally.