Vb.net forms application returning Invalid or expired token (401) upon trying to tweet with 2.1.6.0

Jun 12, 2013 at 7:04 PM
Hi,
I’m trying to create my 1st project to use the LINQtoTwitter library to tweet from my VB.NET ASP forms single user application to twitter. I’m doing this in VS2010. I tried two different libraries with the same result: the LINQ to Twitter Beta v2.1.05 and 2.1.6.0 released just a couple days ago.

When I run the application application I see the following error messages:
Exception Message: Invalid or expired token
and
Inner Exception Message: System.Net.WebException: The remote server returned an error: (401) Unauthorized. at LinqToTwitter.TwitterExecute.PostToTwitter[T](String url, IDictionary2 postData, Func2 getResult)

Here is the relevant block of code. Can you help me to get this working? The code errors out at the line:
Dim tweet = twitterCtx.UpdateStatus(txtBoxTweet.Text)
   Try

       _     'I need to put in credentails - Otherwise I get 407 errors_
            Dim webRequest As HttpWebRequest
            webRequest = Net.WebRequest.Create("https://api.twitter.com/1/")
            webRequest.Proxy = Net.WebRequest.DefaultWebProxy
            webRequest.Credentials = New NetworkCredential("user", "password", "domain"
)
            webRequest.Proxy.Credentials = New NetworkCredential "user", "password", "domain"
)

            Dim resultResponse As String = ""

            ResultLabel.Visible = True

            Dim auth As New SignInAuthorizer

            Dim Credentials As New InMemoryCredentials

 _    'Grab keys from the web.config file. They keys are from the application registered on https://dev.twitter.com/apps_

            If (Credentials.AccessToken Is Nothing) Or (Credentials.ConsumerSecret Is Nothing) Then
                Credentials.ConsumerKey = ConfigurationManager.AppSettings.Item("devl_twitter_consumer_key")
                Credentials.ConsumerSecret = ConfigurationManager.AppSettings.Item("devl_twitter_consumer_secret")
                Credentials.AccessToken = ConfigurationManager.AppSettings.Item("devl_twitter_access_token")
                Credentials.OAuthToken = ConfigurationManager.AppSettings.Item("devl_twitter_access_token_secret")
            End If

            auth.Credentials = Credentials
            Dim twitterCtx As TwitterContext = New TwitterContext(auth)

            Dim tweet = twitterCtx.UpdateStatus(txtBoxTweet.Text)

            resultResponse = "Status returned: " & _
                "(" & tweet.ID & ")" & _
                "[" & tweet.User.ID & "]" & _
                tweet.User.Name & ", " & _
                tweet.Text & ", " & _
                tweet.CreatedAt

            ResultLabel.Text += "<br /><br />Tweet Posting Response: " & resultResponse


        Catch ex As Exception
            ResultLabel.Text += "<br /><br />It appears there was an issue with the tweet: The exception Message:  " & ex.Message & _
                "<br /><br />The Inner Message: " & ex.InnerException.ToString
            ResultLabel.ForeColor = Drawing.Color.Red

        End Try
Thanks,
Hillo
Coordinator
Jun 12, 2013 at 7:41 PM
Hi,

For 401 errors, you should review the FAQ.

Instead of the SignInAuthorizer, try to use the SingleUserAuthorizer.

Also, there is a LinqToTwitterDemoVB project in the downloadable source code. It has VB examples and you can use that to debug any authorization problems.

@JoeMayo
Jun 12, 2013 at 8:08 PM
I read the FAQ, but nothing seemed to help. Trying SingleUserAuthorizer didn't help either. I just downloaded the LinqToTwitterDemoVB from the source code and will try that now.
Jun 13, 2013 at 4:21 PM
Edited Jun 13, 2013 at 4:22 PM
Hi, I ran the LinqToTwitterDemoVB and it was able to interact with the twitter account after Authorizing the application and entering the pin.

However, I was hoping I wouldn't have to make users authorize the application each time they run the application.

Is there a way I can use access_token and the access_token_secret such that authorization is not necessary? Or am I missing something is entering the pin suppose to be a one time deal or something?

I am trying to develop an application that will automatically send twitter updates when certain triggers are sent off.

Thanks.

hillo
Jun 13, 2013 at 8:23 PM
I have resolved the issue!!!

Although I thought I tried this before (maybe I wasn't using SingleUserAuthorizer), I was setting the credentials wrong, they should have been set as follows, though the names are not intuitive.
Credentials.AccessToken = ConfigurationManager.AppSettings.Item("devl_twitter_access_token_secret")
Credentials.OAuthToken = ConfigurationManager.AppSettings.Item("devl_twitter_access_token")
I found out that you can instead of setting the AccessToken and OAuthToken, you can more intuitively set them this way and get the same result:
Credentials.TwitterAccessToken = ConfigurationManager.AppSettings.Item("devl_twitter_access_token")
Credentials.TwitterAccessTokenSecret = ConfigurationManager.AppSettings.Item("devl_twitter_access_token_secret")
Coordinator
Jun 13, 2013 at 8:26 PM
Congrats! and thanks for sharing your solution.

@JoeMayo
Coordinator
Jun 13, 2013 at 8:27 PM
After the first query, you can save credentials for that user and reload them on subsequent queries. Twitter doesn't expire tokens.

@JoeMayo
Jan 25, 2014 at 12:11 PM
Hi,
I have developed a web application with Twitter Authorization. Once user click the sign in with twitter it authorize the application and redirect to home page and againg the same user click login with twitter it again authorize the app then redirect to page.
I was hoping I wouldn't have to make users authorize the application each time they run the application.
is there a way to authorize the app once when user sign in.
I am trying to develop a web application with Sign in with Twitter and login Functionality.
i downloaded "LinqToTwitterWebFormsDemo" from the source code and try with that.
Thanks,
Lalit