Unable to authorize

Dec 5, 2012 at 8:34 AM

I try to authorize my web app but I can't.

Basically, I redirect user to Twitter without any problem. Twitter asks to authorize my app (so 

I think that everything is ok until this) and then will redirect user to my landing page, with oauth paramters in url (GET request). Ok.

Now when I call auth.CompleteAuthorization I always found that user is "not authorized", and I cannot access user tokens (properties are Nothing/Null).

Any idea?


Dec 5, 2012 at 12:21 PM


Do you have a sample of the code you're using? Important: don't post credentials.

When your app isn't authorized, you'll often receive an exception with a 401 Unauthorized message. Just in case, here's the FAQ to help work through those problems:

LINQ to Twitter FAQ


Dec 6, 2012 at 7:46 AM

Here there is the related codebehind.

I don't post any credentials, I use the function provided from linqToTwitter to redirect user to twitter oauth page. I don't receive any 401, I can see the right twitter page that asks for authorizing my application (reporting right name and logo).



 Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        If Not Me.IsPostBack AndAlso Not String.IsNullOrWhiteSpace(Request.QueryString("oauth_token")) Then

            ' -- I'm in twitter callback

        End If

    End Sub


#Region "Twitter integration"

    Private _auth As LinqToTwitter.WebAuthorizer
    Private _twitterCtx As LinqToTwitter.TwitterContext

    Private Function GetTwitterCallBackUrl() As Uri

        Dim url As New UriBuilder(Request.Url)
        Return url.Uri

' tried a fixed absolute url
        'Return New Uri("http://www.myweb.com/twitter/default.aspx")

    End Function

    Protected Sub StartTwitter_Click(sender As Object, e As EventArgs) Handles startTwitter.Click



    End Sub

    Protected Sub EndTwitter()



        If _auth.IsAuthorized Then

            lblTEst.Text = _auth.Credentials.AccessToken & "    " & _auth.Credentials.OAuthToken

' omitted: code to access and save credentials from _auth.Credentials.AccessToken and _auth.Credentials.OAuthToken

' that I found always nothing

        End If


    End Sub

    Private Sub InitializeTwitter()

        Dim credentials As LinqToTwitter.IOAuthCredentials = New LinqToTwitter.SessionStateCredentials
        If String.IsNullOrWhiteSpace(credentials.ConsumerKey) OrElse String.IsNullOrWhiteSpace(credentials.ConsumerSecret) Then

            credentials.ConsumerKey = ConfigurationManager.AppSettings("twitterConsumerKey")
            credentials.ConsumerSecret = ConfigurationManager.AppSettings("twitterConsumerSecret")

        End If

        _auth = New LinqToTwitter.WebAuthorizer()
        _auth.Credentials = credentials
        _auth.PerformRedirect = Sub(authUrl As String)
                                End Sub

    End Sub

#End Region

Dec 7, 2012 at 12:00 AM

I'm not sure if there is anything wrong with your code. However, I did create a new demo on the samples page, which uses a lot of your code:


It's named WebFormsVBSearchDemo. Please try that and compare with what you've written.

Just a thought, if you're using SessionState credentials, make sure you aren't using in-memory. The Web server can recycle the app at any time, meaning that any in-memory state will be lost. It's best to at least start the ASP.NET State service and modify your Web.config to use <sessionState mode="StateServer"/>.


Dec 7, 2012 at 8:07 AM

I'ts really, really strange.

I did run you app: all ok.

Then I did run my app: now all ok!

I really cannot explain this....


Two notes:

you can improve the sample moving the twitter data downloading block

 _twitterCtx = New TwitterContext(_auth)

            Dim search =
                (From srch In _twitterCtx.Search
                 Where srch.Type = SearchType.Search AndAlso
                       srch.Query = "LINQ to Twitter") _

            TwitterListView.DataSource = search.Statuses


inside the block

  If _auth.IsAuthorized Then

            ' omitted: code to access and save credentials from _auth.Credentials.AccessToken and _auth.Credentials.OAuthToken

            ' that I found always nothing

        End If

and, of course, my comments here was related to my problem so you can remove.

Or, better, move the binding code inside Page.Databinding event handler and call me.DataBind after   If _auth.IsAuthorized Then



2 - In fact I'm using in memory state, but I think this cannot be the cause of the problem because I make a lot of test, and my app pools don't recycle so often. In fact, I did try also from VS debugger, that doesn't recycle at all. However, using that kind of code, where is the problem? The SessionStateCredentials instance is created in every page execution, so if the app is recycled I think that it will not be a problem. In fact, I think also I can use InMemoryCredentials, assigning consumerKey and secret every time I instantate the object, and this will works. Or I've not understood how the library works?


Thanks a lot for you assistance!


Dec 7, 2012 at 11:49 AM

You're correct about InMemoryCredentials and SessionStateCredentials. Happy you have it working now.


Dec 7, 2012 at 2:23 PM

Thank you, and my congratulations for the excellent work! :)