How To: Update your Twitter status with ASP

Here is a simple script I wrote in classic ASP to update your Twitter status via the Twitter API. This script handles basic HTTP authentication to validate your Twitter account and URL Encoding to send over friendly status updates.

<%
Response.Buffer = True
Dim xml
Set xml = Server.CreateObject("Microsoft.XMLHTTP")

twitter_username = "username"	'change to your twitter username
twitter_password = "password"	'change to your twitter password

new_status = "visit strangework.com!"		'change to your new status

xml.Open "POST", "http://" & twitter_username & ":" & twitter_password & "@twitter.com/statuses/update.xml?status=" & server.URLencode(new_status), False
xml.setRequestHeader "Content-Type", "content=text/html; charset=iso-8859-1"
xml.Send

Response.Write xml.responseText		'view Twitter's response

Set xml = Nothing
%>
Enjoy this post? Be sure to subscribe to my RSS feed and my WordPress Tips and Tricks Newsletter! Also check out my WordPress books: Professional WordPress Third Edition & Professional WordPress Plugin Development

Comments

  1. Hi Brad!

    Thanks! I’ll include it in my posting tool for my blog!

  2. Wow – this worked first time…THANK YOU, THANK YOU, THANK YOU!

  3. Why would I get an error on the .Open?

    error ‘80004005’

    /include/updateProfile.asp, line 24

  4. Make sure you have the XMLHTTP component installed correctly.

  5. I’m at the mercy of a shared hosting plan, so I took a different approach:

    Dim xmlhttp
    Set xmlhttp = Server.CreateObject(“MSXML2.ServerXMLHTTP”)

    xmlhttp.open “POST”, “http://twitter.com/statuses/update.xml?source=madtownlounge&status=” & value, False,oRs(“MemberTwitterLogin”),oRs(“MemberTwitterPassword”)
    xmlhttp.setRequestHeader “Authorization”, “Basic ” & Base64Encode(oRs(“MemberTwitterLogin”) & “:” & oRs(“MemberTwitterPassword”))
    xmlhttp.send

  6. Fantastic Brad, thanks for posting this – opens a world of possibilities:D

  7. FYI, you may think you are doing a POST, but you are actually still submitting things on the querystring with your syntax above. To get it into the POST object you need to submit the querystring parameters as an argument to the send method. Also, the open method takes optional username, password parameters, so it is nicer to send them that way versus as part of the URL, otherwise you run into URL encoding woes if a user has a :, %, / or @ in their password.

    Kudos to these guys: http://www.4guysfromrolla.com/webtech/110100-1.2.shtml

    Try this code instead:

    Response.Buffer = True
    Dim xml
    Set xml = Server.CreateObject(“Microsoft.XMLHTTP”)

    twitter_username = “username” ‘change to your twitter username
    twitter_password = “password” ‘change to your twitter password

    new_status = “visit strangework.com!” ‘change to your new status

    xml.Open “POST”, “http://twitter.com/statuses/update.xml”, False, twitter_username, twitter_password
    xml.setRequestHeader “Content-Type”, “application/x-www-form-urlencoded”
    xml.Send “status=” & server.URLencode(new_status)”

    Response.Write xml.responseText ‘view Twitter’s response

    Set xml = Nothing

  8. Whoops, delete the extra ” in my example above:

    Response.Buffer = True
    Dim xml
    Set xml = Server.CreateObject(”Microsoft.XMLHTTP”)

    twitter_username = “username” ‘change to your twitter username
    twitter_password = “password” ‘change to your twitter password

    new_status = “visit strangework.com!” ‘change to your new status

    xml.Open “POST”, “http://twitter.com/statuses/update.xml”, False, twitter_username, twitter_password
    xml.setRequestHeader “Content-Type”, “application/x-www-form-urlencoded”
    xml.Send “status=” & server.URLencode(new_status)

    Response.Write xml.responseText ‘view Twitter’s response

    Set xml = Nothing

  9. Thank you! Are there other Classic ASP code that you can share to interact with Twitter such as auto-reply to those who follow you?

  10. Hi this works a treat… however it breaks if you try to login using your email address…. any ideas how come?

    thx

  11. wow. i rarely come across well written scripts that work the first time. you rock my man!

  12. Tried your code and it’s nice.

    I run a radio station and want to tweet what’s playing when a song changes.

    I’ve only been able to send these new tweets when the page I have the code on is refreshed in my browser.

    I’m looking to have something where the tweets will be updated when a file is generated with the message in it. In this case, the message is a song title and artist.

    I’ve been able to semi-automate this by throwing a meta refresh tag into my .asp page and having the page opened in a browser. There’s gotta be a way to streamline this…

  13. You should ask WJJO 94.1 – they are a Madison-based radio station and they are posting the same thing you are looking for.

    http://twitter.com/WJJO

  14. When the file is generated via ASP, run this code on the next line after the file is generated. It’s that simple.

  15. Hi there,

    Great code and worked first time.

    What’s the deal with specifying the source of the status update.

    Adding:

    source=XXX

    into the post data works only if Twitter recognises the XXX, othertimes it doesn’t. Do you have to register the source somewhere?

    Thanks.

  16. You have to email Twitter and ask to be added to the source list. I contacted Biz Stone and he added SnapFoo.com to the Twitter source, so anything coming from SnapFoo said so on Twitter.

  17. perfect! thanks!

  18. fantastic little piece of code, great job!

  19. Outstanding, worked without a hitch. Probably could have figured this out eventually, but this saved me a lot of experimentation time. Thanks!

  20. “Hi this works a treat… however it breaks if you try to login using your email address…. any ideas how come?”

    Ever hear the story about the man who went to see his doctor and told him, “doctor, my arm hurts when I raise it above my head”? The doctor replied, “don’t raise it above your head.”

    🙂

    To answer your question, though, twitter uses a standard built in web server login method, which requires a username and password, and can’t equate your email with that username. So, just use the username instead of the email. If you’re working with a bunch of different users using the same script (or whatever), you could create either an array or a database with username/email address pairs, then use that array or database to match a given email address with the username, then use that matched username in the script on this page.

  21. Hi,

    The code works perfectly but.. I have a problem and I hope you may help me.

    When I try to send a message like this:
    Reverón: titiritero
    Twitter show me this:
    Reverótitiritero

    And I really give up trying to know why?

    Please give me a hand on this!

    Thanks!

  22. A lot of people (myself included) seem to have permission issues using Microsoft.XMLHTTP to access a remote site. Even the fixes for relaxing security didn’t help me. So I just used Softwing.AspTear, which is a free dll. The equivalent code is:

    Set URLFetchObj = CreateObject(“SOFTWING.AspTear”)
    On Error Resume Next
    sResponse = URLFetchObj.Retrieve(“http://twitter.com/statuses/update.xml”, 1, “status=” & Server.URLencode(“[tweet text]”), “[twitter_username]”, “[twitter_password]”)
    If Err.Number 0 Then sResponse = Err.Number & ” ” & Err.Description
    On Error Goto 0

    Response.Write “Twitter says: ” & sResponse

  23. Anyone know how to implement this code using Classic ASP and OAuth?

    (as it seems the oauth is now a requirement with Twitter)

  24. Couldn’t get this to work. Kept getting “could not authenticate you” errors. Yes, I made sure my username and password were correct. Anyone else having this problem?

    • Same problem here rick.. Tried with two accounts now.

      Anybody else have to deal with this? Any idea what it might possibly be?

  25. Same problem as Asdrubal concerning accentuation marks and punctuaction.
    Any solutions?

  26. Excellent example folks, was most impressed.

    Richie Rich, don’t think it’s a requirement yet, but I too would also be interested in seeing any other examples.

  27. Thiago Santos says:

    Excellent!!!
    Very easy to use.

  28. I just created a new Twitter account today and tried the code. I don’t get any error messages, but only a blank screen when I call my function. Anyone know if it takes time for this to work after creating an account?

  29. @Kenny BruntonNo any joy with a Classic ASP version of twitter/oauth? Getting desperate now 🙂

  30. Hi

    Anyone got an idea if I can get the person Icon with Classic ASP?

    Best regards

    Andre
    antispam@key.co.za

  31. Thanks for the code! Works the 1st time.

    Andre, if you parse the xml response, it’s in the profile_image_url tag.

  32. Thanks RO. Yes, I have coded a parser using INSTR and MID, but is there a cleaner way to read that tag using asp. Eg GetElementsByTagName(“image”)

  33. Andre,

    Use: objXMLdoc.GetElementsByTagName(“user/profile_image_url”).Item(0).text

    That will give you the URL of the user’s profile image.

  34. Coolguy97 says:

    What about Posting a tweet using the OAuth.
    Using asp.net.

  35. Hi

    I’m creating a project page to get a whole lot of people together to create an oauth library for Twitter using classic asp.

    The code group is at http://code.google.com/p/twitter-oauth-in-classic-asp/

    and the groups at

    http://groups.google.com/group/twitter-oauth-in-classic-asp

    Please join and help!

  36. jeremy flint says:

    I am trying to append a hashtag to the status, but the API seems to ignore it. Has anyone had any luck with that?

    I am trying it several different ways. Including it as a hidden field value and building the status using Request.Form, by adding it to the end of the URL that is being generated as & “+%23tagname”.

    Any ideas?

  37. is there anyway in classic asp to have some one login or signup via their twitter account if your site is backed by an access database, like twitpic users are able to signup for that site via twitter and began using it.. i havent found any twitter login info that coincides with classic asp

  38. I tried “https” for the twitter link and it worked. Keeps the login info secured.

  39. Hi Brad, thanks for the help. I love the classic asp.

    Have you considered making a google friend connect login script it would be a one touch and you could open source it in there code base and you would get traffic to your site and talents.

  40. Hi Brad,
    Thanks for your excellent scripts.. I am getting “Basic authentication is not supported ” Error.. Can you please give me any solution how to fix this.

    Thanks

  41. it doesn’t work anymore, twitter has discontinued the use of basic authentication.
    Someone have a solution ?

  42. Can you help me get that Twitter API integration script for classic ASP ?
    Thanks a bunch!