Error 401 after click twitter link "click here to continue"

Jul 26, 2011 at 11:28 AM

Hi,
I have a problem with the sample code LinqToTwitterWebFormsDemo.
On the SignIn.aspx page, I click on the twitter button to login and I am redirect to twitter site to authorize the application.
When I authorize the application, twitter start redirecting to my site and offers the link "click here to continue".
If a wait  the authentication are ok, but if I click on the link "click here to continue" I have an error 401.
In debug I saw the CompleteAuthorization is call many times we click on the link and just the first time the CompleteAuthorization is doing correctly.

Snippet of the sample code:
     private const string OAuthCredentialsKey = "OAuthCredentialsKey";
    private SignInAuthorizer auth;

    protected void Page_Load(object sender, EventArgs e)
    {
        IOAuthCredentials credentials = new InMemoryCredentials();
        string authString = Session[OAuthCredentialsKey] as string;

        if (authString == null)
        {
            credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
            credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];

            Session[OAuthCredentialsKey] = credentials.ToString();
        }
        else
        {
            credentials.Load(authString);
        }

        auth = new SignInAuthorizer
        {
            Credentials = new InMemoryCredentials
            {
                ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"],
                ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"]
            },
            PerformRedirect = authUrl => Response.Redirect(authUrl)
        };

        if (!Page.IsPostBack)
        {
            if (!string.IsNullOrWhiteSpace(credentials.ConsumerKey) &&
                !string.IsNullOrWhiteSpace(credentials.ConsumerSecret))
            {
                AuthMultiView.ActiveViewIndex = 1;

                if (auth.CompleteAuthorization(Request.Url))
                {
                    AuthMultiView.SetActiveView(SignedInView);
                    screenNameLabel.Text = auth.ScreenName;
                }
            }
        }
    }

    protected void signInButton_Click(object sender, ImageClickEventArgs e)
    {
        auth.BeginAuthorization(Request.Url, /*forceLogin:*/ true);
    }

 

 

Jul 26, 2011 at 2:09 PM

The complete Authorization checks to see if the credentials are loaded and sends the request to the webpage you specified.

 

Make sure your application is setup as a web application at dev.twitter.com, (I had that problem before)

Make sure your callback url you setup exists. I test locally but I make sure the youpagenameurl  name remains the same.

 

These answers might not be correct  but I thought i would try to help. I been working with this app for 4 months now. Once you get the OAuth dance working your golden.

Check out all my forum posts of Joe re telling me the same thing back that I asked in different ways. 

Jul 26, 2011 at 4:32 PM
Edited Jul 26, 2011 at 4:34 PM

Thanks for your response but now in dev.twitter.com i don't have the choice between client or browser application.

The problem is when I click on the link "click here to continue", because if I don't click I have no problem and I am authenticated.

The page when I wait:

http://imageshack.us/photo/my-images/9/39067357.png/

And when I click on "click here to continue"(Cliquer ici pour continuer) I have an error 401 on the page.

http://imageshack.us/photo/my-images/84/twitterwg.png/

http://imageshack.us/photo/my-images/585/captureloh.png/ ( sry for french error)

Coordinator
Jul 27, 2011 at 2:40 AM

Hi Elca,

My redirect is moving so fast that it's hard for me to get the window to click the link, but I see what you're talking about.  By watching through Fiddler, I did verify that the URL in the "click here to continue" link did match the Request.Url in the code-behind.

What I'm beginning to think is that you might have a timing error.  This could occur whenever your system clock is not in sync with Twitter.  The reason it looks like this is because when the page redirects back normally, all is good.  However, when you have to click the continue link, it breaks.  Given the fact that there is probably a delay associated with the need to click the continue link, I'm thinking that the delay is enough to exceed the timeout between the BeginAuthorization (request_token) and CompleteAuthorization (access_token) of the oauth authentication process.

To check if this is the cause:

  1. Either use the ResponseHeaders of the TwitterContext instance or use Fiddler.  You are looking for the server time of the response from Twitter.
  2. As soon as the response returns, look at the time on your local system.
  3. Compare the time on your local system to what was returned in the response header from Twitter.
  4. Adjust your computer time so that it's UTC time matches Twitter's UTC time.

Hope this helps,

@JoeMayo

Jul 27, 2011 at 8:54 AM

Thanks for you reply. The sample is fast too and i have difficulty to click on the link but in my project is not too fast and users can click on the link,

I think is not the UTC time because I found in debug the function "(auth.CompleteAuthorization(Request.Url)"

is does twice or more if we click more one time on the link. First time for the normal callback from twitter and next times for the number of click on the link.

The first time it's ok but the others does an error 401. and the response redirect in the first callback aren't do.

Have you an idea how do the "(auth.CompleteAuthorization(Request.Url)" only once ?