Home » Asp.netRSS

Sending a Tweet from VB.NET to Twitter

Well, it used to be pretty simple to send a Tweet to twitter from within my vb.net website. Now they have changed the rules using OAuth! I can't seem to find a simple solution to send tweets. All I am really sending are announcements as to what is going on. I need to be able to do it all behind the scenes whenever a new event is entered by the web site user.

I Have the consumerKey and the consumerSecret, how do I get the PIN and the  OAuthToken, and the OAuthTokenSecret?

Can anyone give me a valid VB Example that I can modify to work on my web site? No dll's just good, simple code.

Thanks.

 

3 Answers Found

 
 

Answer 2

Rajneesh, are these examples still viable? It seems that twitter  has stopped using all non-OAuth methods, as is evidenced by my code that stopped working. When I queried twitter, they told me they have quite accecpting non-OAuth requests. 

 

Answer 3

Well I have managed to clean up the code significantly. I added all 4 required values, ConsumerKey, ConsumerSecret, OAuthToken, and OAuthTokenSecret to the web.config file in the "appSettings" section of the "configuration" section.

 <appSettings>
  <!-- Twitter and BitLy access keys -->
  <add key="ConsumerKey" value="YOUR_CONSUMER_KEY" />
  <add key="ConsumerSecret" value="YOUR_CONSUMER_SECRET_KEY" />
  <add key="OAuthToken" value="YOUR_APPS_OAUTHTOKEN_KEY" />
  <add key="OAuthTokenSecret" value="YOUR_APPS_OAUTHTOKEN_SECRET_KEY" />
 </appSettings>

If found that by going to twitter.com and signing into the account that is using the application, then go to the "Settings" page, go to the bottom of the page and click on the "API" menu option. Once the API page appears you will clik on the button "2", Register An App. On the right side on the new page, you will see a button "View Your Applications." If you have previously registered your app, click this button, If not fill out the appliction for a new app. Now at the View your Applications page, select the app you want to get the authorization keys for where is says "Edit Details".

Now click on the button to the right that says, "Application Detail." Here you will find your Consumer Key and your Consumer Secret Key. On the right side of this page you will see the menu option "My Access Token", click it and you will find your oauth_token and your oauth_token_secret keys!

Not that you have all the keys, put them in the "appSettings" section of the web.config file.

Add "Dim twConn As New TwitterVB2.TwitterAPI" at the top of the page.

I then created a subroutine where I pass in the Tweet:

  Public Sub PostOnTwitter(ByVal Tweet As String)
         'Retrieve Keys from web.config      
        Dim twitterKey As String = System.Configuration.ConfigurationManager.AppSettings("ConsumerKey")
        Dim twitterSecret As String = System.Configuration.ConfigurationManager.AppSettings("ConsumerSecret")
        Dim twitterOAuthToken As String = System.Configuration.ConfigurationManager.AppSettings("OAuthToken")
        Dim twitterOAuthTokenSecret As String = System.Configuration.ConfigurationManager.AppSettings("OAuthTokenSecret")

        Try
            twConn.AuthenticateWith(twitterKey, twitterSecret, twitterOAuthToken, twitterOAuthTokenSecret)
            twConn.Update(Trim(Tweet))
        Catch ex As Exception
            lblError.Text = "Twitter Returned: " & ex.Message
        End Try
    End Sub

This code works great in an enviornment that allows at least a "Medium" trust level. However it appears that many of the "new" hosting envrionments such as "Cloud Hosting" and others do not allow this trust level automatically!

However if the trust level is less than "Full" on your host you will likely receive the error message: "Twitter Returned: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed."

If anyone has found a fix for this "Trust Level" issue please post it!

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter